Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
102 changes: 90 additions & 12 deletions spec/std/regex/match_data_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,33 @@ describe "Regex::MatchData" do
end

it "with capture" do
matchdata(/f(o)o/, "foo").begin.should eq 0
matchdata(/f(o)o/, "foo").begin(1).should eq 1
matchdata(/f(o)o/, "foo").begin(-1).should eq 1
matchdata(/f(o)o/, ".foo.").begin.should eq 1
matchdata(/f(o)o/, ".foo.").begin(1).should eq 2
matchdata(/f(o)o/, ".foo.").begin(-1).should eq 2
md = matchdata(/f(o)o/, "foo")
md.begin.should eq 0
md.begin(1).should eq 1
md.begin(-1).should eq 1

md = matchdata(/f(o)o/, ".foo.")
md.begin.should eq 1
md.begin(1).should eq 2
md.begin(-1).should eq 2
end

it "with unmatched capture" do
md = matchdata(/f(x)?o/, "foo")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.begin(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.begin(-1)
end

md = matchdata(/f(x)?o/, ".foo.")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.begin(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.begin(-1)
end
end

it "char index" do
Expand All @@ -82,6 +103,24 @@ describe "Regex::MatchData" do
it "char index" do
matchdata(/foo/, "öfoo").byte_begin.should eq 2
end

it "with unmatched capture" do
md = matchdata(/f(x)?o/, "foo")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_begin(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_begin(-1)
end

md = matchdata(/f(x)?o/, ".foo.")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_begin(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_begin(-1)
end
end
end

describe "#end" do
Expand All @@ -99,12 +138,33 @@ describe "Regex::MatchData" do
end

it "with capture" do
matchdata(/f(o)o/, "foo").end.should eq 3
matchdata(/f(o)o/, "foo").end(1).should eq 2
matchdata(/f(o)o/, "foo").end(-1).should eq 2
matchdata(/f(o)o/, ".foo.").end.should eq 4
matchdata(/f(o)o/, ".foo.").end(1).should eq 3
matchdata(/f(o)o/, ".foo.").end(-1).should eq 3
md = matchdata(/f(o)o/, "foo")
md.end.should eq 3
md.end(1).should eq 2
md.end(-1).should eq 2

md = matchdata(/f(o)o/, ".foo.")
md.end.should eq 4
md.end(1).should eq 3
md.end(-1).should eq 3
end

it "with unmatched capture" do
md = matchdata(/f(x)?o/, "foo")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.end(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.end(-1)
end

md = matchdata(/f(x)?o/, ".foo.")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.end(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.end(-1)
end
end

it "char index" do
Expand All @@ -116,6 +176,24 @@ describe "Regex::MatchData" do
it "char index" do
matchdata(/foo/, "öfoo").byte_end.should eq 5
end

it "with unmatched capture" do
md = matchdata(/f(x)?o/, "foo")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_end(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_end(-1)
end

md = matchdata(/f(x)?o/, ".foo.")
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_end(1)
end
expect_raises(IndexError, "Capture group 1 was not matched") do
md.byte_end(-1)
end
end
end

describe "#[]" do
Expand Down
28 changes: 26 additions & 2 deletions src/regex/match_data.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ class Regex
# When *n* is `0` or not given, uses the match of the entire `Regex`.
# Otherwise, uses the match of the *n*th capture group.
#
# Raises `IndexError` if the index is out of range or the respective
# subpattern is unused.
#
# ```
# "Crystal".match(/r/).not_nil!.begin(0) # => 1
# "Crystal".match(/r(ys)/).not_nil!.begin(1) # => 2
# "クリスタル".match(/リ(ス)/).not_nil!.begin(0) # => 1
# "Crystal".match(/r/).not_nil!.begin(1) # IndexError: Invalid capture group index: 1
# "Crystal".match(/r(x)?/).not_nil!.begin(1) # IndexError: Capture group 1 was not matched
# ```
def begin(n = 0) : Int32
@string.byte_index_to_char_index(byte_begin(n)).not_nil!
Expand All @@ -73,10 +78,15 @@ class Regex
# When *n* is `0` or not given, uses the match of the entire `Regex`.
# Otherwise, uses the match of the *n*th capture group.
#
# Raises `IndexError` if the index is out of range or the respective
# subpattern is unused.
#
# ```
# "Crystal".match(/r/).not_nil!.end(0) # => 2
# "Crystal".match(/r(ys)/).not_nil!.end(1) # => 4
# "クリスタル".match(/リ(ス)/).not_nil!.end(0) # => 3
# "Crystal".match(/r/).not_nil!.end(1) # IndexError: Invalid capture group index: 1
# "Crystal".match(/r(x)?/).not_nil!.end(1) # IndexError: Capture group 1 was not matched
# ```
def end(n = 0) : Int32
@string.byte_index_to_char_index(byte_end(n)).not_nil!
Expand All @@ -87,31 +97,45 @@ class Regex
# When *n* is `0` or not given, uses the match of the entire `Regex`.
# Otherwise, uses the match of the *n*th capture group.
#
# Raises `IndexError` if the index is out of range or the respective
# subpattern is unused.
#
# ```
# "Crystal".match(/r/).not_nil!.byte_begin(0) # => 1
# "Crystal".match(/r(ys)/).not_nil!.byte_begin(1) # => 2
# "クリスタル".match(/リ(ス)/).not_nil!.byte_begin(0) # => 3
# "Crystal".match(/r/).not_nil!.byte_begin(1) # IndexError: Invalid capture group index: 1
# "Crystal".match(/r(x)?/).not_nil!.byte_begin(1) # IndexError: Capture group 1 was not matched
# ```
def byte_begin(n = 0) : Int32
check_index_out_of_bounds n
n += size if n < 0
@ovector[n * 2]
value = @ovector[n * 2]
raise_capture_group_was_not_matched(n) if value < 0
value
end

# Returns the position of the next byte after the match.
#
# When *n* is `0` or not given, uses the match of the entire `Regex`.
# Otherwise, uses the match of the *n*th capture group.
#
# Raises `IndexError` if the index is out of range or the respective
# subpattern is unused.
#
# ```
# "Crystal".match(/r/).not_nil!.byte_end(0) # => 2
# "Crystal".match(/r(ys)/).not_nil!.byte_end(1) # => 4
# "クリスタル".match(/リ(ス)/).not_nil!.byte_end(0) # => 9
# "Crystal".match(/r/).not_nil!.byte_end(1) # IndexError: Invalid capture group index: 1
# "Crystal".match(/r(x)?/).not_nil!.byte_end(1) # IndexError: Capture group 1 was not matched
# ```
def byte_end(n = 0) : Int32
check_index_out_of_bounds n
n += size if n < 0
@ovector[n * 2 + 1]
value = @ovector[n * 2 + 1]
raise_capture_group_was_not_matched(n) if value < 0
value
end

# Returns the match of the *n*th capture group, or `nil` if there isn't
Expand Down