Repository navigation
Don't match a separator from the middle of a character - #249
Open
saddestmartian wants to merge 1 commit into
Open
saddestmartian wants to merge 1 commit into
saddestmartian wants to merge 1 commit into
Conversation
`StringIO#gets`, `#each_line` and `#readlines` search for the
separator byte by byte, so it could match the trailing bytes of a
multibyte character and split the character:
```ruby
s = "a\x83\x5Cb\\c".force_encoding("Shift_JIS")
StringIO.new(s).each_line("\\").to_a
# => ["a\x{835C}", "b\\", "c"], should be ["a\x{835C}b\\", "c"]
StringIO.new(s).each_line("\\", chomp: true).to_a
# => ["a\x83", "b", "c"], should be ["a\x{835C}b", "c"]
```
"\x83\x5C" is one character in Shift_JIS whose second byte is "\\".
The same happens in other encodings whose characters can contain ASCII
bytes after their first byte, such as Windows-31J, GBK, GB18030 and
Big5.
The default separator has the same problem in UTF-16 and UTF-32,
where "\n" is converted to the encoding of the stream and can match
across two characters:
```ruby
StringIO.new("x\u0A41\u4E00y\nz".encode("UTF-16LE")).each_line.to_a
# => ["x\u0A41\x00", "\u794E\u0A00\x00", "z"]
# should be ["x\u0A41\u4E00y\n", "z"]
```
`IO#each_line` and `String#each_line` return the expected lines for
the same bytes.
Skip a match that doesn't start at a character boundary, as `IO#gets`
does, and search again from the next character, as `String#index`
does. The Java implementation has the same logic and gets the same
change.
`test_gets_separator_inside_a_character` expected "\u3042\u3042" to
be split by "\x81" in the middle of both characters. It now expects one
line, which is also what `IO#each_line` returns.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StringIO#gets,#each_lineand#readlinessearch for the separator byte by byte, so it can match the trailing bytes of a multibyte character:The default separator has the same problem in UTF-16 and UTF-32, where
"\n"is converted to the stream's encoding and can occur across two characters:IO#each_lineandString#each_linereturn the expected lines for the same bytes.This accepts a match only at a character boundary, as
IO#getsdoes, and otherwise searches again from the next character, asString#indexdoes. The Java implementation gets the same change.Tests cover a Shift_JIS separator with and without
chomp, a two-byte separator in a short line and after 1024 bytes (the Boyer-Moore path), and the default separator in UTF-16LE/BE and UTF-32LE/BE.test_gets_separator_inside_a_character(#248) expected"\u3042\u3042"to be split by"\x81"in the middle of both characters; it now expects one line, which is whatIO#each_linereturns.