@@ -26,11 +26,11 @@ class Regex
26
26
# Returns the number of capture groups, including named capture groups.
27
27
#
28
28
# ```
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
32
32
# ```
33
- getter size : Int32
33
+ getter group_size : Int32
34
34
35
35
# Returns the original string.
36
36
#
@@ -40,7 +40,18 @@ class Regex
40
40
getter string : String
41
41
42
42
# :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
44
55
end
45
56
46
57
# Return the position of the first character of the *n*th match.
@@ -204,7 +215,7 @@ class Regex
204
215
name_table = @regex .name_table
205
216
206
217
caps = [] of String ?
207
- (1 ..size).each do |i |
218
+ (1... size ).each do |i |
208
219
caps << self [i]? unless name_table.has_key? i
209
220
end
210
221
@@ -226,7 +237,7 @@ class Regex
226
237
name_table = @regex .name_table
227
238
228
239
caps = {} of String => String ?
229
- (1 ..size).each do |i |
240
+ (1... size ).each do |i |
230
241
if name = name_table[i]?
231
242
caps[name] = self [i]?
232
243
end
@@ -247,7 +258,7 @@ class Regex
247
258
# match.to_a # => ["Cr", "Cr", nil]
248
259
# ```
249
260
def to_a
250
- (0 ..size).map { |i | self [i]? }
261
+ (0... size ).map { |i | self [i]? }
251
262
end
252
263
253
264
# Convert this match data into a hash.
@@ -265,7 +276,7 @@ class Regex
265
276
name_table = @regex .name_table
266
277
267
278
hash = {} of (String | Int32 ) => String ?
268
- (0 ..size).each do |i |
279
+ (0... size ).each do |i |
269
280
hash[name_table.fetch(i) { i }] = self [i]?
270
281
end
271
282
@@ -281,13 +292,12 @@ class Regex
281
292
282
293
io << " #<Regex::MatchData "
283
294
self [0 ].inspect(io)
284
- if size > 0
295
+ if size > 1
285
296
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 }
289
299
io << " :"
290
- self [i + 1 ]?.inspect(io)
300
+ self [i]?.inspect(io)
291
301
end
292
302
end
293
303
io << " >"
@@ -306,15 +316,15 @@ class Regex
306
316
return false unless regex == other.regex
307
317
return false unless string == other.string
308
318
309
- return @ovector .memcmp(other.@ovector , ( size + 1 ) * 2 ) == 0
319
+ return @ovector .memcmp(other.@ovector , size * 2 ) == 0
310
320
end
311
321
312
322
private def check_index_out_of_bounds (index )
313
323
raise_invalid_group_index(index) unless valid_group?(index)
314
324
end
315
325
316
326
private def valid_group? (index )
317
- index <= @ size
327
+ index < size
318
328
end
319
329
320
330
private def raise_invalid_group_index (index )
0 commit comments