Skip to content

Commit e5825de

Browse files
authored
[Bug #19769] Fix range of size 1 in String#tr
1 parent f15123c commit e5825de

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

spec/ruby/core/string/tr_s_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
"hello ^--^".tr_s("---", "_").should == "hello ^_^"
1818
end
1919

20+
ruby_bug "#19769", ""..."3.3" do
21+
it "accepts c1-c1 notation to denote range of one character" do
22+
"hello".tr_s('e-e', 'x').should == "hxllo"
23+
"123456789".tr_s("2-23","xy").should == "1xy456789"
24+
"hello ^-^".tr_s("e-", "a-a_").should == "hallo ^_^"
25+
"hello ^-^".tr_s("---o", "_a").should == "hella ^_^"
26+
end
27+
end
28+
2029
it "pads to_str with its last char if it is shorter than from_string" do
2130
"this".tr_s("this", "x").should == "x"
2231
end

spec/ruby/core/string/tr_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
"hello ^-^".tr("---", "_").should == "hello ^_^"
1717
end
1818

19+
ruby_bug "#19769", ""..."3.3" do
20+
it "accepts c1-c1 notation to denote range of one character" do
21+
"hello".tr('e-e', 'x').should == "hxllo"
22+
"123456789".tr("2-23","xy").should == "1xy456789"
23+
"hello ^-^".tr("e-", "a-a_").should == "hallo ^_^"
24+
"hello ^-^".tr("---o", "_a").should == "hella ^_^"
25+
end
26+
end
27+
1928
it "pads to_str with its last char if it is shorter than from_string" do
2029
"this".tr("this", "x").should == "xxxx"
2130
"hello".tr("a-z", "A-H.").should == "HE..."

string.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7714,8 +7714,10 @@ trnext(struct tr *t, rb_encoding *enc)
77147714
}
77157715
continue; /* not reached */
77167716
}
7717-
t->gen = 1;
7718-
t->max = c;
7717+
else if (t->now < c) {
7718+
t->gen = 1;
7719+
t->max = c;
7720+
}
77197721
}
77207722
}
77217723
return t->now;

test/ruby/test_string.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,8 @@ def test_tr
23012301
assert_not_predicate(str, :ascii_only?)
23022302
assert_not_predicate(star, :ascii_only?)
23032303
assert_not_predicate(result, :ascii_only?, bug13950)
2304+
2305+
assert_equal(S("XYC"), S("ABC").tr("A-AB", "XY"))
23042306
end
23052307

23062308
def test_tr!
@@ -2325,13 +2327,17 @@ def test_tr!
23252327
a = S("abc".force_encoding(Encoding::US_ASCII))
23262328
assert_nil(a.tr!(S("z"), S("\u0101")), '[ruby-core:22326]')
23272329
assert_equal(Encoding::US_ASCII, a.encoding, '[ruby-core:22326]')
2330+
2331+
assert_equal(S("XYC"), S("ABC").tr!("A-AB", "XY"))
23282332
end
23292333

23302334
def test_tr_s
23312335
assert_equal(S("hypo"), S("hello").tr_s(S("el"), S("yp")))
23322336
assert_equal(S("h*o"), S("hello").tr_s(S("el"), S("*")))
23332337
assert_equal("a".hash, S("\u0101\u0101").tr_s("\u0101", "a").hash)
23342338
assert_equal(true, S("\u3041\u3041").tr("\u3041", "a").ascii_only?)
2339+
2340+
assert_equal(S("XYC"), S("ABC").tr_s("A-AB", "XY"))
23352341
end
23362342

23372343
def test_tr_s!
@@ -2344,6 +2350,8 @@ def test_tr_s!
23442350
a = S("hello")
23452351
assert_equal(S("h*o"), a.tr_s!(S("el"), S("*")))
23462352
assert_equal(S("h*o"), a)
2353+
2354+
assert_equal(S("XYC"), S("ABC").tr_s!("A-AB", "XY"))
23472355
end
23482356

23492357
def test_unpack

0 commit comments

Comments
 (0)