Skip to content

Commit 5a86638

Browse files
committed
Fix String#lines for multibyte characters
PullRequest: truffleruby/543
2 parents 5975d2a + 6deb924 commit 5a86638

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 1.0 RC 12
2+
3+
Bug fixes:
4+
5+
* Fixed a bug with `String#lines` and similar methods with multibyte
6+
characters (#1543).
7+
18
# 1.0 RC 11
29

310
New features:

spec/ruby/core/string/shared/each_line.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
c.should == ["hello\n", "\n", "\n", "world"]
2828
end
2929

30+
it "splits strings containing multibyte characters" do
31+
s = <<~EOS
32+
foo
33+
🤡🤡🤡🤡🤡🤡🤡
34+
bar
35+
baz
36+
EOS
37+
38+
b = []
39+
s.send(@method) { |s| b << s }
40+
b.should == ["foo\n", "🤡🤡🤡🤡🤡🤡🤡\n", "bar\n", "baz\n"]
41+
end
42+
3043
it "taints substrings that are passed to the block if self is tainted" do
3144
"one\ntwo\r\nthree".taint.send(@method) { |s| s.tainted?.should == true }
3245

src/main/ruby/core/string.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ def each_line(sep=$/, chomp: false)
934934
sep = "\n\n"
935935
data = bytes
936936

937-
while pos < size
937+
while pos < bytesize
938938
nxt = Truffle.invoke_primitive(:find_string, self, sep, pos)
939939
break unless nxt
940940

@@ -964,10 +964,10 @@ def each_line(sep=$/, chomp: false)
964964
else
965965

966966
# This is the normal case.
967-
pat_size = sep.size
967+
pat_size = sep.bytesize
968968
unmodified_self = clone
969969

970-
while pos < size
970+
while pos < bytesize
971971
nxt = Truffle.invoke_primitive(:find_string, unmodified_self, sep, pos)
972972
break unless nxt
973973

0 commit comments

Comments
 (0)