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 `invalid: :replace` for `CSV.open` #130

Merged
merged 1 commit into from Jun 4, 2020

Conversation

@koic
Copy link
Contributor

@koic koic commented Jun 3, 2020

This PR adds invalid: :replace for CSV.open. It is a PR similar to #129.

@@ -156,7 +156,7 @@ def quote(field)
else
field = String(field) # Stringify fields
# represent empty fields as empty quoted fields
if (@quote_empty and field.empty?) or @quotable_pattern.match?(field)
if (@quote_empty and field.empty?) or @quotable_pattern.match?(field.scrub)

This comment has been minimized.

@kou

kou Jun 3, 2020
Member

I don't think that this is a good approach.
If we have \uFFFD in @quotable_pattern, it matches all invalid characters.

We need to process this case carefully something like:

Suggested change
if (@quote_empty and field.empty?) or @quotable_pattern.match?(field.scrub)
if (@quote_empty and field.empty?) or (filed.valid_encoding? and @quotable_pattern.match?(field)

Could you create a separated pull request for this case and then we can back to this pull request?

This comment has been minimized.

@koic

koic Jun 4, 2020
Author Contributor

Thanks for your suggestion. I think that is definitely a safe approach and suitable. I've opened #131.

This comment has been minimized.

@koic

koic Jun 4, 2020
Author Contributor

I rebased this PR with the latest mater branch.

@koic koic force-pushed the koic:add_invalid_replace_option_for_csv_open branch from 76bdf53 to 59b4305 Jun 4, 2020
This PR adds `invalid: :replace` for `CSV.open`. It is a PR similar to #129.

And this PR uses `String#scrub` to prevent the following `ArgumentError`.

```ruby
/[,"]/.match?("\x82\xA0")       #=> ArgumentError (invalid byte sequence in UTF-8)
/[,"]/.match?("\x82\xA0".scrub) #=> false
```
@koic koic force-pushed the koic:add_invalid_replace_option_for_csv_open branch from 59b4305 to d1f9bc0 Jun 4, 2020
@kou kou merged commit 5bf6873 into ruby:master Jun 4, 2020
17 checks passed
17 checks passed
Benchmark: Ruby 2.7: macos-latest
Details
Normal test: Ruby 2.5: macos-latest
Details
Benchmark: Ruby 2.7: ubuntu-latest
Details
Normal test: Ruby 2.5: ubuntu-latest
Details
Benchmark: Ruby 2.7: windows-latest
Details
Normal test: Ruby 2.5: windows-latest
Details
Normal test: Ruby 2.6: macos-latest
Details
Normal test: Ruby 2.6: ubuntu-latest
Details
Normal test: Ruby 2.6: windows-latest
Details
Normal test: Ruby 2.7: macos-latest
Details
Normal test: Ruby 2.7: ubuntu-latest
Details
Normal test: Ruby 2.7: windows-latest
Details
Scanner test: Ruby 2.7: macos-latest
Details
Scanner test: Ruby 2.7: ubuntu-latest
Details
Scanner test: Ruby 2.7: windows-latest
Details
Gem test
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@kou
Copy link
Member

@kou kou commented Jun 4, 2020

Thanks!

@koic koic deleted the koic:add_invalid_replace_option_for_csv_open branch Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.