Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: String#gsub replacing ascii chars to work in non-ascii string #5350

Merged
merged 3 commits into from
Jan 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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