Skip to content

Commit

Permalink
[Truffle] Backported a String#scrub fix from Rubinius 2.5.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvdrum committed Apr 3, 2015
1 parent 4f50d0a commit c2146b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/string/scrub_tags.txt
@@ -1,2 +1 @@
fails(needs unpack):String#scrub with a block replaces invalid byte sequences
fails(inherited - rubinius):String#scrub with a block replaces invalid byte sequences using a custom encoding
12 changes: 11 additions & 1 deletion truffle/src/main/ruby/core/rubinius/common/string.rb
Expand Up @@ -1948,6 +1948,12 @@ def scrub(replace = nil)
replace = replace.force_encoding(Encoding::BINARY)
end

# MRI appears to just return a copy of self when the input encoding is
# BINARY/ASCII_8BIT.
if input.encoding == Encoding::BINARY
return input
end

converter = Encoding::Converter.new(input.encoding, Encoding::BINARY)

while input.length > 0
Expand All @@ -1958,8 +1964,12 @@ def scrub(replace = nil)
elsif result == :undefined_conversion
output << converter.primitive_errinfo[3]
else
# Blocks can return strings in any encoding so we'll make sure it's the
# same as our buffer for the mean time.
if block_given?
output << yield(converter.primitive_errinfo[3])
block_output = yield(converter.primitive_errinfo[3])

output << block_output.force_encoding(output.encoding)
else
output << replace
end
Expand Down

1 comment on commit c2146b2

@nirvdrum
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisseaton Both of these modifications were made in 2014, so I left the copyright header intact.

Please sign in to comment.