re2 2.4.3-x86-mingw32 → 2.5.0-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +236 -192
- data/ext/re2/extconf.rb +6 -70
- data/ext/re2/re2.cc +450 -263
- data/ext/re2/recipes.rb +8 -0
- data/lib/2.6/re2.so +0 -0
- data/lib/2.7/re2.so +0 -0
- data/lib/3.0/re2.so +0 -0
- data/lib/3.1/re2.so +0 -0
- data/lib/3.2/re2.so +0 -0
- data/lib/re2/regexp.rb +69 -0
- data/lib/re2/scanner.rb +8 -0
- data/lib/re2/string.rb +9 -59
- data/lib/re2/version.rb +9 -1
- data/lib/re2.rb +7 -3
- data/re2.gemspec +1 -0
- data/spec/kernel_spec.rb +2 -2
- data/spec/re2/match_data_spec.rb +64 -25
- data/spec/re2/regexp_spec.rb +492 -113
- data/spec/re2/scanner_spec.rb +3 -8
- data/spec/re2/set_spec.rb +18 -18
- data/spec/re2_spec.rb +4 -4
- metadata +3 -2
data/ext/re2/recipes.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
# re2 (https://github.com/mudge/re2)
|
2
|
+
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
|
3
|
+
# backtracking regular expression engines like those used in PCRE, Perl, and
|
4
|
+
# Python".
|
5
|
+
#
|
6
|
+
# Copyright (c) 2010, Paul Mucur (https://mudge.name)
|
7
|
+
# Released under the BSD Licence, please see LICENSE.txt
|
8
|
+
|
1
9
|
PACKAGE_ROOT_DIR = File.expand_path('../..', __dir__)
|
2
10
|
REQUIRED_MINI_PORTILE_VERSION = '~> 2.8.5' # keep this version in sync with the one in the gemspec
|
3
11
|
|
data/lib/2.6/re2.so
CHANGED
Binary file
|
data/lib/2.7/re2.so
CHANGED
Binary file
|
data/lib/3.0/re2.so
CHANGED
Binary file
|
data/lib/3.1/re2.so
CHANGED
Binary file
|
data/lib/3.2/re2.so
CHANGED
Binary file
|
data/lib/re2/regexp.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# re2 (https://github.com/mudge/re2)
|
2
|
+
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
|
3
|
+
# backtracking regular expression engines like those used in PCRE, Perl, and
|
4
|
+
# Python".
|
5
|
+
#
|
6
|
+
# Copyright (c) 2010, Paul Mucur (https://mudge.name)
|
7
|
+
# Released under the BSD Licence, please see LICENSE.txt
|
8
|
+
|
9
|
+
module RE2
|
10
|
+
class Regexp
|
11
|
+
# Match the pattern against any substring of the given `text` and return a
|
12
|
+
# {RE2::MatchData} instance with the specified number of submatches
|
13
|
+
# (defaults to the total number of capturing groups) or a boolean (if no
|
14
|
+
# submatches are required).
|
15
|
+
#
|
16
|
+
# The number of submatches has a significant impact on performance: requesting
|
17
|
+
# one submatch is much faster than requesting more than one and requesting
|
18
|
+
# zero submatches is faster still.
|
19
|
+
#
|
20
|
+
# @param [String] text the text to search
|
21
|
+
# @param [Hash] options the options with which to perform the match
|
22
|
+
# @option options [Integer] :submatches how many submatches to extract (0
|
23
|
+
# is fastest), defaults to the total number of capturing groups
|
24
|
+
# @return [RE2::MatchData, nil] if extracting any submatches
|
25
|
+
# @return [Boolean] if not extracting any submatches
|
26
|
+
# @raise [ArgumentError] if given a negative number of submatches
|
27
|
+
# @raise [NoMemoryError] if there was not enough memory to allocate the
|
28
|
+
# matches
|
29
|
+
# @raise [TypeError] if given non-numeric submatches or non-hash options
|
30
|
+
# @example
|
31
|
+
# r = RE2::Regexp.new('w(o)(o)')
|
32
|
+
# r.partial_match('woot') #=> #<RE2::MatchData "woo" 1:"o" 2:"o">
|
33
|
+
# r.partial_match('nope') #=> nil
|
34
|
+
# r.partial_match('woot', submatches: 1) #=> #<RE2::MatchData "woo" 1:"o">
|
35
|
+
# r.partial_match('woot', submatches: 0) #=> true
|
36
|
+
def partial_match(text, options = {})
|
37
|
+
match(text, Hash(options).merge(anchor: :unanchored))
|
38
|
+
end
|
39
|
+
|
40
|
+
# Match the pattern against the given `text` exactly and return a
|
41
|
+
# {RE2::MatchData} instance with the specified number of submatches
|
42
|
+
# (defaults to the total number of capturing groups) or a boolean (if no
|
43
|
+
# submatches are required).
|
44
|
+
#
|
45
|
+
# The number of submatches has a significant impact on performance: requesting
|
46
|
+
# one submatch is much faster than requesting more than one and requesting
|
47
|
+
# zero submatches is faster still.
|
48
|
+
#
|
49
|
+
# @param [String] text the text to search
|
50
|
+
# @param [Hash] options the options with which to perform the match
|
51
|
+
# @option options [Integer] :submatches how many submatches to extract (0
|
52
|
+
# is fastest), defaults to the total number of capturing groups
|
53
|
+
# @return [RE2::MatchData, nil] if extracting any submatches
|
54
|
+
# @return [Boolean] if not extracting any submatches
|
55
|
+
# @raise [ArgumentError] if given a negative number of submatches
|
56
|
+
# @raise [NoMemoryError] if there was not enough memory to allocate the
|
57
|
+
# matches
|
58
|
+
# @raise [TypeError] if given non-numeric submatches or non-hash options
|
59
|
+
# @example
|
60
|
+
# r = RE2::Regexp.new('w(o)(o)')
|
61
|
+
# r.full_match('woo') #=> #<RE2::MatchData "woo" 1:"o" 2:"o">
|
62
|
+
# r.full_match('woot') #=> nil
|
63
|
+
# r.full_match('woo', submatches: 1) #=> #<RE2::MatchData "woo" 1:"o">
|
64
|
+
# r.full_match('woo', submatches: 0) #=> true
|
65
|
+
def full_match(text, options = {})
|
66
|
+
match(text, Hash(options).merge(anchor: :anchor_both))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/re2/scanner.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
# re2 (https://github.com/mudge/re2)
|
2
|
+
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
|
3
|
+
# backtracking regular expression engines like those used in PCRE, Perl, and
|
4
|
+
# Python".
|
5
|
+
#
|
6
|
+
# Copyright (c) 2010, Paul Mucur (https://mudge.name)
|
7
|
+
# Released under the BSD Licence, please see LICENSE.txt
|
8
|
+
|
1
9
|
module RE2
|
2
10
|
class Scanner
|
3
11
|
include Enumerable
|
data/lib/re2/string.rb
CHANGED
@@ -1,81 +1,31 @@
|
|
1
|
-
# re2 (
|
2
|
-
# Ruby bindings to
|
1
|
+
# re2 (https://github.com/mudge/re2)
|
2
|
+
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
|
3
|
+
# backtracking regular expression engines like those used in PCRE, Perl, and
|
4
|
+
# Python".
|
3
5
|
#
|
4
|
-
# Copyright (c) 2010
|
6
|
+
# Copyright (c) 2010, Paul Mucur (https://mudge.name)
|
5
7
|
# Released under the BSD Licence, please see LICENSE.txt
|
6
8
|
|
7
9
|
require "re2"
|
8
10
|
|
9
11
|
module RE2
|
10
12
|
module String
|
11
|
-
|
12
|
-
# Replaces the first occurrence +pattern+ with +rewrite+ and returns a new
|
13
|
-
# string.
|
14
|
-
#
|
15
|
-
# @see RE2.Replace
|
13
|
+
# @deprecated Use {RE2.Replace} instead.
|
16
14
|
def re2_sub(*args)
|
17
15
|
RE2.Replace(self, *args)
|
18
16
|
end
|
19
17
|
|
20
|
-
#
|
21
|
-
#
|
22
|
-
# @see RE2.GlobalReplace
|
18
|
+
# @deprecated Use {RE2.GlobalReplace} instead.
|
23
19
|
def re2_gsub(*args)
|
24
20
|
RE2.GlobalReplace(self, *args)
|
25
21
|
end
|
26
22
|
|
27
|
-
#
|
28
|
-
# or a {RE2::MatchData} instance.
|
29
|
-
#
|
30
|
-
# @return [Boolean, RE2::MatchData]
|
31
|
-
#
|
32
|
-
# @overload match(pattern)
|
33
|
-
# Returns an {RE2::MatchData} containing the matching
|
34
|
-
# pattern and all subpatterns resulting from looking for
|
35
|
-
# +pattern+.
|
36
|
-
#
|
37
|
-
# @param [String, RE2::Regexp] pattern the regular expression to match
|
38
|
-
# @return [RE2::MatchData] the matches
|
39
|
-
# @raise [NoMemoryError] if there was not enough memory to allocate the matches
|
40
|
-
# @example
|
41
|
-
# r = RE2::Regexp.new('w(o)(o)')
|
42
|
-
# "woo".re2_match(r) #=> #<RE2::MatchData "woo" 1:"o" 2:"o">
|
43
|
-
#
|
44
|
-
# @overload match(pattern, 0)
|
45
|
-
# Returns either true or false indicating whether a
|
46
|
-
# successful match was made.
|
47
|
-
#
|
48
|
-
# @param [String, RE2::Regexp] pattern the regular expression to match
|
49
|
-
# @return [Boolean] whether the match was successful
|
50
|
-
# @raise [NoMemoryError] if there was not enough memory to allocate the matches
|
51
|
-
# @example
|
52
|
-
# r = RE2::Regexp.new('w(o)(o)')
|
53
|
-
# "woo".re2_match(0) #=> true
|
54
|
-
# "bob".re2_match(0) #=> false
|
55
|
-
#
|
56
|
-
# @overload match(pattern, number_of_matches)
|
57
|
-
# See +match(pattern)+ but with a specific number of
|
58
|
-
# matches returned (padded with nils if necessary).
|
59
|
-
#
|
60
|
-
# @param [String, RE2::Regexp] pattern the regular expression to match
|
61
|
-
# @param [Integer] number_of_matches the number of matches to return
|
62
|
-
# @return [RE2::MatchData] the matches
|
63
|
-
# @raise [NoMemoryError] if there was not enough memory to allocate the matches
|
64
|
-
# @example
|
65
|
-
# r = RE2::Regexp.new('w(o)(o)')
|
66
|
-
# "woo".re2_match(r, 1) #=> #<RE2::MatchData "woo" 1:"o">
|
67
|
-
# "woo".re2_match(r, 3) #=> #<RE2::MatchData "woo" 1:"o" 2:"o" 3:nil>
|
23
|
+
# @deprecated Use {RE2::Regexp#match} instead.
|
68
24
|
def re2_match(pattern, *args)
|
69
25
|
RE2::Regexp.new(pattern).match(self, *args)
|
70
26
|
end
|
71
27
|
|
72
|
-
#
|
73
|
-
# The returned string, used as a regular expression, will exactly match the
|
74
|
-
# original string.
|
75
|
-
#
|
76
|
-
# @return [String] the escaped string
|
77
|
-
# @example
|
78
|
-
# "1.5-2.0?".escape #=> "1\.5\-2\.0\?"
|
28
|
+
# @deprecated Use {RE2.QuoteMeta} instead.
|
79
29
|
def re2_escape
|
80
30
|
RE2.QuoteMeta(self)
|
81
31
|
end
|
data/lib/re2/version.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# re2 (https://github.com/mudge/re2)
|
4
|
+
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
|
5
|
+
# backtracking regular expression engines like those used in PCRE, Perl, and
|
6
|
+
# Python".
|
7
|
+
#
|
8
|
+
# Copyright (c) 2010, Paul Mucur (https://mudge.name)
|
9
|
+
# Released under the BSD Licence, please see LICENSE.txt
|
10
|
+
|
3
11
|
module RE2
|
4
|
-
VERSION = "2.
|
12
|
+
VERSION = "2.5.0"
|
5
13
|
end
|
data/lib/re2.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
# re2 (
|
2
|
-
# Ruby bindings to
|
1
|
+
# re2 (https://github.com/mudge/re2)
|
2
|
+
# Ruby bindings to RE2, a "fast, safe, thread-friendly alternative to
|
3
|
+
# backtracking regular expression engines like those used in PCRE, Perl, and
|
4
|
+
# Python".
|
3
5
|
#
|
4
|
-
# Copyright (c) 2010
|
6
|
+
# Copyright (c) 2010, Paul Mucur (https://mudge.name)
|
5
7
|
# Released under the BSD Licence, please see LICENSE.txt
|
8
|
+
|
6
9
|
begin
|
7
10
|
::RUBY_VERSION =~ /(\d+\.\d+)/
|
8
11
|
require_relative "#{Regexp.last_match(1)}/re2.so"
|
@@ -10,5 +13,6 @@ rescue LoadError
|
|
10
13
|
require 're2.so'
|
11
14
|
end
|
12
15
|
|
16
|
+
require "re2/regexp"
|
13
17
|
require "re2/scanner"
|
14
18
|
require "re2/version"
|
data/re2.gemspec
CHANGED
data/spec/kernel_spec.rb
CHANGED
@@ -5,7 +5,7 @@ RSpec.describe Kernel do
|
|
5
5
|
end
|
6
6
|
|
7
7
|
it "returns an RE2::Regexp instance given a pattern and options" do
|
8
|
-
re = RE2('w(o)(o)', :
|
8
|
+
re = RE2('w(o)(o)', case_sensitive: false)
|
9
9
|
|
10
10
|
expect(re).not_to be_case_sensitive
|
11
11
|
end
|
@@ -15,7 +15,7 @@ RSpec.describe Kernel do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
it "allows invalid patterns to be created" do
|
18
|
-
re = RE2('???', :
|
18
|
+
re = RE2('???', log_errors: false)
|
19
19
|
|
20
20
|
expect(re).to be_a(RE2::Regexp)
|
21
21
|
end
|
data/spec/re2/match_data_spec.rb
CHANGED
@@ -12,11 +12,13 @@ RSpec.describe RE2::MatchData do
|
|
12
12
|
describe "#to_a" do
|
13
13
|
it "is populated with the match and capturing groups" do
|
14
14
|
a = RE2::Regexp.new('w(o)(o)').match('woo').to_a
|
15
|
+
|
15
16
|
expect(a).to eq(["woo", "o", "o"])
|
16
17
|
end
|
17
18
|
|
18
19
|
it "populates optional capturing groups with nil if they are missing" do
|
19
20
|
a = RE2::Regexp.new('(\d?)(a)(b)').match('ab').to_a
|
21
|
+
|
20
22
|
expect(a).to eq(["ab", nil, "a", "b"])
|
21
23
|
end
|
22
24
|
|
@@ -27,15 +29,16 @@ RSpec.describe RE2::MatchData do
|
|
27
29
|
end
|
28
30
|
|
29
31
|
it "returns ISO-8859-1 strings if the pattern is not UTF-8" do
|
30
|
-
a = RE2::Regexp.new('w(o)(o)', :
|
32
|
+
a = RE2::Regexp.new('w(o)(o)', utf8: false).match('woo').to_a
|
31
33
|
|
32
34
|
expect(a.map(&:encoding)).to all eq(Encoding::ISO_8859_1)
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
38
|
describe "#[]" do
|
37
|
-
it "accesses capturing groups by numerical index" do
|
39
|
+
it "accesses capturing groups by numerical index", :aggregate_failures do
|
38
40
|
md = RE2::Regexp.new('(\d)(\d{2})').match("123")
|
41
|
+
|
39
42
|
expect(md[1]).to eq("1")
|
40
43
|
expect(md[2]).to eq("23")
|
41
44
|
end
|
@@ -47,46 +50,53 @@ RSpec.describe RE2::MatchData do
|
|
47
50
|
end
|
48
51
|
|
49
52
|
it "returns a ISO-8859-1 string by numerical index if the pattern is not UTF-8" do
|
50
|
-
md = RE2::Regexp.new('(\d)(\d{2})', :
|
53
|
+
md = RE2::Regexp.new('(\d)(\d{2})', utf8: false).match("123")
|
51
54
|
|
52
55
|
expect(md[1].encoding).to eq(Encoding::ISO_8859_1)
|
53
56
|
end
|
54
57
|
|
55
58
|
it "has the whole match as the 0th item" do
|
56
59
|
md = RE2::Regexp.new('(\d)(\d{2})').match("123")
|
60
|
+
|
57
61
|
expect(md[0]).to eq("123")
|
58
62
|
end
|
59
63
|
|
60
|
-
it "supports access by numerical ranges" do
|
64
|
+
it "supports access by numerical ranges", :aggregate_failures do
|
61
65
|
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789")
|
66
|
+
|
62
67
|
expect(md[1..3]).to eq(["123", "456", "789"])
|
63
68
|
expect(md[1...3]).to eq(["123", "456"])
|
64
69
|
end
|
65
70
|
|
66
|
-
it "supports slicing" do
|
71
|
+
it "supports slicing", :aggregate_failures do
|
67
72
|
md = RE2::Regexp.new('(\d+) (\d+) (\d+)').match("123 456 789")
|
73
|
+
|
68
74
|
expect(md[1, 3]).to eq(["123", "456", "789"])
|
69
75
|
expect(md[1, 2]).to eq(["123", "456"])
|
70
76
|
end
|
71
77
|
|
72
|
-
it "returns nil if attempting to access non-existent capturing groups by index" do
|
78
|
+
it "returns nil if attempting to access non-existent capturing groups by index", :aggregate_failures do
|
73
79
|
md = RE2::Regexp.new('(\d+)').match('bob 123')
|
80
|
+
|
74
81
|
expect(md[2]).to be_nil
|
75
82
|
expect(md[3]).to be_nil
|
76
83
|
end
|
77
84
|
|
78
85
|
it "allows access by string names when there are named groups" do
|
79
86
|
md = RE2::Regexp.new('(?P<numbers>\d+)').match('bob 123')
|
87
|
+
|
80
88
|
expect(md["numbers"]).to eq("123")
|
81
89
|
end
|
82
90
|
|
83
91
|
it "allows access by symbol names when there are named groups" do
|
84
92
|
md = RE2::Regexp.new('(?P<numbers>\d+)').match('bob 123')
|
93
|
+
|
85
94
|
expect(md[:numbers]).to eq("123")
|
86
95
|
end
|
87
96
|
|
88
|
-
it "allows access by names and indices with mixed groups" do
|
97
|
+
it "allows access by names and indices with mixed groups", :aggregate_failures do
|
89
98
|
md = RE2::Regexp.new('(?P<name>\w+)(\s*)(?P<numbers>\d+)').match("bob 123")
|
99
|
+
|
90
100
|
expect(md["name"]).to eq("bob")
|
91
101
|
expect(md[:name]).to eq("bob")
|
92
102
|
expect(md[2]).to eq(" ")
|
@@ -94,54 +104,60 @@ RSpec.describe RE2::MatchData do
|
|
94
104
|
expect(md[:numbers]).to eq("123")
|
95
105
|
end
|
96
106
|
|
97
|
-
it "returns nil if no such named group exists" do
|
107
|
+
it "returns nil if no such named group exists", :aggregate_failures do
|
98
108
|
md = RE2::Regexp.new('(\d+)').match("bob 123")
|
109
|
+
|
99
110
|
expect(md["missing"]).to be_nil
|
100
111
|
expect(md[:missing]).to be_nil
|
101
112
|
end
|
102
113
|
|
103
114
|
it "raises an error if given an inappropriate index" do
|
104
115
|
md = RE2::Regexp.new('(\d+)').match("bob 123")
|
116
|
+
|
105
117
|
expect { md[nil] }.to raise_error(TypeError)
|
106
118
|
end
|
107
119
|
|
108
|
-
|
109
|
-
|
110
|
-
md = RE2::Regexp.new('(?P<name>\S+)').match("bob")
|
111
|
-
expect(md[0].encoding.name).to eq("UTF-8")
|
112
|
-
expect(md["name"].encoding.name).to eq("UTF-8")
|
113
|
-
expect(md[:name].encoding.name).to eq("UTF-8")
|
114
|
-
end
|
120
|
+
it "returns UTF-8 encoded strings by default", :aggregate_failures do
|
121
|
+
md = RE2::Regexp.new('(?P<name>\S+)').match("bob")
|
115
122
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
123
|
+
expect(md[0].encoding.name).to eq("UTF-8")
|
124
|
+
expect(md["name"].encoding.name).to eq("UTF-8")
|
125
|
+
expect(md[:name].encoding.name).to eq("UTF-8")
|
126
|
+
end
|
127
|
+
|
128
|
+
it "returns Latin 1 strings encoding when utf-8 is false", :aggregate_failures do
|
129
|
+
md = RE2::Regexp.new('(?P<name>\S+)', utf8: false).match('bob')
|
130
|
+
|
131
|
+
expect(md[0].encoding.name).to eq("ISO-8859-1")
|
132
|
+
expect(md["name"].encoding.name).to eq("ISO-8859-1")
|
133
|
+
expect(md[:name].encoding.name).to eq("ISO-8859-1")
|
122
134
|
end
|
123
135
|
end
|
124
136
|
|
125
137
|
describe "#string" do
|
126
138
|
it "returns the original string to match against" do
|
127
139
|
re = RE2::Regexp.new('(\D+)').match("bob")
|
140
|
+
|
128
141
|
expect(re.string).to eq("bob")
|
129
142
|
end
|
130
143
|
|
131
144
|
it "returns a copy, not the actual original" do
|
132
145
|
string = "bob"
|
133
146
|
re = RE2::Regexp.new('(\D+)').match(string)
|
147
|
+
|
134
148
|
expect(re.string).to_not equal(string)
|
135
149
|
end
|
136
150
|
|
137
151
|
it "returns a frozen string" do
|
138
152
|
re = RE2::Regexp.new('(\D+)').match("bob")
|
153
|
+
|
139
154
|
expect(re.string).to be_frozen
|
140
155
|
end
|
141
156
|
|
142
157
|
it "does not copy the string if it was already frozen" do
|
143
158
|
string = "bob".freeze
|
144
159
|
re = RE2::Regexp.new('(\D+)').match(string)
|
160
|
+
|
145
161
|
expect(re.string).to equal(string)
|
146
162
|
end
|
147
163
|
end
|
@@ -149,6 +165,7 @@ RSpec.describe RE2::MatchData do
|
|
149
165
|
describe "#size" do
|
150
166
|
it "returns the number of capturing groups plus the matching string" do
|
151
167
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
168
|
+
|
152
169
|
expect(md.size).to eq(3)
|
153
170
|
end
|
154
171
|
end
|
@@ -156,6 +173,7 @@ RSpec.describe RE2::MatchData do
|
|
156
173
|
describe "#length" do
|
157
174
|
it "returns the number of capturing groups plus the matching string" do
|
158
175
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
176
|
+
|
159
177
|
expect(md.length).to eq(3)
|
160
178
|
end
|
161
179
|
end
|
@@ -164,6 +182,7 @@ RSpec.describe RE2::MatchData do
|
|
164
182
|
it "returns the original RE2::Regexp used" do
|
165
183
|
re = RE2::Regexp.new('(\d+)')
|
166
184
|
md = re.match("123")
|
185
|
+
|
167
186
|
expect(md.regexp).to equal(re)
|
168
187
|
end
|
169
188
|
end
|
@@ -171,11 +190,13 @@ RSpec.describe RE2::MatchData do
|
|
171
190
|
describe "#inspect" do
|
172
191
|
it "returns a text representation of the object and indices" do
|
173
192
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
193
|
+
|
174
194
|
expect(md.inspect).to eq('#<RE2::MatchData "1234 56" 1:"1234" 2:"56">')
|
175
195
|
end
|
176
196
|
|
177
197
|
it "represents missing matches as nil" do
|
178
198
|
md = RE2::Regexp.new('(\d+) (\d+)?').match("1234 ")
|
199
|
+
|
179
200
|
expect(md.inspect).to eq('#<RE2::MatchData "1234 " 1:"1234" 2:nil>')
|
180
201
|
end
|
181
202
|
end
|
@@ -183,14 +204,16 @@ RSpec.describe RE2::MatchData do
|
|
183
204
|
describe "#to_s" do
|
184
205
|
it "returns the matching part of the original string" do
|
185
206
|
md = RE2::Regexp.new('(\d{2,5})').match("one two 23456")
|
207
|
+
|
186
208
|
expect(md.to_s).to eq("23456")
|
187
209
|
end
|
188
210
|
end
|
189
211
|
|
190
212
|
describe "#to_ary" do
|
191
|
-
it "allows the object to be expanded with an asterisk" do
|
213
|
+
it "allows the object to be expanded with an asterisk", :aggregate_failures do
|
192
214
|
md = RE2::Regexp.new('(\d+) (\d+)').match("1234 56")
|
193
215
|
m1, m2, m3 = *md
|
216
|
+
|
194
217
|
expect(m1).to eq("1234 56")
|
195
218
|
expect(m2).to eq("1234")
|
196
219
|
expect(m3).to eq("56")
|
@@ -200,41 +223,49 @@ RSpec.describe RE2::MatchData do
|
|
200
223
|
describe "#begin" do
|
201
224
|
it "returns the offset of the start of a match by index" do
|
202
225
|
md = RE2::Regexp.new('(wo{2})').match('a woohoo')
|
226
|
+
|
203
227
|
expect(md.string[md.begin(0)..-1]).to eq('woohoo')
|
204
228
|
end
|
205
229
|
|
206
230
|
it "returns the offset of the start of a match by string name" do
|
207
231
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
232
|
+
|
208
233
|
expect(md.string[md.begin('foo')..-1]).to eq('foobar')
|
209
234
|
end
|
210
235
|
|
211
236
|
it "returns the offset of the start of a match by symbol name" do
|
212
237
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
238
|
+
|
213
239
|
expect(md.string[md.begin(:foo)..-1]).to eq('foobar')
|
214
240
|
end
|
215
241
|
|
216
242
|
it "returns the offset despite multibyte characters" do
|
217
243
|
md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby')
|
244
|
+
|
218
245
|
expect(md.string[md.begin(0)..-1]).to eq('Ruby')
|
219
246
|
end
|
220
247
|
|
221
248
|
it "returns nil for non-existent numerical matches" do
|
222
249
|
md = RE2::Regexp.new('(\d)').match('123')
|
250
|
+
|
223
251
|
expect(md.begin(10)).to be_nil
|
224
252
|
end
|
225
253
|
|
226
254
|
it "returns nil for negative numerical matches" do
|
227
255
|
md = RE2::Regexp.new('(\d)').match('123')
|
256
|
+
|
228
257
|
expect(md.begin(-4)).to be_nil
|
229
258
|
end
|
230
259
|
|
231
260
|
it "returns nil for non-existent named matches" do
|
232
261
|
md = RE2::Regexp.new('(\d)').match('123')
|
262
|
+
|
233
263
|
expect(md.begin('foo')).to be_nil
|
234
264
|
end
|
235
265
|
|
236
266
|
it "returns nil for non-existent symbol named matches" do
|
237
267
|
md = RE2::Regexp.new('(\d)').match('123')
|
268
|
+
|
238
269
|
expect(md.begin(:foo)).to be_nil
|
239
270
|
end
|
240
271
|
end
|
@@ -242,41 +273,49 @@ RSpec.describe RE2::MatchData do
|
|
242
273
|
describe "#end" do
|
243
274
|
it "returns the offset of the character following the end of a match" do
|
244
275
|
md = RE2::Regexp.new('(wo{2})').match('a woohoo')
|
276
|
+
|
245
277
|
expect(md.string[0...md.end(0)]).to eq('a woo')
|
246
278
|
end
|
247
279
|
|
248
280
|
it "returns the offset of a match by string name" do
|
249
281
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
282
|
+
|
250
283
|
expect(md.string[0...md.end('foo')]).to eq('a foo')
|
251
284
|
end
|
252
285
|
|
253
286
|
it "returns the offset of a match by symbol name" do
|
254
287
|
md = RE2::Regexp.new('(?P<foo>fo{2})').match('a foobar')
|
288
|
+
|
255
289
|
expect(md.string[0...md.end(:foo)]).to eq('a foo')
|
256
290
|
end
|
257
291
|
|
258
292
|
it "returns the offset despite multibyte characters" do
|
259
293
|
md = RE2::Regexp.new('(Ruby)').match('I ♥ Ruby')
|
294
|
+
|
260
295
|
expect(md.string[0...md.end(0)]).to eq('I ♥ Ruby')
|
261
296
|
end
|
262
297
|
|
263
298
|
it "returns nil for non-existent numerical matches" do
|
264
299
|
md = RE2::Regexp.new('(\d)').match('123')
|
300
|
+
|
265
301
|
expect(md.end(10)).to be_nil
|
266
302
|
end
|
267
303
|
|
268
304
|
it "returns nil for negative numerical matches" do
|
269
305
|
md = RE2::Regexp.new('(\d)').match('123')
|
306
|
+
|
270
307
|
expect(md.end(-4)).to be_nil
|
271
308
|
end
|
272
309
|
|
273
310
|
it "returns nil for non-existent named matches" do
|
274
311
|
md = RE2::Regexp.new('(\d)').match('123')
|
312
|
+
|
275
313
|
expect(md.end('foo')).to be_nil
|
276
314
|
end
|
277
315
|
|
278
316
|
it "returns nil for non-existent symbol named matches" do
|
279
317
|
md = RE2::Regexp.new('(\d)').match('123')
|
318
|
+
|
280
319
|
expect(md.end(:foo)).to be_nil
|
281
320
|
end
|
282
321
|
end
|
@@ -299,19 +338,19 @@ RSpec.describe RE2::MatchData do
|
|
299
338
|
it "returns all named captures if given nil" do
|
300
339
|
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
|
301
340
|
|
302
|
-
expect(md.deconstruct_keys(nil)).to eq(:
|
341
|
+
expect(md.deconstruct_keys(nil)).to eq(numbers: '123', letters: 'abc')
|
303
342
|
end
|
304
343
|
|
305
344
|
it "returns only named captures if given names" do
|
306
345
|
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
|
307
346
|
|
308
|
-
expect(md.deconstruct_keys([:numbers])).to eq(:
|
347
|
+
expect(md.deconstruct_keys([:numbers])).to eq(numbers: '123')
|
309
348
|
end
|
310
349
|
|
311
350
|
it "returns named captures up until an invalid name is given" do
|
312
351
|
md = RE2::Regexp.new('(?P<numbers>\d+) (?P<letters>[a-zA-Z]+)').match('123 abc')
|
313
352
|
|
314
|
-
expect(md.deconstruct_keys([:numbers, :punctuation])).to eq(:
|
353
|
+
expect(md.deconstruct_keys([:numbers, :punctuation])).to eq(numbers: '123')
|
315
354
|
end
|
316
355
|
|
317
356
|
it "returns an empty hash if given more capture names than exist" do
|