Skip to content

Commit

Permalink
Share logic for write transcoding
Browse files Browse the repository at this point in the history
Co-authored-by: Mike Dalessio <mike.dalessio@gmail.com>
  • Loading branch information
eregon and flavorjones committed Mar 14, 2023
1 parent 5c7340f commit a27f06b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
7 changes: 1 addition & 6 deletions lib/truffle/stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,7 @@ 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
str = Truffle::IOOperations.write_transcoding(str, external_encoding)

d = @__data__
TruffleRuby.synchronized(d) do
Expand Down
6 changes: 1 addition & 5 deletions src/main/ruby/truffleruby/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2324,11 +2324,7 @@ def write(*objects)

ensure_open_and_writable

if external_encoding && external_encoding != string.encoding && external_encoding != Encoding::BINARY
unless string.ascii_only? && external_encoding.ascii_compatible?
string = string.encode(external_encoding)
end
end
string = Truffle::IOOperations.write_transcoding(string, external_encoding)

count = Truffle::POSIX.write_string self, string, true
bytes_written += count
Expand Down
10 changes: 10 additions & 0 deletions src/main/ruby/truffleruby/core/truffle/io_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ def self.puts(io, *args)
nil
end

def self.write_transcoding(string, external_encoding)
if external_encoding && external_encoding != string.encoding &&
external_encoding != Encoding::BINARY && string.encoding != Encoding::BINARY &&
!(string.ascii_only? && external_encoding.ascii_compatible?)
string.encode(external_encoding)
else
string
end
end

def self.dup2_with_cloexec(old_fd, new_fd)
if new_fd < 3
# STDIO should not be made close-on-exec. `dup2` clears the close-on-exec bit for the destination FD.
Expand Down

0 comments on commit a27f06b

Please sign in to comment.