Skip to content

Commit b8a18c1

Browse files
author
Ary Borenszweig
committedNov 7, 2016
String: fixed index and rindex for ascii chars (optimization only applies when string is ascii only). Fixes #3513
1 parent e019442 commit b8a18c1

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed
 

Diff for: ‎spec/std/string_spec.cr

+3
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ describe "String" do
546546
assert { "bar".index('r').should eq(2) }
547547
assert { "日本語".index('本').should eq(1) }
548548
assert { "bar".index('あ').should be_nil }
549+
assert { "あいう_えお".index('_').should eq(3) }
549550

550551
describe "with offset" do
551552
assert { "foobarbaz".index('a', 5).should eq(7) }
@@ -594,6 +595,7 @@ describe "String" do
594595
assert { "foobar".rindex('a').should eq(4) }
595596
assert { "foobar".rindex('g').should be_nil }
596597
assert { "日本語日本語".rindex('本').should eq(4) }
598+
assert { "あいう_えお".rindex('_').should eq(3) }
597599

598600
describe "with offset" do
599601
assert { "faobar".rindex('a', 3).should eq(1) }
@@ -801,6 +803,7 @@ describe "String" do
801803
assert { "foobar".ends_with?('x').should be_false }
802804
assert { "よし".ends_with?('し').should be_true }
803805
assert { "よし".ends_with?('な').should be_false }
806+
assert { "あいう_".ends_with?('_').should be_true }
804807
end
805808

806809
describe "=~" do

Diff for: ‎src/string.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ class String
20932093
# ```
20942094
def index(search : Char, offset = 0)
20952095
# If it's ASCII we can delegate to slice
2096-
if search.ascii?
2096+
if search.ascii? && ascii_only?
20972097
return to_slice.index(search.ord.to_u8, offset)
20982098
end
20992099

@@ -2150,7 +2150,7 @@ class String
21502150
# ```
21512151
def rindex(search : Char, offset = size - 1)
21522152
# If it's ASCII we can delegate to slice
2153-
if search.ascii?
2153+
if search.ascii? && ascii_only?
21542154
return to_slice.rindex(search.ord.to_u8, offset)
21552155
end
21562156

0 commit comments

Comments
 (0)
Please sign in to comment.