Skip to content

Commit

Permalink
Fix: String#gsub replacing ascii chars to work in non-ascii string (#…
Browse files Browse the repository at this point in the history
…5350)

Fixes #5348
  • Loading branch information
straight-shoota authored and RX14 committed Jan 1, 2018
1 parent 00880be commit d7273c7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 4 additions & 0 deletions spec/std/string_spec.cr
Expand Up @@ -1248,6 +1248,10 @@ describe "String" do
s.gsub('x', "yz").should be(s)
end

it "gsubs char with char in non-ascii string" do
"".gsub('/', '-').should eq("")
end

it "gsubs char with string depending on the char" do
replaced = "foobar".gsub do |char|
case char
Expand Down
12 changes: 6 additions & 6 deletions src/string.cr
Expand Up @@ -2026,12 +2026,12 @@ class String

private def gsub_ascii_char(char, replacement)
String.new(bytesize) do |buffer|
each_char_with_index do |my_char, i|
buffer[i] = if my_char == char
replacement.ord.to_u8
else
unsafe_byte_at(i)
end
to_slice.each_with_index do |byte, i|
if char.ord == byte
buffer[i] = replacement.ord.to_u8
else
buffer[i] = byte
end
end
{bytesize, bytesize}
end
Expand Down

0 comments on commit d7273c7

Please sign in to comment.