Skip to content

Commit

Permalink
Add Regex::MatchOptions overload for String#index! (#14462)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Apr 16, 2024
1 parent a4ca7cc commit fc3352c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -995,13 +995,15 @@ describe "String" do
describe "by regex" do
it { "string 12345".index(/\d+/).should eq(7) }
it { "12345".index(/\d/).should eq(0) }
it { "Hello\xFF".index(/l/, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(2) }
it { "Hello, world!".index(/\d/).should be_nil }
it { "abcdef".index(/[def]/).should eq(3) }
it { "日本語日本語".index(/本語/).should eq(1) }

describe "with offset" do
it { "abcDef".index(/[A-Z]/).should eq(3) }
it { "foobarbaz".index(/ba/, -5).should eq(6) }
it { "Hello\xFF".index(/l/, 3, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(3) }
it { "Foo".index(/[A-Z]/, 1).should be_nil }
it { "foo".index(/o/, 2).should eq(2) }
it { "foo".index(//, 3).should eq(3) }
Expand Down Expand Up @@ -1065,6 +1067,7 @@ describe "String" do
describe "by regex" do
it { "string 12345".index!(/\d+/).should eq(7) }
it { "12345".index!(/\d/).should eq(0) }
it { "Hello\xFF".index!(/l/, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(2) }
it do
expect_raises(Enumerable::NotFoundError) do
"Hello, world!".index!(/\d/)
Expand All @@ -1074,6 +1077,7 @@ describe "String" do
describe "with offset" do
it { "abcDef".index!(/[A-Z]/).should eq(3) }
it { "foobarbaz".index!(/ba/, -5).should eq(6) }
it { "Hello\xFF".index!(/l/, 3, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(3) }
it do
expect_raises(Enumerable::NotFoundError) do
"Foo".index!(/[A-Z]/, 1)
Expand Down Expand Up @@ -1142,6 +1146,7 @@ describe "String" do

describe "by regex" do
it { "bbbb".rindex(/b/).should eq(3) }
it { "\xFFbbb".rindex(/b/, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(3) }
it { "a43b53".rindex(/\d+/).should eq(4) }
it { "bbbb".rindex(/\d/).should be_nil }

Expand All @@ -1153,6 +1158,7 @@ describe "String" do

describe "with offset" do
it { "bbbb".rindex(/b/, 2).should eq(2) }
it { "\xFFbbb".rindex(/b/, 2, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(2) }
it { "abbbb".rindex(/b/, 0).should be_nil }
it { "abbbb".rindex(/a/, 0).should eq(0) }
it { "bbbb".rindex(/b/, -2).should eq(2) }
Expand Down Expand Up @@ -1209,6 +1215,7 @@ describe "String" do

describe "by regex" do
it { "bbbb".rindex!(/b/).should eq(3) }
it { "\xFFbbb".rindex!(/b/, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(3) }
it { "a43b53".rindex!(/\d+/).should eq(4) }
it do
expect_raises(Enumerable::NotFoundError) do
Expand All @@ -1218,6 +1225,7 @@ describe "String" do

describe "with offset" do
it { "bbbb".rindex!(/b/, 2).should eq(2) }
it { "\xFFbbb".rindex!(/b/, 2, options: Regex::MatchOptions::NO_UTF_CHECK).should eq(2) }
it do
expect_raises(Enumerable::NotFoundError) do
"abbbb".rindex!(/b/, 0)
Expand Down
5 changes: 5 additions & 0 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3433,6 +3433,11 @@ class String
index(search, offset) || raise Enumerable::NotFoundError.new
end

# :ditto:
def index!(search : Regex, offset = 0, *, options : Regex::MatchOptions = Regex::MatchOptions::None) : Int32
index(search, offset, options: options) || raise Enumerable::NotFoundError.new
end

# Returns the index of the _last_ appearance of *search* in the string,
# If *offset* is present, it defines the position to _end_ the search
# (characters beyond this point are ignored).
Expand Down

0 comments on commit fc3352c

Please sign in to comment.