Skip to content

Commit 938aecb

Browse files
committed
Share logic for write transcoding
1 parent 5c7340f commit 938aecb

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

lib/truffle/stringio.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,7 @@ def write(str)
280280
str = String(str)
281281
return 0 if str.empty?
282282

283-
if external_encoding &&
284-
external_encoding != str.encoding &&
285-
external_encoding != Encoding::BINARY &&
286-
str.encoding != Encoding::BINARY
287-
str = str.encode(external_encoding)
288-
end
283+
str = Truffle::IOOperations.write_transcoding(str, external_encoding)
289284

290285
d = @__data__
291286
TruffleRuby.synchronized(d) do

src/main/ruby/truffleruby/core/io.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,11 +2324,7 @@ def write(*objects)
23242324

23252325
ensure_open_and_writable
23262326

2327-
if external_encoding && external_encoding != string.encoding && external_encoding != Encoding::BINARY
2328-
unless string.ascii_only? && external_encoding.ascii_compatible?
2329-
string = string.encode(external_encoding)
2330-
end
2331-
end
2327+
string = Truffle::IOOperations.write_transcoding(string, external_encoding)
23322328

23332329
count = Truffle::POSIX.write_string self, string, true
23342330
bytes_written += count

src/main/ruby/truffleruby/core/truffle/io_operations.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def self.puts(io, *args)
7272
nil
7373
end
7474

75+
def self.write_transcoding(string, external_encoding)
76+
if external_encoding && external_encoding != string.encoding && external_encoding != Encoding::BINARY &&
77+
!(string.ascii_only? && external_encoding.ascii_compatible?)
78+
string.encode(external_encoding)
79+
else
80+
string
81+
end
82+
end
83+
7584
def self.dup2_with_cloexec(old_fd, new_fd)
7685
if new_fd < 3
7786
# STDIO should not be made close-on-exec. `dup2` clears the close-on-exec bit for the destination FD.

0 commit comments

Comments
 (0)