Skip to content

Don't match a separator from the middle of a character - #249

Open
saddestmartian wants to merge 1 commit into
ruby:masterfrom
saddestmartian:getline-char-boundary
Open

saddestmartian wants to merge 1 commit into
ruby:masterfrom
saddestmartian:getline-char-boundary

Conversation

@saddestmartian

Copy link
Copy Markdown

StringIO#gets, #each_line and #readlines search for the separator byte by byte, so it can match the trailing bytes of a multibyte character:

s = "a\x83\x5Cb\\c".force_encoding("Shift_JIS")   # "\x83\x5C" is one character
StringIO.new(s).each_line("\\").to_a               # => ["a\x{835C}", "b\\", "c"]
                                                    # expected ["a\x{835C}b\\", "c"]
StringIO.new(s).each_line("\\", chomp: true).to_a  # => ["a\x83", "b", "c"]
                                                    # expected ["a\x{835C}b", "c"]

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:

StringIO.new("x\u0A41\u4E00y\nz".encode("UTF-16LE")).each_line.to_a
# => ["x\u0A41\x00", "\u794E\u0A00\x00", "z"], expected ["x\u0A41\u4E00y\n", "z"]

IO#each_line and String#each_line return the expected lines for the same bytes.

This accepts a match only at a character boundary, as IO#gets does, and otherwise searches again from the next character, as String#index does. 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 what IO#each_line returns.

`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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant