Skip to content

Commit dc3d4c2

Browse files
committed
Correctly handle multibyte characters for String#gsub(String, String)
1 parent 74d7abe commit dc3d4c2

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

spec/ruby/core/string/gsub_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,14 @@ def obj.to_s() "ok" end
594594
end
595595
end
596596

597+
describe "String#gsub with a string pattern" do
598+
it "handles multibyte characters" do
599+
"é".gsub("é", "â").should == "â"
600+
"aé".gsub("é", "â").should == "aâ"
601+
"éa".gsub("é", "â").should == "âa"
602+
end
603+
end
604+
597605
describe "String#gsub! with pattern and replacement" do
598606
it "modifies self in place and returns self" do
599607
a = "hello"

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4528,11 +4528,7 @@ protected RubyNode coercePatternToRope(RubyNode pattern) {
45284528
return ToRopeNodeGen.create(pattern);
45294529
}
45304530

4531-
@Specialization(
4532-
guards = {
4533-
"offset >= 0",
4534-
"singleByteOptimizableNode.execute(stringRope)",
4535-
"!patternFits(stringRope, patternRope, offset)" })
4531+
@Specialization(guards = { "offset >= 0", "!patternFits(stringRope, patternRope, offset)" })
45364532
protected Object stringByteIndexPatternTooLarge(Rope stringRope, Rope patternRope, int offset) {
45374533
return nil;
45384534
}
@@ -4590,7 +4586,7 @@ protected Object stringByteIndex(Rope stringRope, Rope patternRope, int offset,
45904586

45914587
final Encoding enc = stringRope.getEncoding();
45924588
final CodeRange cr = stringRope.getCodeRange();
4593-
int c = 0;
4589+
int c;
45944590

45954591
for (; p < l; p += c) {
45964592
c = calculateCharacterLengthNode.characterLength(enc, cr, Bytes.fromRange(stringBytes, p, e));

0 commit comments

Comments
 (0)