Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling for ambiguous parsing options #226

Merged
merged 2 commits into from
Nov 18, 2021
Merged

Add handling for ambiguous parsing options #226

merged 2 commits into from
Nov 18, 2021

Conversation

adamroyjones
Copy link
Contributor

This pull request addresses this bug report.

Let me know if there are any issues or places where I can improve it!

lib/csv.rb Outdated Show resolved Hide resolved
lib/csv/parser.rb Show resolved Hide resolved
test/csv/parse/test_strip.rb Outdated Show resolved Hide resolved
test/csv/parse/test_strip.rb Outdated Show resolved Hide resolved
test/csv/parse/test_strip.rb Outdated Show resolved Hide resolved
With Ruby 3.0.2 and csv 3.2.1, the file

```ruby
require "csv"
File.open("example.tsv", "w") { |f| f.puts("foo\t\tbar") }
CSV.read("example.tsv", col_sep: "\t", strip: true)
```

produces the error

```
lib/csv/parser.rb:935:in `parse_quotable_robust': TODO: Meaningful
message in line 1. (CSV::MalformedCSVError)
```

However, the CSV in this example is not malformed; instead, ambiguous
options were provided to the parser. It is not obvious (to me) whether
the string should be parsed as

- `["foo\t\tbar"]`,
- `["foo", "bar"]`,
- `["foo", "", "bar"]`, or
- `["foo", nil, "bar"]`.

This commit adds code that raises an exception when this situation is
encountered. Specifically, it checks if the column separator either ends
with or starts with the characters that would be stripped away.

This commit also adds unit tests and updates the documentation.
@kou kou merged commit cc317dd into ruby:master Nov 18, 2021
@kou
Copy link
Member

kou commented Nov 18, 2021

Thanks!

@adamroyjones adamroyjones deleted the add-handling-for-ambiguous-parsing-options branch November 19, 2021 01:00
@adamroyjones
Copy link
Contributor Author

Thanks!

Any time! This was all really pleasant to work on—the Ruby community's reputation is deserved. Thanks!

kou pushed a commit to kou/ruby that referenced this pull request Dec 24, 2021
GitHub: fix rubyGH-225

With Ruby 3.0.2 and csv 3.2.1, the file

```ruby
require "csv"
File.open("example.tsv", "w") { |f| f.puts("foo\t\tbar") }
CSV.read("example.tsv", col_sep: "\t", strip: true)
```

produces the error

```
lib/csv/parser.rb:935:in `parse_quotable_robust': TODO: Meaningful
message in line 1. (CSV::MalformedCSVError)
```

However, the CSV in this example is not malformed; instead, ambiguous
options were provided to the parser. It is not obvious (to me) whether
the string should be parsed as

- `["foo\t\tbar"]`,
- `["foo", "bar"]`,
- `["foo", "", "bar"]`, or
- `["foo", nil, "bar"]`.

This commit adds code that raises an exception when this situation is
encountered. Specifically, it checks if the column separator either ends
with or starts with the characters that would be stripped away.

This commit also adds unit tests and updates the documentation.

ruby/csv@cc317dd42d
kou pushed a commit to ruby/ruby that referenced this pull request Dec 24, 2021
GitHub: fix GH-225

With Ruby 3.0.2 and csv 3.2.1, the file

```ruby
require "csv"
File.open("example.tsv", "w") { |f| f.puts("foo\t\tbar") }
CSV.read("example.tsv", col_sep: "\t", strip: true)
```

produces the error

```
lib/csv/parser.rb:935:in `parse_quotable_robust': TODO: Meaningful
message in line 1. (CSV::MalformedCSVError)
```

However, the CSV in this example is not malformed; instead, ambiguous
options were provided to the parser. It is not obvious (to me) whether
the string should be parsed as

- `["foo\t\tbar"]`,
- `["foo", "bar"]`,
- `["foo", "", "bar"]`, or
- `["foo", nil, "bar"]`.

This commit adds code that raises an exception when this situation is
encountered. Specifically, it checks if the column separator either ends
with or starts with the characters that would be stripped away.

This commit also adds unit tests and updates the documentation.

ruby/csv@cc317dd42d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants