Skip to content

Commit be85fa4

Browse files
makenowjustasterite
authored andcommittedJun 14, 2017
MatchData: fixed #4565: rename size to group_size and fix size behavior
1 parent 526837e commit be85fa4

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed
 

‎spec/std/match_data_spec.cr

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ describe "Regex::MatchData" do
1313
/fox/.match("the fox").to_s.should eq(%(#<Regex::MatchData "fox">))
1414
end
1515

16+
it "does size" do
17+
"Crystal".match(/[p-s]/).not_nil!.size.should eq(1)
18+
"Crystal".match(/r(ys)/).not_nil!.size.should eq(2)
19+
"Crystal".match(/r(ys)(?<ok>ta)/).not_nil!.size.should eq(3)
20+
end
21+
1622
describe "#[]" do
1723
it "captures empty group" do
1824
("foo" =~ /(?<g1>z?)foo/).should eq(0)

‎spec/std/regex_spec.cr

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe "Regex" do
88

99
it "does =~" do
1010
(/foo/ =~ "bar foo baz").should eq(4)
11-
$~.size.should eq(0)
11+
$~.group_size.should eq(0)
1212
end
1313

1414
it "does inspect" do
@@ -64,7 +64,7 @@ describe "Regex" do
6464

6565
it "matches with =~ and captures" do
6666
("fooba" =~ /f(o+)(bar?)/).should eq(0)
67-
$~.size.should eq(2)
67+
$~.group_size.should eq(2)
6868
$1.should eq("oo")
6969
$2.should eq("ba")
7070
end
@@ -77,7 +77,7 @@ describe "Regex" do
7777
it "matches with === and captures" do
7878
"foo" =~ /foo/
7979
(/f(o+)(bar?)/ === "fooba").should be_true
80-
$~.size.should eq(2)
80+
$~.group_size.should eq(2)
8181
$1.should eq("oo")
8282
$2.should eq("ba")
8383
end

‎spec/std/string_spec.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ describe "String" do
17791779

17801780
it "matches empty string" do
17811781
match = "".match(/.*/).not_nil!
1782-
match.size.should eq(0)
1782+
match.group_size.should eq(0)
17831783
match[0].should eq("")
17841784
end
17851785

‎src/regex/match_data.cr

+26-16
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ class Regex
2626
# Returns the number of capture groups, including named capture groups.
2727
#
2828
# ```
29-
# "Crystal".match(/[p-s]/).not_nil!.size # => 0
30-
# "Crystal".match(/r(ys)/).not_nil!.size # => 1
31-
# "Crystal".match(/r(ys)(?<ok>ta)/).not_nil!.size # => 2
29+
# "Crystal".match(/[p-s]/).not_nil!.group_size # => 0
30+
# "Crystal".match(/r(ys)/).not_nil!.group_size # => 1
31+
# "Crystal".match(/r(ys)(?<ok>ta)/).not_nil!.group_size # => 2
3232
# ```
33-
getter size : Int32
33+
getter group_size : Int32
3434

3535
# Returns the original string.
3636
#
@@ -40,7 +40,18 @@ class Regex
4040
getter string : String
4141

4242
# :nodoc:
43-
def initialize(@regex : Regex, @code : LibPCRE::Pcre, @string : String, @pos : Int32, @ovector : Int32*, @size : Int32)
43+
def initialize(@regex : Regex, @code : LibPCRE::Pcre, @string : String, @pos : Int32, @ovector : Int32*, @group_size : Int32)
44+
end
45+
46+
# Returns the number of elements in this match object.
47+
#
48+
# ```
49+
# "Crystal".match(/[p-s]/).not_nil!.size # => 1
50+
# "Crystal".match(/r(ys)/).not_nil!.size # => 2
51+
# "Crystal".match(/r(ys)(?<ok>ta)/).not_nil!.size # => 3
52+
# ```
53+
def size
54+
group_size + 1
4455
end
4556

4657
# Return the position of the first character of the *n*th match.
@@ -204,7 +215,7 @@ class Regex
204215
name_table = @regex.name_table
205216

206217
caps = [] of String?
207-
(1..size).each do |i|
218+
(1...size).each do |i|
208219
caps << self[i]? unless name_table.has_key? i
209220
end
210221

@@ -226,7 +237,7 @@ class Regex
226237
name_table = @regex.name_table
227238

228239
caps = {} of String => String?
229-
(1..size).each do |i|
240+
(1...size).each do |i|
230241
if name = name_table[i]?
231242
caps[name] = self[i]?
232243
end
@@ -247,7 +258,7 @@ class Regex
247258
# match.to_a # => ["Cr", "Cr", nil]
248259
# ```
249260
def to_a
250-
(0..size).map { |i| self[i]? }
261+
(0...size).map { |i| self[i]? }
251262
end
252263

253264
# Convert this match data into a hash.
@@ -265,7 +276,7 @@ class Regex
265276
name_table = @regex.name_table
266277

267278
hash = {} of (String | Int32) => String?
268-
(0..size).each do |i|
279+
(0...size).each do |i|
269280
hash[name_table.fetch(i) { i }] = self[i]?
270281
end
271282

@@ -281,13 +292,12 @@ class Regex
281292

282293
io << "#<Regex::MatchData "
283294
self[0].inspect(io)
284-
if size > 0
295+
if size > 1
285296
io << " "
286-
size.times do |i|
287-
io << " " if i > 0
288-
io << name_table.fetch(i + 1) { i + 1 }
297+
(1...size).join " ", io do |i|
298+
io << name_table.fetch(i) { i }
289299
io << ":"
290-
self[i + 1]?.inspect(io)
300+
self[i]?.inspect(io)
291301
end
292302
end
293303
io << ">"
@@ -306,15 +316,15 @@ class Regex
306316
return false unless regex == other.regex
307317
return false unless string == other.string
308318

309-
return @ovector.memcmp(other.@ovector, (size + 1) * 2) == 0
319+
return @ovector.memcmp(other.@ovector, size * 2) == 0
310320
end
311321

312322
private def check_index_out_of_bounds(index)
313323
raise_invalid_group_index(index) unless valid_group?(index)
314324
end
315325

316326
private def valid_group?(index)
317-
index <= @size
327+
index < size
318328
end
319329

320330
private def raise_invalid_group_index(index)

0 commit comments

Comments
 (0)
Please sign in to comment.