Project

General

Profile

Actions

Bug #22715

open

String#each_line drops text before a separator match in the middle of a character

Bug #22715: String#each_line drops text before a separator match in the middle of a character

Added by saddestmartian (sad mart) 1 day ago. Updated 1 day ago.

Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-12-03T23:00:19Z master bcd243e8e8) +PRISM [x86_64-linux]
[ruby-core:127843]

Description

String#each_line and String#lines with a separator drop the text before a byte match of the separator that falls inside a multibyte character:

s = "a\x83\x5Cb\\c".force_encoding("Shift_JIS")  # "\x83\x5C" is one character
s.lines("\\")   #=> ["b\\", "c"]       expected ["a\x{835C}b\\", "c"]
t = "a\x83\x5C".force_encoding("Shift_JIS")
t.lines("\\")   #=> []                 expected ["a\x{835C}"]

In rb_str_enumerate_lines, skipping a match in the middle of a character moves the start of the current line as well as the search position, so the text before it is never yielded. The same class was fixed for String#split in fef67df224 (GitHub #19706).

It affects encodings whose characters can contain the separator bytes after the first byte (Shift_JIS, Windows-31J, GBK, GB18030, Big5, CP949), and multibyte separators in EUC-JP. It reproduces on 2.1 through 3.4.13, 4.0.10 and master.

A patch with tests: https://github.com/ruby/ruby/pull/20472

Actions

Also available in: PDF Atom