Skip to content

Commit

Permalink
Fix String#tr 1 byte from optimization bug
Browse files Browse the repository at this point in the history
Ref: #5912 (comment)

`"aabbcc".tr("a", "xyz")` yields `"xyzxyzbbcc"` currently.
Of course it is unintentional behavior, in Ruby it yields `"xxbbcc"` and
on shell `echo aabbcc | tr a xyz` shows `xxbbcc`.
  • Loading branch information
makenowjust authored and ysbaddaden committed Apr 4, 2018
1 parent ec423eb commit 9662abe
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions spec/std/string_spec.cr
Expand Up @@ -1718,6 +1718,8 @@ describe "String" do
"hello".tr("helo", "1212").should eq("12112")
"this".tr("this", "").should eq("ⓧⓧⓧⓧ")
"über".tr("ü", "u").should eq("uber")
"aabbcc".tr("a", "xyz").should eq("xxbbcc")
"aabbcc".tr("a", "いろは").should eq("いいbbcc")
end

context "given no replacement characters" do
Expand Down
2 changes: 1 addition & 1 deletion src/string.cr
Expand Up @@ -1591,7 +1591,7 @@ class String
return delete(from) if to.empty?

if from.bytesize == 1
return gsub(from.unsafe_byte_at(0).unsafe_chr, to)
return gsub(from.unsafe_byte_at(0).unsafe_chr, to[0])
end

multi = nil
Expand Down

0 comments on commit 9662abe

Please sign in to comment.