re2 2.23.0-aarch64-linux-gnu → 2.25.0-aarch64-linux-gnu
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +107 -4
- data/dependencies.yml +2 -2
- data/ext/re2/extconf.rb +1 -1
- data/ext/re2/re2.cc +587 -155
- data/lib/3.1/re2.so +0 -0
- data/lib/3.2/re2.so +0 -0
- data/lib/3.3/re2.so +0 -0
- data/lib/3.4/re2.so +0 -0
- data/lib/4.0/re2.so +0 -0
- data/lib/re2/string.rb +6 -6
- data/lib/re2/version.rb +1 -1
- data/spec/re2/match_data_spec.rb +409 -0
- data/spec/re2/regexp_spec.rb +233 -1
- data/spec/re2/scanner_spec.rb +66 -0
- data/spec/re2/set_spec.rb +36 -0
- data/spec/re2_spec.rb +145 -43
- metadata +1 -1
data/lib/3.1/re2.so
CHANGED
|
Binary file
|
data/lib/3.2/re2.so
CHANGED
|
Binary file
|
data/lib/3.3/re2.so
CHANGED
|
Binary file
|
data/lib/3.4/re2.so
CHANGED
|
Binary file
|
data/lib/4.0/re2.so
CHANGED
|
Binary file
|
data/lib/re2/string.rb
CHANGED
|
@@ -13,14 +13,14 @@ require "re2"
|
|
|
13
13
|
module RE2
|
|
14
14
|
# @deprecated Use methods on {RE2} and {RE2::Regexp} instead.
|
|
15
15
|
module String
|
|
16
|
-
# @deprecated Use {RE2.
|
|
16
|
+
# @deprecated Use {RE2.replace} instead.
|
|
17
17
|
def re2_sub(*args)
|
|
18
|
-
RE2.
|
|
18
|
+
RE2.replace(self, *args)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
# @deprecated Use {RE2.
|
|
21
|
+
# @deprecated Use {RE2.global_replace} instead.
|
|
22
22
|
def re2_gsub(*args)
|
|
23
|
-
RE2.
|
|
23
|
+
RE2.global_replace(self, *args)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
# @deprecated Use {RE2::Regexp#match} instead.
|
|
@@ -28,9 +28,9 @@ module RE2
|
|
|
28
28
|
RE2::Regexp.new(pattern).match(self, *args)
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
# @deprecated Use {RE2.
|
|
31
|
+
# @deprecated Use {RE2.escape} instead.
|
|
32
32
|
def re2_escape
|
|
33
|
-
RE2.
|
|
33
|
+
RE2.escape(self)
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
alias_method :re2_quote, :re2_escape
|
data/lib/re2/version.rb
CHANGED
data/spec/re2/match_data_spec.rb
CHANGED
|
@@ -10,6 +10,55 @@ RSpec.describe RE2::MatchData do
|
|
|
10
10
|
expect(ObjectSpace.memsize_of(matches1)).to be < ObjectSpace.memsize_of(matches2)
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
+
describe "#dup" do
|
|
14
|
+
it "returns a copy of the match data" do
|
|
15
|
+
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456")
|
|
16
|
+
copy = md.dup
|
|
17
|
+
|
|
18
|
+
expect(copy.to_a).to eq(["123 456", "123", "456"])
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "returns a different object" do
|
|
22
|
+
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456")
|
|
23
|
+
copy = md.dup
|
|
24
|
+
|
|
25
|
+
expect(copy).to_not equal(md)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it "preserves the original string" do
|
|
29
|
+
str = "bob 123"
|
|
30
|
+
md = RE2::Regexp.new('(\d+)').match(str)
|
|
31
|
+
copy = md.dup
|
|
32
|
+
|
|
33
|
+
expect(copy.string).to equal(str)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "preserves the original regexp" do
|
|
37
|
+
re = RE2::Regexp.new('(\d+)')
|
|
38
|
+
md = re.match("123")
|
|
39
|
+
copy = md.dup
|
|
40
|
+
|
|
41
|
+
expect(copy.regexp).to equal(re)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "raises an error when called on an uninitialized object" do
|
|
45
|
+
expect { described_class.allocate.dup }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe "#clone" do
|
|
50
|
+
it "returns a copy of the match data" do
|
|
51
|
+
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456")
|
|
52
|
+
copy = md.clone
|
|
53
|
+
|
|
54
|
+
expect(copy.to_a).to eq(["123 456", "123", "456"])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "raises an error when called on an uninitialized object" do
|
|
58
|
+
expect { described_class.allocate.clone }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
13
62
|
describe "#to_a" do
|
|
14
63
|
it "is populated with the match and capturing groups" do
|
|
15
64
|
a = RE2::Regexp.new('w(o)(o)').match('woo').to_a
|
|
@@ -34,6 +83,10 @@ RSpec.describe RE2::MatchData do
|
|
|
34
83
|
|
|
35
84
|
expect(a.map(&:encoding)).to all eq(Encoding::ISO_8859_1)
|
|
36
85
|
end
|
|
86
|
+
|
|
87
|
+
it "raises an error when called on an uninitialized object" do
|
|
88
|
+
expect { described_class.allocate.to_a }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
89
|
+
end
|
|
37
90
|
end
|
|
38
91
|
|
|
39
92
|
describe "#[]" do
|
|
@@ -140,6 +193,10 @@ RSpec.describe RE2::MatchData do
|
|
|
140
193
|
|
|
141
194
|
expect(md[1]).to eq("woo")
|
|
142
195
|
end
|
|
196
|
+
|
|
197
|
+
it "raises an error when called on an uninitialized object" do
|
|
198
|
+
expect { described_class.allocate[0] }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
199
|
+
end
|
|
143
200
|
end
|
|
144
201
|
|
|
145
202
|
describe "#string" do
|
|
@@ -168,6 +225,10 @@ RSpec.describe RE2::MatchData do
|
|
|
168
225
|
|
|
169
226
|
expect(re.string).to equal(string)
|
|
170
227
|
end
|
|
228
|
+
|
|
229
|
+
it "raises an error when called on an uninitialized object" do
|
|
230
|
+
expect { described_class.allocate.string }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
231
|
+
end
|
|
171
232
|
end
|
|
172
233
|
|
|
173
234
|
describe "#size" do
|
|
@@ -176,6 +237,10 @@ RSpec.describe RE2::MatchData do
|
|
|
176
237
|
|
|
177
238
|
expect(md.size).to eq(3)
|
|
178
239
|
end
|
|
240
|
+
|
|
241
|
+
it "raises an error when called on an uninitialized object" do
|
|
242
|
+
expect { described_class.allocate.size }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
243
|
+
end
|
|
179
244
|
end
|
|
180
245
|
|
|
181
246
|
describe "#length" do
|
|
@@ -184,6 +249,10 @@ RSpec.describe RE2::MatchData do
|
|
|
184
249
|
|
|
185
250
|
expect(md.length).to eq(3)
|
|
186
251
|
end
|
|
252
|
+
|
|
253
|
+
it "raises an error when called on an uninitialized object" do
|
|
254
|
+
expect { described_class.allocate.length }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
255
|
+
end
|
|
187
256
|
end
|
|
188
257
|
|
|
189
258
|
describe "#regexp" do
|
|
@@ -193,6 +262,10 @@ RSpec.describe RE2::MatchData do
|
|
|
193
262
|
|
|
194
263
|
expect(md.regexp).to equal(re)
|
|
195
264
|
end
|
|
265
|
+
|
|
266
|
+
it "raises an error when called on an uninitialized object" do
|
|
267
|
+
expect { described_class.allocate.regexp }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
268
|
+
end
|
|
196
269
|
end
|
|
197
270
|
|
|
198
271
|
describe "#inspect" do
|
|
@@ -213,6 +286,10 @@ RSpec.describe RE2::MatchData do
|
|
|
213
286
|
|
|
214
287
|
expect(md.inspect).to eq("#<RE2::MatchData \"a\0b c\0d\" 1:\"a\0b\" 2:\"c\0d\">")
|
|
215
288
|
end
|
|
289
|
+
|
|
290
|
+
it "raises an error when called on an uninitialized object" do
|
|
291
|
+
expect { described_class.allocate.inspect }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
292
|
+
end
|
|
216
293
|
end
|
|
217
294
|
|
|
218
295
|
describe "#to_s" do
|
|
@@ -221,6 +298,10 @@ RSpec.describe RE2::MatchData do
|
|
|
221
298
|
|
|
222
299
|
expect(md.to_s).to eq("23456")
|
|
223
300
|
end
|
|
301
|
+
|
|
302
|
+
it "raises an error when called on an uninitialized object" do
|
|
303
|
+
expect { described_class.allocate.to_s }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
304
|
+
end
|
|
224
305
|
end
|
|
225
306
|
|
|
226
307
|
describe "#to_ary" do
|
|
@@ -301,6 +382,10 @@ RSpec.describe RE2::MatchData do
|
|
|
301
382
|
|
|
302
383
|
expect(md.string[md.begin(0)..-1]).to eq('woohoo' * 5)
|
|
303
384
|
end
|
|
385
|
+
|
|
386
|
+
it "raises an error when called on an uninitialized object" do
|
|
387
|
+
expect { described_class.allocate.begin(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
388
|
+
end
|
|
304
389
|
end
|
|
305
390
|
|
|
306
391
|
describe "#end" do
|
|
@@ -370,6 +455,238 @@ RSpec.describe RE2::MatchData do
|
|
|
370
455
|
|
|
371
456
|
expect(md.string[0...md.end(0)]).to eq('woo')
|
|
372
457
|
end
|
|
458
|
+
|
|
459
|
+
it "raises an error when called on an uninitialized object" do
|
|
460
|
+
expect { described_class.allocate.end(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
461
|
+
end
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
describe "#pre_match" do
|
|
465
|
+
it "returns the portion of the string before the match" do
|
|
466
|
+
md = RE2::Regexp.new('(\d+)').match("bob 123 456")
|
|
467
|
+
|
|
468
|
+
expect(md.pre_match).to eq("bob ")
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
it "returns an empty string when the match starts at the beginning" do
|
|
472
|
+
md = RE2::Regexp.new('(\w+)').match("bob 123")
|
|
473
|
+
|
|
474
|
+
expect(md.pre_match).to eq("")
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
it "supports multibyte characters" do
|
|
478
|
+
md = RE2::Regexp.new('(\d+)').match("I ♥ 123")
|
|
479
|
+
|
|
480
|
+
expect(md.pre_match).to eq("I ♥ ")
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
it "returns UTF-8 strings by default" do
|
|
484
|
+
md = RE2::Regexp.new('(\d+)').match("abc 123")
|
|
485
|
+
|
|
486
|
+
expect(md.pre_match.encoding).to eq(Encoding::UTF_8)
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do
|
|
490
|
+
md = RE2::Regexp.new('(\d+)', utf8: false).match("abc 123")
|
|
491
|
+
|
|
492
|
+
expect(md.pre_match.encoding).to eq(Encoding::ISO_8859_1)
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
it "raises an error when called on an uninitialized object" do
|
|
496
|
+
expect { described_class.allocate.pre_match }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
497
|
+
end
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
describe "#post_match" do
|
|
501
|
+
it "returns the portion of the string after the match" do
|
|
502
|
+
md = RE2::Regexp.new('(\d+)').match("bob 123 456")
|
|
503
|
+
|
|
504
|
+
expect(md.post_match).to eq(" 456")
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
it "returns an empty string when the match ends at the end" do
|
|
508
|
+
md = RE2::Regexp.new('(\d+)$').match("bob 123")
|
|
509
|
+
|
|
510
|
+
expect(md.post_match).to eq("")
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
it "supports multibyte characters" do
|
|
514
|
+
md = RE2::Regexp.new('(\d+)').match("123 ♥ world")
|
|
515
|
+
|
|
516
|
+
expect(md.post_match).to eq(" ♥ world")
|
|
517
|
+
end
|
|
518
|
+
|
|
519
|
+
it "returns UTF-8 strings by default" do
|
|
520
|
+
md = RE2::Regexp.new('(\d+)').match("abc 123 def")
|
|
521
|
+
|
|
522
|
+
expect(md.post_match.encoding).to eq(Encoding::UTF_8)
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do
|
|
526
|
+
md = RE2::Regexp.new('(\d+)', utf8: false).match("abc 123 def")
|
|
527
|
+
|
|
528
|
+
expect(md.post_match.encoding).to eq(Encoding::ISO_8859_1)
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
it "raises an error when called on an uninitialized object" do
|
|
532
|
+
expect { described_class.allocate.post_match }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
533
|
+
end
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
describe "#offset" do
|
|
537
|
+
it "returns the offset of a match by index" do
|
|
538
|
+
md = RE2::Regexp.new('ob (\d+)').match("bob 123")
|
|
539
|
+
|
|
540
|
+
expect(md.offset(0)).to eq([1, 7])
|
|
541
|
+
end
|
|
542
|
+
|
|
543
|
+
it "returns the offset of a submatch by index" do
|
|
544
|
+
md = RE2::Regexp.new('ob (\d+)').match("bob 123")
|
|
545
|
+
|
|
546
|
+
expect(md.offset(1)).to eq([4, 7])
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
it "returns the offset of a match by string name" do
|
|
550
|
+
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123")
|
|
551
|
+
|
|
552
|
+
expect(md.offset("number")).to eq([4, 7])
|
|
553
|
+
end
|
|
554
|
+
|
|
555
|
+
it "returns the offset of a match by symbol name" do
|
|
556
|
+
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123")
|
|
557
|
+
|
|
558
|
+
expect(md.offset(:number)).to eq([4, 7])
|
|
559
|
+
end
|
|
560
|
+
|
|
561
|
+
it "returns character offsets despite multibyte characters" do
|
|
562
|
+
md = RE2::Regexp.new('(Ruby)').match("I ♥ Ruby")
|
|
563
|
+
|
|
564
|
+
expect(md.offset(0)).to eq([4, 8])
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
it "returns nil for non-existent numerical matches" do
|
|
568
|
+
md = RE2::Regexp.new('(\d)').match("123")
|
|
569
|
+
|
|
570
|
+
expect(md.offset(10)).to be_nil
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
it "returns nil for non-existent named matches" do
|
|
574
|
+
md = RE2::Regexp.new('(\d)').match("123")
|
|
575
|
+
|
|
576
|
+
expect(md.offset("foo")).to be_nil
|
|
577
|
+
end
|
|
578
|
+
|
|
579
|
+
it "raises a type error if given an invalid name or number" do
|
|
580
|
+
md = RE2::Regexp.new('(\d)').match("123")
|
|
581
|
+
|
|
582
|
+
expect { md.offset(nil) }.to raise_error(TypeError)
|
|
583
|
+
end
|
|
584
|
+
|
|
585
|
+
it "raises an error when called on an uninitialized object" do
|
|
586
|
+
expect { described_class.allocate.offset(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
587
|
+
end
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
describe "#match_length" do
|
|
591
|
+
it "returns the length of the overall match" do
|
|
592
|
+
md = RE2::Regexp.new('ob (\d+)').match("bob 123")
|
|
593
|
+
|
|
594
|
+
expect(md.match_length(0)).to eq(6)
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
it "returns the length of a submatch by index" do
|
|
598
|
+
md = RE2::Regexp.new('ob (\d+)').match("bob 123")
|
|
599
|
+
|
|
600
|
+
expect(md.match_length(1)).to eq(3)
|
|
601
|
+
end
|
|
602
|
+
|
|
603
|
+
it "returns the length of a match by string name" do
|
|
604
|
+
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123")
|
|
605
|
+
|
|
606
|
+
expect(md.match_length("number")).to eq(3)
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
it "returns the length of a match by symbol name" do
|
|
610
|
+
md = RE2::Regexp.new('(?P<number>\d+)').match("bob 123")
|
|
611
|
+
|
|
612
|
+
expect(md.match_length(:number)).to eq(3)
|
|
613
|
+
end
|
|
614
|
+
|
|
615
|
+
it "returns character length despite multibyte characters" do
|
|
616
|
+
md = RE2::Regexp.new('(♥ Ruby)').match("I ♥ Ruby!")
|
|
617
|
+
|
|
618
|
+
expect(md.match_length(0)).to eq(6)
|
|
619
|
+
end
|
|
620
|
+
|
|
621
|
+
it "returns nil for non-existent numerical matches" do
|
|
622
|
+
md = RE2::Regexp.new('(\d)').match("123")
|
|
623
|
+
|
|
624
|
+
expect(md.match_length(10)).to be_nil
|
|
625
|
+
end
|
|
626
|
+
|
|
627
|
+
it "returns nil for non-existent named matches" do
|
|
628
|
+
md = RE2::Regexp.new('(\d)').match("123")
|
|
629
|
+
|
|
630
|
+
expect(md.match_length("foo")).to be_nil
|
|
631
|
+
end
|
|
632
|
+
|
|
633
|
+
it "raises a type error if given an invalid name or number" do
|
|
634
|
+
md = RE2::Regexp.new('(\d)').match("123")
|
|
635
|
+
|
|
636
|
+
expect { md.match_length(nil) }.to raise_error(TypeError)
|
|
637
|
+
end
|
|
638
|
+
|
|
639
|
+
it "raises an error when called on an uninitialized object" do
|
|
640
|
+
expect { described_class.allocate.match_length(0) }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
641
|
+
end
|
|
642
|
+
end
|
|
643
|
+
|
|
644
|
+
describe "#values_at" do
|
|
645
|
+
it "returns match values at the given indices" do
|
|
646
|
+
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789")
|
|
647
|
+
|
|
648
|
+
expect(md.values_at(1, 3)).to eq(["123", "789"])
|
|
649
|
+
end
|
|
650
|
+
|
|
651
|
+
it "returns match values by named groups" do
|
|
652
|
+
md = RE2::Regexp.new('(?P<a>\d+) (?P<b>\d+)').match("123 456")
|
|
653
|
+
|
|
654
|
+
expect(md.values_at(:a, :b)).to eq(["123", "456"])
|
|
655
|
+
end
|
|
656
|
+
|
|
657
|
+
it "supports a mix of indices and names" do
|
|
658
|
+
md = RE2::Regexp.new('(?P<a>\d+) (\d+)').match("123 456")
|
|
659
|
+
|
|
660
|
+
expect(md.values_at(2, :a)).to eq(["456", "123"])
|
|
661
|
+
end
|
|
662
|
+
|
|
663
|
+
it "returns nil for non-existent indices" do
|
|
664
|
+
md = RE2::Regexp.new('(\d+)').match("123")
|
|
665
|
+
|
|
666
|
+
expect(md.values_at(1, 5)).to eq(["123", nil])
|
|
667
|
+
end
|
|
668
|
+
|
|
669
|
+
it "returns nil for non-existent names" do
|
|
670
|
+
md = RE2::Regexp.new('(?P<a>\d+)').match("123")
|
|
671
|
+
|
|
672
|
+
expect(md.values_at(:a, :z)).to eq(["123", nil])
|
|
673
|
+
end
|
|
674
|
+
|
|
675
|
+
it "returns the full match when given index 0" do
|
|
676
|
+
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456")
|
|
677
|
+
|
|
678
|
+
expect(md.values_at(0, 1)).to eq(["123 456", "123"])
|
|
679
|
+
end
|
|
680
|
+
|
|
681
|
+
it "returns an empty array if given no arguments" do
|
|
682
|
+
md = RE2::Regexp.new('(\d+) (\d+)').match("123 456")
|
|
683
|
+
|
|
684
|
+
expect(md.values_at).to be_empty
|
|
685
|
+
end
|
|
686
|
+
|
|
687
|
+
it "raises an error when called on an uninitialized object" do
|
|
688
|
+
expect { described_class.allocate.values_at(1) }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
689
|
+
end
|
|
373
690
|
end
|
|
374
691
|
|
|
375
692
|
describe "#deconstruct" do
|
|
@@ -384,6 +701,94 @@ RSpec.describe RE2::MatchData do
|
|
|
384
701
|
|
|
385
702
|
expect(md.deconstruct).to eq(['o', 'o', nil])
|
|
386
703
|
end
|
|
704
|
+
|
|
705
|
+
it "raises an error when called on an uninitialized object" do
|
|
706
|
+
expect { described_class.allocate.deconstruct }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
707
|
+
end
|
|
708
|
+
end
|
|
709
|
+
|
|
710
|
+
describe "#captures" do
|
|
711
|
+
it "returns all capturing groups" do
|
|
712
|
+
md = RE2::Regexp.new('w(o)(o)').match('woo')
|
|
713
|
+
|
|
714
|
+
expect(md.captures).to eq(['o', 'o'])
|
|
715
|
+
end
|
|
716
|
+
|
|
717
|
+
it "includes optional capturing groups as nil" do
|
|
718
|
+
md = RE2::Regexp.new('w(.)(.)(.)?').match('woo')
|
|
719
|
+
|
|
720
|
+
expect(md.captures).to eq(['o', 'o', nil])
|
|
721
|
+
end
|
|
722
|
+
|
|
723
|
+
it "raises an error when called on an uninitialized object" do
|
|
724
|
+
expect { described_class.allocate.captures }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
725
|
+
end
|
|
726
|
+
end
|
|
727
|
+
|
|
728
|
+
describe "#named_captures" do
|
|
729
|
+
it "returns a hash of capturing group names to matched strings" do
|
|
730
|
+
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
|
|
731
|
+
|
|
732
|
+
expect(md.named_captures).to eq("numbers" => "123", "letters" => "abc")
|
|
733
|
+
end
|
|
734
|
+
|
|
735
|
+
it "returns an empty hash if there are no named capturing groups" do
|
|
736
|
+
md = RE2::Regexp.new('(\d+)').match('123')
|
|
737
|
+
|
|
738
|
+
expect(md.named_captures).to be_empty
|
|
739
|
+
end
|
|
740
|
+
|
|
741
|
+
it "returns unmatched optional groups as nil" do
|
|
742
|
+
md = RE2::Regexp.new('(?P<a>\d+) (?P<b>\w+)?').match('123 ')
|
|
743
|
+
|
|
744
|
+
expect(md.named_captures).to eq("a" => "123", "b" => nil)
|
|
745
|
+
end
|
|
746
|
+
|
|
747
|
+
it "returns symbol keys when symbolize_names: true" do
|
|
748
|
+
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
|
|
749
|
+
|
|
750
|
+
expect(md.named_captures(symbolize_names: true)).to eq(numbers: "123", letters: "abc")
|
|
751
|
+
end
|
|
752
|
+
|
|
753
|
+
it "returns string keys when symbolize_names: false" do
|
|
754
|
+
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
|
|
755
|
+
|
|
756
|
+
expect(md.named_captures(symbolize_names: false)).to eq("numbers" => "123", "letters" => "abc")
|
|
757
|
+
end
|
|
758
|
+
|
|
759
|
+
it "raises an error when called on an uninitialized object" do
|
|
760
|
+
expect { described_class.allocate.named_captures }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
761
|
+
end
|
|
762
|
+
end
|
|
763
|
+
|
|
764
|
+
describe "#names" do
|
|
765
|
+
it "returns an array of names of named capturing groups" do
|
|
766
|
+
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
|
|
767
|
+
|
|
768
|
+
expect(md.names).to eq(["letters", "numbers"])
|
|
769
|
+
end
|
|
770
|
+
|
|
771
|
+
it "returns an empty array if there are no named capturing groups" do
|
|
772
|
+
md = RE2::Regexp.new('(\d+)').match('123')
|
|
773
|
+
|
|
774
|
+
expect(md.names).to be_empty
|
|
775
|
+
end
|
|
776
|
+
|
|
777
|
+
it "returns UTF-8 strings if the pattern is UTF-8" do
|
|
778
|
+
md = RE2::Regexp.new('(?P<numbers>\d+)').match('123')
|
|
779
|
+
|
|
780
|
+
expect(md.names.first.encoding).to eq(Encoding::UTF_8)
|
|
781
|
+
end
|
|
782
|
+
|
|
783
|
+
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do
|
|
784
|
+
md = RE2::Regexp.new('(?P<numbers>\d+)', utf8: false).match('123')
|
|
785
|
+
|
|
786
|
+
expect(md.names.first.encoding).to eq(Encoding::ISO_8859_1)
|
|
787
|
+
end
|
|
788
|
+
|
|
789
|
+
it "raises an error when called on an uninitialized object" do
|
|
790
|
+
expect { described_class.allocate.names }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
791
|
+
end
|
|
387
792
|
end
|
|
388
793
|
|
|
389
794
|
describe "#deconstruct_keys" do
|
|
@@ -428,5 +833,9 @@ RSpec.describe RE2::MatchData do
|
|
|
428
833
|
|
|
429
834
|
expect { md.deconstruct_keys([0]) }.to raise_error(TypeError)
|
|
430
835
|
end
|
|
836
|
+
|
|
837
|
+
it "raises an error when called on an uninitialized object" do
|
|
838
|
+
expect { described_class.allocate.deconstruct_keys(nil) }.to raise_error(TypeError, /uninitialized RE2::MatchData/)
|
|
839
|
+
end
|
|
431
840
|
end
|
|
432
841
|
end
|