Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -707,24 +707,34 @@ describe "String" do

describe "rindex" do
describe "by char" do
it { "bbbb".rindex('b').should eq(3) }
it { "foobar".rindex('a').should eq(4) }
it { "foobar".rindex('g').should be_nil }
it { "日本語日本語".rindex('本').should eq(4) }
it { "あいう_えお".rindex('_').should eq(3) }

describe "with offset" do
it { "bbbb".rindex('b', 2).should eq(2) }
it { "abbbb".rindex('b', 0).should be_nil }
it { "abbbb".rindex('b', 1).should eq(1) }
it { "abbbb".rindex('a', 0).should eq(0) }
it { "faobar".rindex('a', 3).should eq(1) }
it { "faobarbaz".rindex('a', -3).should eq(4) }
it { "日本語日本語".rindex('本', 3).should eq(1) }
end
end

describe "by string" do
it { "bbbb".rindex("b").should eq(3) }
it { "foo baro baz".rindex("o b").should eq(7) }
it { "foo baro baz".rindex("fg").should be_nil }
it { "日本語日本語".rindex("日本").should eq(3) }

describe "with offset" do
it { "bbbb".rindex("b", 2).should eq(2) }
it { "abbbb".rindex("b", 0).should be_nil }
it { "abbbb".rindex("b", 1).should eq(1) }
it { "abbbb".rindex("a", 0).should eq(0) }
it { "foo baro baz".rindex("o b", 6).should eq(2) }
it { "foo".rindex("", 3).should eq(3) }
it { "foo".rindex("", 4).should eq(3) }
Expand All @@ -738,8 +748,9 @@ describe "String" do
it { "bbbb".rindex(/\d/).should be_nil }

describe "with offset" do
it { "bbbb".rindex(/b/, -3).should eq(2) }
it { "bbbb".rindex(/b/, -1235).should be_nil }
it { "bbbb".rindex(/b/, 2).should eq(2) }
it { "abbbb".rindex(/b/, 0).should be_nil }
it { "abbbb".rindex(/a/, 0).should eq(0) }
it { "日本語日本語".rindex(/日本/, 2).should eq(0) }
end
end
Expand Down
5 changes: 2 additions & 3 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2738,12 +2738,11 @@ class String
end

# ditto
def rindex(search : Regex, offset = 0)
offset += size if offset < 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed? You must support negative indexing from the end of the string.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The substring (self[0..offset]) handles negative indexes. There is no need to bother until we change that for a more performant implementation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why are the specs for negative indexes removed, and why do we have

return nil unless 0 <= offset <= size

which always returns nil for negative indexes?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh, true that won't work.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll just replace the implementation as suggested above. And add more specs...

def rindex(search : Regex, offset = size - 1)
return nil unless 0 <= offset <= size

match_result = nil
self[0, self.size - offset].scan(search) do |match_data|
self[0..offset].scan(search) do |match_data|
match_result = match_data
end

Expand Down