Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: StringIO#write transcodes strings with a different encoding #2927

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Bug fixes:
* Fix constants lookup when `BasicObject#instance_eval` method is called with a String (#2810, @andrykonchin).
* Don't trigger the `method_added` event when changing a method's visibility or calling `module_function` (@paracycle, @nirvdrum).
* Fix `rb_time_timespec_new` function to not call `Time.at` method directly (@andrykonchin).
* Fix `StringIO#write` to transcode strings with encodings that don't match the `StringIO`'s `external_encoding`. (#2839, @flavorjones)

Compatibility:

Expand Down
7 changes: 7 additions & 0 deletions lib/truffle/stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ def write(str)
str = String(str)
return 0 if str.empty?

if external_encoding &&
external_encoding != str.encoding &&
external_encoding != Encoding::BINARY &&
str.encoding != Encoding::BINARY
str = str.encode(external_encoding)
end
eregon marked this conversation as resolved.
Show resolved Hide resolved

d = @__data__
TruffleRuby.synchronized(d) do
pos = d.pos
Expand Down
20 changes: 20 additions & 0 deletions spec/ruby/library/stringio/write_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@
describe "StringIO#write when in append mode" do
it_behaves_like :stringio_write_append, :write
end

describe "StringIO#write transcoding" do
describe "when UTF-16 encoding is set" do
it "accepts a UTF-8-encoded string and transcodes it" do
eregon marked this conversation as resolved.
Show resolved Hide resolved
io = StringIO.new.set_encoding(Encoding::UTF_16)
utf8_str = "hello"

io.write(utf8_str)

result = io.string
expected = [
254, 255, # BOM
0, 104, 0, 101, 0, 108, 0, 108, 0, 111, # double-width "hello"
]

io.external_encoding.should == Encoding::UTF_16
result.bytes.should == expected
end
end
end