Skip to content

Commit be96ee5

Browse files
committed
[GR-17457] Update StringIO position correctly after reading multi-byte characters.
PullRequest: truffleruby/3421
2 parents 2d0ae5e + 3d5c7b6 commit be96ee5

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ New features:
66

77
Bug fixes:
88

9+
* Fix `StringIO` to set position correctly after reading multi-byte characters (#2207, @aardvark179).
910

1011
Compatibility:
1112

lib/truffle/stringio.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def each_byte
211211
d = @__data__
212212
string = d.string
213213

214-
while d.pos < string.length
214+
while d.pos < string.bytesize
215215
check_readable
216216
byte = string.getbyte d.pos
217217
d.pos += 1
@@ -468,7 +468,7 @@ def read(length = nil, buffer = nil)
468468
buffer.replace str if buffer
469469
end
470470

471-
d.pos += str.length
471+
d.pos += str.bytesize
472472
str
473473
end
474474

spec/ruby/library/stringio/shared/read.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
8989
@io.send(@method)
9090
@io.pos.should eql(7)
9191
end
92+
93+
it "correctly update the current position in bytes when multi-byte characters are used" do
94+
@io.print("example\u03A3") # Overwrite the original string with 8 characters containing 9 bytes.
95+
@io.send(@method)
96+
@io.pos.should eql(9)
97+
end
9298
end
9399

94100
describe :stringio_read_nil, shared: true do

0 commit comments

Comments
 (0)