re2 1.19.2 → 1.20.0

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.
Files changed (151) hide show
  1. package/.github/actions/linux-alpine-node-16/Dockerfile +1 -1
  2. package/.github/actions/linux-alpine-node-18/Dockerfile +1 -1
  3. package/.github/actions/linux-alpine-node-20/Dockerfile +1 -1
  4. package/.gitmodules +5 -2
  5. package/README.md +1 -0
  6. package/binding.gyp +82 -25
  7. package/lib/replace.cc +4 -4
  8. package/package.json +1 -1
  9. package/tsconfig.json +2 -1
  10. package/vendor/re2/.bazelrc +16 -0
  11. package/vendor/re2/.github/bazel.sh +24 -0
  12. package/vendor/re2/.github/cmake.sh +12 -0
  13. package/vendor/re2/.github/workflows/ci-bazel.yml +17 -0
  14. package/vendor/re2/.github/workflows/ci-cmake.yml +108 -0
  15. package/vendor/re2/.github/workflows/ci.yml +89 -0
  16. package/vendor/re2/.github/workflows/pr.yml +26 -0
  17. package/vendor/re2/.github/workflows/python.yml +185 -0
  18. package/vendor/re2/AUTHORS +13 -0
  19. package/vendor/re2/BUILD.bazel +280 -0
  20. package/vendor/re2/CMakeLists.txt +268 -0
  21. package/vendor/re2/CONTRIBUTING.md +2 -0
  22. package/vendor/re2/CONTRIBUTORS +41 -0
  23. package/vendor/re2/Makefile +400 -0
  24. package/vendor/{README → re2/README} +5 -0
  25. package/vendor/re2/SECURITY.md +4 -0
  26. package/vendor/re2/WORKSPACE.bazel +62 -0
  27. package/vendor/re2/app/BUILD.bazel +19 -0
  28. package/vendor/re2/app/_re2.cc +94 -0
  29. package/vendor/re2/app/_re2.d.ts +23 -0
  30. package/vendor/re2/app/app.ts +111 -0
  31. package/vendor/re2/app/build.sh +50 -0
  32. package/vendor/re2/app/index.html +5 -0
  33. package/vendor/re2/app/package.json +14 -0
  34. package/vendor/re2/app/rollup.config.js +28 -0
  35. package/vendor/re2/app/tsconfig.json +17 -0
  36. package/vendor/re2/benchlog/benchplot.py +98 -0
  37. package/vendor/re2/benchlog/mktable +155 -0
  38. package/vendor/re2/doc/mksyntaxgo +42 -0
  39. package/vendor/re2/doc/mksyntaxhtml +42 -0
  40. package/vendor/re2/doc/mksyntaxwiki +36 -0
  41. package/vendor/re2/doc/syntax.html +477 -0
  42. package/vendor/re2/doc/syntax.txt +463 -0
  43. package/vendor/re2/lib/git/commit-msg.hook +104 -0
  44. package/vendor/re2/libre2.symbols +19 -0
  45. package/vendor/re2/libre2.symbols.darwin +15 -0
  46. package/vendor/re2/python/BUILD.bazel +36 -0
  47. package/vendor/re2/python/README +1 -0
  48. package/vendor/re2/python/_re2.cc +338 -0
  49. package/vendor/re2/python/re2.py +582 -0
  50. package/vendor/re2/python/re2_test.py +482 -0
  51. package/vendor/re2/python/setup.py +105 -0
  52. package/vendor/re2/{bitmap256.cc → re2/bitmap256.cc} +4 -4
  53. package/vendor/re2/{bitstate.cc → re2/bitstate.cc} +21 -25
  54. package/vendor/re2/{compile.cc → re2/compile.cc} +5 -4
  55. package/vendor/re2/{dfa.cc → re2/dfa.cc} +120 -101
  56. package/vendor/re2/{filtered_re2.cc → re2/filtered_re2.cc} +8 -11
  57. package/vendor/re2/{filtered_re2.h → re2/filtered_re2.h} +5 -4
  58. package/vendor/re2/{fuzzing → re2/fuzzing}/re2_fuzzer.cc +4 -6
  59. package/vendor/re2/{mimics_pcre.cc → re2/mimics_pcre.cc} +0 -1
  60. package/vendor/re2/{nfa.cc → re2/nfa.cc} +34 -37
  61. package/vendor/re2/{onepass.cc → re2/onepass.cc} +25 -27
  62. package/vendor/re2/{parse.cc → re2/parse.cc} +118 -121
  63. package/vendor/re2/{prefilter.cc → re2/prefilter.cc} +2 -3
  64. package/vendor/re2/{prefilter.h → re2/prefilter.h} +0 -1
  65. package/vendor/re2/{prefilter_tree.cc → re2/prefilter_tree.cc} +4 -5
  66. package/vendor/re2/{prefilter_tree.h → re2/prefilter_tree.h} +1 -2
  67. package/vendor/re2/{prog.cc → re2/prog.cc} +20 -21
  68. package/vendor/re2/{prog.h → re2/prog.h} +21 -22
  69. package/vendor/re2/{re2.cc → re2/re2.cc} +51 -62
  70. package/vendor/re2/{re2.h → re2/re2.h} +78 -40
  71. package/vendor/re2/{regexp.cc → re2/regexp.cc} +11 -12
  72. package/vendor/re2/{regexp.h → re2/regexp.h} +9 -10
  73. package/vendor/re2/{set.cc → re2/set.cc} +3 -5
  74. package/vendor/re2/{set.h → re2/set.h} +4 -3
  75. package/vendor/re2/{simplify.cc → re2/simplify.cc} +1 -2
  76. package/vendor/re2/re2/stringpiece.h +24 -0
  77. package/vendor/re2/{testing → re2/testing}/backtrack.cc +22 -25
  78. package/vendor/re2/{testing → re2/testing}/charclass_test.cc +23 -21
  79. package/vendor/re2/{testing → re2/testing}/compile_test.cc +5 -4
  80. package/vendor/re2/{testing → re2/testing}/dfa_test.cc +36 -30
  81. package/vendor/re2/{testing → re2/testing}/dump.cc +8 -8
  82. package/vendor/re2/{testing → re2/testing}/exhaustive1_test.cc +1 -1
  83. package/vendor/re2/{testing → re2/testing}/exhaustive2_test.cc +1 -1
  84. package/vendor/re2/{testing → re2/testing}/exhaustive3_test.cc +1 -1
  85. package/vendor/re2/{testing → re2/testing}/exhaustive_test.cc +1 -1
  86. package/vendor/re2/{testing → re2/testing}/exhaustive_tester.cc +36 -32
  87. package/vendor/re2/{testing → re2/testing}/exhaustive_tester.h +0 -1
  88. package/vendor/re2/{testing → re2/testing}/filtered_re2_test.cc +9 -8
  89. package/vendor/re2/{testing → re2/testing}/mimics_pcre_test.cc +3 -2
  90. package/vendor/re2/{testing → re2/testing}/null_walker.cc +1 -1
  91. package/vendor/re2/{testing → re2/testing}/parse_test.cc +13 -12
  92. package/vendor/re2/{testing → re2/testing}/possible_match_test.cc +15 -14
  93. package/vendor/re2/{testing → re2/testing}/random_test.cc +13 -12
  94. package/vendor/re2/{testing → re2/testing}/re2_arg_test.cc +25 -2
  95. package/vendor/re2/{testing → re2/testing}/re2_test.cc +49 -47
  96. package/vendor/re2/{testing → re2/testing}/regexp_benchmark.cc +144 -145
  97. package/vendor/re2/{testing → re2/testing}/regexp_generator.cc +18 -14
  98. package/vendor/re2/{testing → re2/testing}/regexp_generator.h +3 -4
  99. package/vendor/re2/{testing → re2/testing}/regexp_test.cc +1 -1
  100. package/vendor/re2/{testing → re2/testing}/required_prefix_test.cc +5 -4
  101. package/vendor/re2/{testing → re2/testing}/search_test.cc +3 -2
  102. package/vendor/re2/{testing → re2/testing}/set_test.cc +1 -1
  103. package/vendor/re2/{testing → re2/testing}/simplify_test.cc +3 -2
  104. package/vendor/re2/{testing → re2/testing}/string_generator.cc +3 -3
  105. package/vendor/re2/{testing → re2/testing}/string_generator.h +5 -6
  106. package/vendor/re2/{testing → re2/testing}/string_generator_test.cc +2 -2
  107. package/vendor/re2/{testing → re2/testing}/tester.cc +77 -78
  108. package/vendor/re2/{testing → re2/testing}/tester.h +13 -15
  109. package/vendor/re2/{tostring.cc → re2/tostring.cc} +7 -8
  110. package/vendor/re2/{unicode_casefold.h → re2/unicode_casefold.h} +0 -1
  111. package/vendor/re2/{unicode_groups.h → re2/unicode_groups.h} +0 -1
  112. package/vendor/re2/{walker-inl.h → re2/walker-inl.h} +2 -1
  113. package/vendor/re2/re2.pc.in +9 -0
  114. package/vendor/re2/re2Config.cmake.in +28 -0
  115. package/vendor/re2/runtests +33 -0
  116. package/vendor/re2/testinstall.cc +27 -0
  117. package/vendor/re2/ucs2.diff +567 -0
  118. package/vendor/{util → re2/util}/logging.h +2 -2
  119. package/vendor/{util → re2/util}/pcre.cc +47 -116
  120. package/vendor/{util → re2/util}/pcre.h +53 -63
  121. package/vendor/re2/util/strutil.cc +26 -0
  122. package/vendor/{util → re2/util}/strutil.h +0 -5
  123. package/vendor/re2/stringpiece.cc +0 -65
  124. package/vendor/re2/stringpiece.h +0 -213
  125. package/vendor/util/benchmark.cc +0 -131
  126. package/vendor/util/benchmark.h +0 -156
  127. package/vendor/util/flags.h +0 -26
  128. package/vendor/util/mix.h +0 -41
  129. package/vendor/util/mutex.h +0 -164
  130. package/vendor/util/strutil.cc +0 -149
  131. package/vendor/util/test.cc +0 -34
  132. package/vendor/util/test.h +0 -50
  133. package/vendor/util/util.h +0 -42
  134. /package/vendor/{LICENSE → re2/LICENSE} +0 -0
  135. /package/vendor/re2/{bitmap256.h → re2/bitmap256.h} +0 -0
  136. /package/vendor/re2/{fuzzing → re2/fuzzing}/compiler-rt/LICENSE +0 -0
  137. /package/vendor/re2/{fuzzing → re2/fuzzing}/compiler-rt/include/fuzzer/FuzzedDataProvider.h +0 -0
  138. /package/vendor/re2/{make_perl_groups.pl → re2/make_perl_groups.pl} +0 -0
  139. /package/vendor/re2/{make_unicode_casefold.py → re2/make_unicode_casefold.py} +0 -0
  140. /package/vendor/re2/{make_unicode_groups.py → re2/make_unicode_groups.py} +0 -0
  141. /package/vendor/re2/{perl_groups.cc → re2/perl_groups.cc} +0 -0
  142. /package/vendor/re2/{pod_array.h → re2/pod_array.h} +0 -0
  143. /package/vendor/re2/{sparse_array.h → re2/sparse_array.h} +0 -0
  144. /package/vendor/re2/{sparse_set.h → re2/sparse_set.h} +0 -0
  145. /package/vendor/re2/{unicode.py → re2/unicode.py} +0 -0
  146. /package/vendor/re2/{unicode_casefold.cc → re2/unicode_casefold.cc} +0 -0
  147. /package/vendor/re2/{unicode_groups.cc → re2/unicode_groups.cc} +0 -0
  148. /package/vendor/{util → re2/util}/fuzz.cc +0 -0
  149. /package/vendor/{util → re2/util}/malloc_counter.h +0 -0
  150. /package/vendor/{util → re2/util}/rune.cc +0 -0
  151. /package/vendor/{util → re2/util}/utf.h +0 -0
@@ -0,0 +1,482 @@
1
+ # Copyright 2019 The RE2 Authors. All Rights Reserved.
2
+ # Use of this source code is governed by a BSD-style
3
+ # license that can be found in the LICENSE file.
4
+ """Tests for google3.third_party.re2.python.re2."""
5
+
6
+ import collections
7
+ import pickle
8
+ import re
9
+
10
+ from absl.testing import absltest
11
+ from absl.testing import parameterized
12
+ import re2
13
+
14
+
15
+ class OptionsTest(parameterized.TestCase):
16
+
17
+ @parameterized.parameters(*re2.Options.NAMES)
18
+ def test_option(self, name):
19
+ options = re2.Options()
20
+ value = getattr(options, name)
21
+ if isinstance(value, re2.Options.Encoding):
22
+ value = next(v for v in type(value).__members__.values() if v != value)
23
+ elif isinstance(value, bool):
24
+ value = not value
25
+ elif isinstance(value, int):
26
+ value = value + 1
27
+ else:
28
+ raise TypeError('option {!r}: {!r} {!r}'.format(name, type(value), value))
29
+ setattr(options, name, value)
30
+ self.assertEqual(value, getattr(options, name))
31
+
32
+
33
+ class Re2CompileTest(parameterized.TestCase):
34
+ """Contains tests that apply to the re2 module only.
35
+
36
+ We disagree with Python on the string types of group names,
37
+ so there is no point attempting to verify consistency.
38
+ """
39
+
40
+ @parameterized.parameters(
41
+ (u'(foo*)(?P<bar>qux+)', 2, [(u'bar', 2)]),
42
+ (b'(foo*)(?P<bar>qux+)', 2, [(b'bar', 2)]),
43
+ (u'(foo*)(?P<中文>qux+)', 2, [(u'中文', 2)]),
44
+ )
45
+ def test_compile(self, pattern, expected_groups, expected_groupindex):
46
+ regexp = re2.compile(pattern)
47
+ self.assertIs(regexp, re2.compile(pattern)) # cached
48
+ self.assertIs(regexp, re2.compile(regexp)) # cached
49
+ with self.assertRaisesRegex(re2.error,
50
+ ('pattern is already compiled, so '
51
+ 'options may not be specified')):
52
+ options = re2.Options()
53
+ options.log_errors = not options.log_errors
54
+ re2.compile(regexp, options=options)
55
+ self.assertIsNotNone(regexp.options)
56
+ self.assertEqual(expected_groups, regexp.groups)
57
+ self.assertDictEqual(dict(expected_groupindex), regexp.groupindex)
58
+
59
+ def test_compile_with_options(self):
60
+ options = re2.Options()
61
+ options.max_mem = 100
62
+ with self.assertRaisesRegex(re2.error, 'pattern too large'):
63
+ re2.compile('.{1000}', options=options)
64
+
65
+ def test_programsize_reverseprogramsize(self):
66
+ regexp = re2.compile('a+b')
67
+ self.assertEqual(7, regexp.programsize)
68
+ self.assertEqual(7, regexp.reverseprogramsize)
69
+
70
+ def test_programfanout_reverseprogramfanout(self):
71
+ regexp = re2.compile('a+b')
72
+ self.assertListEqual([1, 1], regexp.programfanout)
73
+ self.assertListEqual([3], regexp.reverseprogramfanout)
74
+
75
+ @parameterized.parameters(
76
+ (u'abc', 0, None),
77
+ (b'abc', 0, None),
78
+ (u'abc', 10, (b'abc', b'abc')),
79
+ (b'abc', 10, (b'abc', b'abc')),
80
+ (u'ab*c', 10, (b'ab', b'ac')),
81
+ (b'ab*c', 10, (b'ab', b'ac')),
82
+ (u'ab+c', 10, (b'abb', b'abc')),
83
+ (b'ab+c', 10, (b'abb', b'abc')),
84
+ (u'ab?c', 10, (b'abc', b'ac')),
85
+ (b'ab?c', 10, (b'abc', b'ac')),
86
+ (u'.*', 10, (b'', b'\xf4\xbf\xbf\xc0')),
87
+ (b'.*', 10, None),
88
+ (u'\\C*', 10, None),
89
+ (b'\\C*', 10, None),
90
+ )
91
+ def test_possiblematchrange(self, pattern, maxlen, expected_min_max):
92
+ # For brevity, the string type of pattern determines the encoding.
93
+ # It would otherwise be possible to have bytes with UTF8, but as per
94
+ # the module docstring, it isn't permitted to have str with LATIN1.
95
+ options = re2.Options()
96
+ if isinstance(pattern, str):
97
+ options.encoding = re2.Options.Encoding.UTF8
98
+ else:
99
+ options.encoding = re2.Options.Encoding.LATIN1
100
+ regexp = re2.compile(pattern, options=options)
101
+ if expected_min_max:
102
+ self.assertEqual(expected_min_max, regexp.possiblematchrange(maxlen))
103
+ else:
104
+ with self.assertRaisesRegex(re2.error, 'failed to compute match range'):
105
+ regexp.possiblematchrange(maxlen)
106
+
107
+
108
+ Params = collections.namedtuple(
109
+ 'Params', ('pattern', 'text', 'spans', 'search', 'match', 'fullmatch'))
110
+
111
+ PARAMS = [
112
+ Params(u'\\d+', u'Hello, world.', None, False, False, False),
113
+ Params(b'\\d+', b'Hello, world.', None, False, False, False),
114
+ Params(u'\\s+', u'Hello, world.', [(6, 7)], True, False, False),
115
+ Params(b'\\s+', b'Hello, world.', [(6, 7)], True, False, False),
116
+ Params(u'\\w+', u'Hello, world.', [(0, 5)], True, True, False),
117
+ Params(b'\\w+', b'Hello, world.', [(0, 5)], True, True, False),
118
+ Params(u'(\\d+)?', u'Hello, world.', [(0, 0), (-1, -1)], True, True, False),
119
+ Params(b'(\\d+)?', b'Hello, world.', [(0, 0), (-1, -1)], True, True, False),
120
+ Params(u'youtube(_device|_md|_gaia|_multiday|_multiday_gaia)?',
121
+ u'youtube_ads', [(0, 7), (-1, -1)], True, True, False),
122
+ Params(b'youtube(_device|_md|_gaia|_multiday|_multiday_gaia)?',
123
+ b'youtube_ads', [(0, 7), (-1, -1)], True, True, False),
124
+ ]
125
+
126
+
127
+ def upper(match):
128
+ return match.group().upper()
129
+
130
+
131
+ class ReRegexpTest(parameterized.TestCase):
132
+ """Contains tests that apply to the re and re2 modules."""
133
+
134
+ MODULE = re
135
+
136
+ @parameterized.parameters((p.pattern,) for p in PARAMS)
137
+ def test_pickle(self, pattern):
138
+ regexp = self.MODULE.compile(pattern)
139
+ rick = pickle.loads(pickle.dumps(regexp))
140
+ self.assertEqual(regexp.pattern, rick.pattern)
141
+
142
+ @parameterized.parameters(
143
+ (p.pattern, p.text, (p.spans if p.search else None)) for p in PARAMS)
144
+ def test_search(self, pattern, text, expected_spans):
145
+ match = self.MODULE.search(pattern, text)
146
+ if expected_spans is None:
147
+ self.assertIsNone(match)
148
+ else:
149
+ spans = [match.span(group) for group in range(match.re.groups + 1)]
150
+ self.assertListEqual(expected_spans, spans)
151
+
152
+ def test_search_with_pos_and_endpos(self):
153
+ regexp = self.MODULE.compile(u'.+') # empty string NOT allowed
154
+ text = u'I \u2665 RE2!'
155
+ # Note that len(text) is the position of the empty string at the end of
156
+ # text, so range() stops at len(text) + 1 in order to include len(text).
157
+ for pos in range(len(text) + 1):
158
+ for endpos in range(pos, len(text) + 1):
159
+ match = regexp.search(text, pos=pos, endpos=endpos)
160
+ if pos == endpos:
161
+ self.assertIsNone(match)
162
+ else:
163
+ self.assertEqual(pos, match.pos)
164
+ self.assertEqual(endpos, match.endpos)
165
+ self.assertEqual(pos, match.start())
166
+ self.assertEqual(endpos, match.end())
167
+ self.assertTupleEqual((pos, endpos), match.span())
168
+
169
+ def test_search_with_bogus_pos_and_endpos(self):
170
+ regexp = self.MODULE.compile(u'.*') # empty string allowed
171
+ text = u'I \u2665 RE2!'
172
+
173
+ match = regexp.search(text, pos=-100)
174
+ self.assertEqual(0, match.pos)
175
+ match = regexp.search(text, pos=100)
176
+ self.assertEqual(8, match.pos)
177
+
178
+ match = regexp.search(text, endpos=-100)
179
+ self.assertEqual(0, match.endpos)
180
+ match = regexp.search(text, endpos=100)
181
+ self.assertEqual(8, match.endpos)
182
+
183
+ match = regexp.search(text, pos=100, endpos=-100)
184
+ self.assertIsNone(match)
185
+
186
+ @parameterized.parameters(
187
+ (p.pattern, p.text, (p.spans if p.match else None)) for p in PARAMS)
188
+ def test_match(self, pattern, text, expected_spans):
189
+ match = self.MODULE.match(pattern, text)
190
+ if expected_spans is None:
191
+ self.assertIsNone(match)
192
+ else:
193
+ spans = [match.span(group) for group in range(match.re.groups + 1)]
194
+ self.assertListEqual(expected_spans, spans)
195
+
196
+ @parameterized.parameters(
197
+ (p.pattern, p.text, (p.spans if p.fullmatch else None)) for p in PARAMS)
198
+ def test_fullmatch(self, pattern, text, expected_spans):
199
+ match = self.MODULE.fullmatch(pattern, text)
200
+ if expected_spans is None:
201
+ self.assertIsNone(match)
202
+ else:
203
+ spans = [match.span(group) for group in range(match.re.groups + 1)]
204
+ self.assertListEqual(expected_spans, spans)
205
+
206
+ @parameterized.parameters(
207
+ (u'', u'', [(0, 0)]),
208
+ (b'', b'', [(0, 0)]),
209
+ (u'', u'x', [(0, 0), (1, 1)]),
210
+ (b'', b'x', [(0, 0), (1, 1)]),
211
+ (u'', u'xy', [(0, 0), (1, 1), (2, 2)]),
212
+ (b'', b'xy', [(0, 0), (1, 1), (2, 2)]),
213
+ (u'.', u'xy', [(0, 1), (1, 2)]),
214
+ (b'.', b'xy', [(0, 1), (1, 2)]),
215
+ (u'x', u'xy', [(0, 1)]),
216
+ (b'x', b'xy', [(0, 1)]),
217
+ (u'y', u'xy', [(1, 2)]),
218
+ (b'y', b'xy', [(1, 2)]),
219
+ (u'z', u'xy', []),
220
+ (b'z', b'xy', []),
221
+ (u'\\w*', u'Hello, world.', [(0, 5), (5, 5), (6, 6), (7, 12), (12, 12),
222
+ (13, 13)]),
223
+ (b'\\w*', b'Hello, world.', [(0, 5), (5, 5), (6, 6), (7, 12), (12, 12),
224
+ (13, 13)]),
225
+ )
226
+ def test_finditer(self, pattern, text, expected_matches):
227
+ matches = [match.span() for match in self.MODULE.finditer(pattern, text)]
228
+ self.assertListEqual(expected_matches, matches)
229
+
230
+ @parameterized.parameters(
231
+ (u'\\w\\w+', u'Hello, world.', [u'Hello', u'world']),
232
+ (b'\\w\\w+', b'Hello, world.', [b'Hello', b'world']),
233
+ (u'(\\w)\\w+', u'Hello, world.', [u'H', u'w']),
234
+ (b'(\\w)\\w+', b'Hello, world.', [b'H', b'w']),
235
+ (u'(\\w)(\\w+)', u'Hello, world.', [(u'H', u'ello'), (u'w', u'orld')]),
236
+ (b'(\\w)(\\w+)', b'Hello, world.', [(b'H', b'ello'), (b'w', b'orld')]),
237
+ (u'(\\w)(\\w+)?', u'Hello, w.', [(u'H', u'ello'), (u'w', u'')]),
238
+ (b'(\\w)(\\w+)?', b'Hello, w.', [(b'H', b'ello'), (b'w', b'')]),
239
+ )
240
+ def test_findall(self, pattern, text, expected_matches):
241
+ matches = self.MODULE.findall(pattern, text)
242
+ self.assertListEqual(expected_matches, matches)
243
+
244
+ @parameterized.parameters(
245
+ (u'\\W+', u'Hello, world.', -1, [u'Hello, world.']),
246
+ (b'\\W+', b'Hello, world.', -1, [b'Hello, world.']),
247
+ (u'\\W+', u'Hello, world.', 0, [u'Hello', u'world', u'']),
248
+ (b'\\W+', b'Hello, world.', 0, [b'Hello', b'world', b'']),
249
+ (u'\\W+', u'Hello, world.', 1, [u'Hello', u'world.']),
250
+ (b'\\W+', b'Hello, world.', 1, [b'Hello', b'world.']),
251
+ (u'(\\W+)', u'Hello, world.', -1, [u'Hello, world.']),
252
+ (b'(\\W+)', b'Hello, world.', -1, [b'Hello, world.']),
253
+ (u'(\\W+)', u'Hello, world.', 0, [u'Hello', u', ', u'world', u'.', u'']),
254
+ (b'(\\W+)', b'Hello, world.', 0, [b'Hello', b', ', b'world', b'.', b'']),
255
+ (u'(\\W+)', u'Hello, world.', 1, [u'Hello', u', ', u'world.']),
256
+ (b'(\\W+)', b'Hello, world.', 1, [b'Hello', b', ', b'world.']),
257
+ )
258
+ def test_split(self, pattern, text, maxsplit, expected_pieces):
259
+ pieces = self.MODULE.split(pattern, text, maxsplit)
260
+ self.assertListEqual(expected_pieces, pieces)
261
+
262
+ @parameterized.parameters(
263
+ (u'\\w+', upper, u'Hello, world.', -1, u'Hello, world.', 0),
264
+ (b'\\w+', upper, b'Hello, world.', -1, b'Hello, world.', 0),
265
+ (u'\\w+', upper, u'Hello, world.', 0, u'HELLO, WORLD.', 2),
266
+ (b'\\w+', upper, b'Hello, world.', 0, b'HELLO, WORLD.', 2),
267
+ (u'\\w+', upper, u'Hello, world.', 1, u'HELLO, world.', 1),
268
+ (b'\\w+', upper, b'Hello, world.', 1, b'HELLO, world.', 1),
269
+ (u'\\w+', u'MEEP', u'Hello, world.', -1, u'Hello, world.', 0),
270
+ (b'\\w+', b'MEEP', b'Hello, world.', -1, b'Hello, world.', 0),
271
+ (u'\\w+', u'MEEP', u'Hello, world.', 0, u'MEEP, MEEP.', 2),
272
+ (b'\\w+', b'MEEP', b'Hello, world.', 0, b'MEEP, MEEP.', 2),
273
+ (u'\\w+', u'MEEP', u'Hello, world.', 1, u'MEEP, world.', 1),
274
+ (b'\\w+', b'MEEP', b'Hello, world.', 1, b'MEEP, world.', 1),
275
+ (u'\\\\', u'\\\\\\\\', u'Hello,\\world.', 0, u'Hello,\\\\world.', 1),
276
+ (b'\\\\', b'\\\\\\\\', b'Hello,\\world.', 0, b'Hello,\\\\world.', 1),
277
+ )
278
+ def test_subn_sub(self, pattern, repl, text, count, expected_joined_pieces,
279
+ expected_numsplit):
280
+ joined_pieces, numsplit = self.MODULE.subn(pattern, repl, text, count)
281
+ self.assertEqual(expected_joined_pieces, joined_pieces)
282
+ self.assertEqual(expected_numsplit, numsplit)
283
+
284
+ joined_pieces = self.MODULE.sub(pattern, repl, text, count)
285
+ self.assertEqual(expected_joined_pieces, joined_pieces)
286
+
287
+
288
+ class Re2RegexpTest(ReRegexpTest):
289
+ """Contains tests that apply to the re2 module only."""
290
+
291
+ MODULE = re2
292
+
293
+ def test_compile_with_latin1_encoding(self):
294
+ options = re2.Options()
295
+ options.encoding = re2.Options.Encoding.LATIN1
296
+ with self.assertRaisesRegex(re2.error,
297
+ ('string type of pattern is str, but '
298
+ 'encoding specified in options is LATIN1')):
299
+ re2.compile(u'.?', options=options)
300
+
301
+ # ... whereas this is fine, of course.
302
+ re2.compile(b'.?', options=options)
303
+
304
+ @parameterized.parameters(
305
+ (u'\\p{Lo}', u'\u0ca0_\u0ca0', [(0, 1), (2, 3)]),
306
+ (b'\\p{Lo}', b'\xe0\xb2\xa0_\xe0\xb2\xa0', [(0, 3), (4, 7)]),
307
+ )
308
+ def test_finditer_with_utf8(self, pattern, text, expected_matches):
309
+ matches = [match.span() for match in self.MODULE.finditer(pattern, text)]
310
+ self.assertListEqual(expected_matches, matches)
311
+
312
+ def test_purge(self):
313
+ re2.compile('Goodbye, world.')
314
+ self.assertGreater(re2._Regexp._make.cache_info().currsize, 0)
315
+ re2.purge()
316
+ self.assertEqual(re2._Regexp._make.cache_info().currsize, 0)
317
+
318
+
319
+ class Re2EscapeTest(parameterized.TestCase):
320
+ """Contains tests that apply to the re2 module only.
321
+
322
+ We disagree with Python on the escaping of some characters,
323
+ so there is no point attempting to verify consistency.
324
+ """
325
+
326
+ @parameterized.parameters(
327
+ (u'a*b+c?', u'a\\*b\\+c\\?'),
328
+ (b'a*b+c?', b'a\\*b\\+c\\?'),
329
+ )
330
+ def test_escape(self, pattern, expected_escaped):
331
+ escaped = re2.escape(pattern)
332
+ self.assertEqual(expected_escaped, escaped)
333
+
334
+
335
+ class ReMatchTest(parameterized.TestCase):
336
+ """Contains tests that apply to the re and re2 modules."""
337
+
338
+ MODULE = re
339
+
340
+ def test_expand(self):
341
+ pattern = u'(?P<S>[\u2600-\u26ff]+).*?(?P<P>[^\\s\\w]+)'
342
+ text = u'I \u2665 RE2!\n'
343
+ match = self.MODULE.search(pattern, text)
344
+
345
+ self.assertEqual(u'\u2665\n!', match.expand(u'\\1\\n\\2'))
346
+ self.assertEqual(u'\u2665\n!', match.expand(u'\\g<1>\\n\\g<2>'))
347
+ self.assertEqual(u'\u2665\n!', match.expand(u'\\g<S>\\n\\g<P>'))
348
+ self.assertEqual(u'\\1\\2\n\u2665!', match.expand(u'\\\\1\\\\2\\n\\1\\2'))
349
+
350
+ def test_expand_with_octal(self):
351
+ pattern = u'()()()()()()()()()(\\w+)'
352
+ text = u'Hello, world.'
353
+ match = self.MODULE.search(pattern, text)
354
+
355
+ self.assertEqual(u'Hello\n', match.expand(u'\\g<0>\\n'))
356
+ self.assertEqual(u'Hello\n', match.expand(u'\\g<10>\\n'))
357
+
358
+ self.assertEqual(u'\x00\n', match.expand(u'\\0\\n'))
359
+ self.assertEqual(u'\x00\n', match.expand(u'\\00\\n'))
360
+ self.assertEqual(u'\x00\n', match.expand(u'\\000\\n'))
361
+ self.assertEqual(u'\x000\n', match.expand(u'\\0000\\n'))
362
+
363
+ self.assertEqual(u'\n', match.expand(u'\\1\\n'))
364
+ self.assertEqual(u'Hello\n', match.expand(u'\\10\\n'))
365
+ self.assertEqual(u'@\n', match.expand(u'\\100\\n'))
366
+ self.assertEqual(u'@0\n', match.expand(u'\\1000\\n'))
367
+
368
+ def test_getitem_group_groups_groupdict(self):
369
+ pattern = u'(?P<S>[\u2600-\u26ff]+).*?(?P<P>[^\\s\\w]+)'
370
+ text = u'Hello, world.\nI \u2665 RE2!\nGoodbye, world.\n'
371
+ match = self.MODULE.search(pattern, text)
372
+
373
+ self.assertEqual(u'\u2665 RE2!', match[0])
374
+ self.assertEqual(u'\u2665', match[1])
375
+ self.assertEqual(u'!', match[2])
376
+ self.assertEqual(u'\u2665', match[u'S'])
377
+ self.assertEqual(u'!', match[u'P'])
378
+
379
+ self.assertEqual(u'\u2665 RE2!', match.group())
380
+ self.assertEqual(u'\u2665 RE2!', match.group(0))
381
+ self.assertEqual(u'\u2665', match.group(1))
382
+ self.assertEqual(u'!', match.group(2))
383
+ self.assertEqual(u'\u2665', match.group(u'S'))
384
+ self.assertEqual(u'!', match.group(u'P'))
385
+
386
+ self.assertTupleEqual((u'\u2665', u'!'), match.group(1, 2))
387
+ self.assertTupleEqual((u'\u2665', u'!'), match.group(u'S', u'P'))
388
+ self.assertTupleEqual((u'\u2665', u'!'), match.groups())
389
+ self.assertDictEqual({u'S': u'\u2665', u'P': u'!'}, match.groupdict())
390
+
391
+ def test_bogus_group_start_end_and_span(self):
392
+ pattern = u'(?P<S>[\u2600-\u26ff]+).*?(?P<P>[^\\s\\w]+)'
393
+ text = u'I \u2665 RE2!\n'
394
+ match = self.MODULE.search(pattern, text)
395
+
396
+ self.assertRaises(IndexError, match.group, -1)
397
+ self.assertRaises(IndexError, match.group, 3)
398
+ self.assertRaises(IndexError, match.group, 'X')
399
+
400
+ self.assertRaises(IndexError, match.start, -1)
401
+ self.assertRaises(IndexError, match.start, 3)
402
+
403
+ self.assertRaises(IndexError, match.end, -1)
404
+ self.assertRaises(IndexError, match.end, 3)
405
+
406
+ self.assertRaises(IndexError, match.span, -1)
407
+ self.assertRaises(IndexError, match.span, 3)
408
+
409
+ @parameterized.parameters(
410
+ (u'((a)(b))((c)(d))', u'foo bar qux', None, None),
411
+ (u'(?P<one>(a)(b))((c)(d))', u'foo abcd qux', 4, None),
412
+ (u'(?P<one>(a)(b))(?P<four>(c)(d))', u'foo abcd qux', 4, 'four'),
413
+ )
414
+ def test_lastindex_lastgroup(self, pattern, text, expected_lastindex,
415
+ expected_lastgroup):
416
+ match = self.MODULE.search(pattern, text)
417
+ if expected_lastindex is None:
418
+ self.assertIsNone(match)
419
+ else:
420
+ self.assertEqual(expected_lastindex, match.lastindex)
421
+ self.assertEqual(expected_lastgroup, match.lastgroup)
422
+
423
+
424
+ class Re2MatchTest(ReMatchTest):
425
+ """Contains tests that apply to the re2 module only."""
426
+
427
+ MODULE = re2
428
+
429
+
430
+ class SetTest(absltest.TestCase):
431
+
432
+ def test_search(self):
433
+ s = re2.Set.SearchSet()
434
+ self.assertEqual(0, s.Add('\\d+'))
435
+ self.assertEqual(1, s.Add('\\s+'))
436
+ self.assertEqual(2, s.Add('\\w+'))
437
+ self.assertRaises(re2.error, s.Add, '(MEEP')
438
+ s.Compile()
439
+ self.assertItemsEqual([1, 2], s.Match('Hello, world.'))
440
+
441
+ def test_match(self):
442
+ s = re2.Set.MatchSet()
443
+ self.assertEqual(0, s.Add('\\d+'))
444
+ self.assertEqual(1, s.Add('\\s+'))
445
+ self.assertEqual(2, s.Add('\\w+'))
446
+ self.assertRaises(re2.error, s.Add, '(MEEP')
447
+ s.Compile()
448
+ self.assertItemsEqual([2], s.Match('Hello, world.'))
449
+
450
+ def test_fullmatch(self):
451
+ s = re2.Set.FullMatchSet()
452
+ self.assertEqual(0, s.Add('\\d+'))
453
+ self.assertEqual(1, s.Add('\\s+'))
454
+ self.assertEqual(2, s.Add('\\w+'))
455
+ self.assertRaises(re2.error, s.Add, '(MEEP')
456
+ s.Compile()
457
+ self.assertIsNone(s.Match('Hello, world.'))
458
+
459
+
460
+ class FilterTest(absltest.TestCase):
461
+
462
+ def test_match(self):
463
+ f = re2.Filter()
464
+ self.assertEqual(0, f.Add('Hello, \\w+\\.'))
465
+ self.assertEqual(1, f.Add('\\w+, world\\.'))
466
+ self.assertEqual(2, f.Add('Goodbye, \\w+\\.'))
467
+ self.assertRaises(re2.error, f.Add, '(MEEP')
468
+ f.Compile()
469
+ self.assertItemsEqual([0, 1], f.Match('Hello, world.', potential=True))
470
+ self.assertItemsEqual([0, 1], f.Match('HELLO, WORLD.', potential=True))
471
+ self.assertItemsEqual([0, 1], f.Match('Hello, world.'))
472
+ self.assertIsNone(f.Match('HELLO, WORLD.'))
473
+
474
+ self.assertRaises(IndexError, f.re, -1)
475
+ self.assertRaises(IndexError, f.re, 3)
476
+ self.assertEqual('Goodbye, \\w+\\.', f.re(2).pattern)
477
+ # Verify whether the underlying RE2 object is usable.
478
+ self.assertEqual(0, f.re(2).groups)
479
+
480
+
481
+ if __name__ == '__main__':
482
+ absltest.main()
@@ -0,0 +1,105 @@
1
+ # Copyright 2019 The RE2 Authors. All Rights Reserved.
2
+ # Use of this source code is governed by a BSD-style
3
+ # license that can be found in the LICENSE file.
4
+
5
+ import os
6
+ import setuptools
7
+ import setuptools.command.build_ext
8
+ import shutil
9
+ import sys
10
+
11
+ long_description = r"""A drop-in replacement for the re module.
12
+
13
+ It uses RE2 under the hood, of course, so various PCRE features
14
+ (e.g. backreferences, look-around assertions) are not supported.
15
+ See https://github.com/google/re2/wiki/Syntax for the canonical
16
+ reference, but known syntactic "gotchas" relative to Python are:
17
+
18
+ * PCRE supports \Z and \z; RE2 supports \z; Python supports \z,
19
+ but calls it \Z. You must rewrite \Z to \z in pattern strings.
20
+
21
+ Known differences between this module's API and the re module's API:
22
+
23
+ * The error class does not provide any error information as attributes.
24
+ * The Options class replaces the re module's flags with RE2's options as
25
+ gettable/settable properties. Please see re2.h for their documentation.
26
+ * The pattern string and the input string do not have to be the same type.
27
+ Any str will be encoded to UTF-8.
28
+ * The pattern string cannot be str if the options specify Latin-1 encoding.
29
+
30
+ Known issues with regard to building the C++ extension:
31
+
32
+ * Building requires RE2 to be installed on your system.
33
+ On Debian, for example, install the libre2-dev package.
34
+ * Building requires pybind11 to be installed on your system OR venv.
35
+ On Debian, for example, install the pybind11-dev package.
36
+ For a venv, install the pybind11 package from PyPI.
37
+ * Building on macOS is known to work, but has been known to fail.
38
+ For example, the system Python may not know which compiler flags
39
+ to set when building bindings for software installed by Homebrew;
40
+ see https://docs.brew.sh/Homebrew-and-Python#brewed-python-modules.
41
+ * Building on Windows has not been tested yet and will probably fail.
42
+ """
43
+
44
+
45
+ class BuildExt(setuptools.command.build_ext.build_ext):
46
+
47
+ def build_extension(self, ext):
48
+ if 'GITHUB_ACTIONS' not in os.environ:
49
+ return super().build_extension(ext)
50
+
51
+ # For @pybind11_bazel's `python_configure()`.
52
+ os.environ['PYTHON_BIN_PATH'] = sys.executable
53
+
54
+ cmd = ['bazel', 'clean', '--expunge']
55
+ self.spawn(cmd)
56
+
57
+ cmd = ['bazel', 'build']
58
+ if 'BAZEL_CPU' in os.environ:
59
+ cmd.append(f'--cpu={os.environ["BAZEL_CPU"].lower()}')
60
+ cmd += ['--compilation_mode=opt', '--', ':all']
61
+ self.spawn(cmd)
62
+
63
+ # This ensures that f'_re2.{importlib.machinery.EXTENSION_SUFFIXES[0]}'
64
+ # is the filename in the destination directory, which is what's needed.
65
+ shutil.copyfile('../bazel-bin/python/_re2.so',
66
+ self.get_ext_fullpath(ext.name))
67
+
68
+
69
+ def include_dirs():
70
+ try:
71
+ import pybind11
72
+ yield pybind11.get_include()
73
+ except ModuleNotFoundError:
74
+ pass
75
+
76
+
77
+ ext_module = setuptools.Extension(
78
+ name='_re2',
79
+ sources=['_re2.cc'],
80
+ include_dirs=list(include_dirs()),
81
+ libraries=['re2'],
82
+ extra_compile_args=['-fvisibility=hidden'],
83
+ )
84
+
85
+ setuptools.setup(
86
+ name='google-re2',
87
+ version='1.0',
88
+ description='RE2 Python bindings',
89
+ long_description=long_description,
90
+ long_description_content_type='text/plain',
91
+ author='The RE2 Authors',
92
+ author_email='re2-dev@googlegroups.com',
93
+ url='https://github.com/google/re2',
94
+ py_modules=['re2'],
95
+ ext_modules=[ext_module],
96
+ classifiers=[
97
+ 'Development Status :: 5 - Production/Stable',
98
+ 'Intended Audience :: Developers',
99
+ 'License :: OSI Approved :: BSD License',
100
+ 'Programming Language :: C++',
101
+ 'Programming Language :: Python :: 3.7',
102
+ ],
103
+ cmdclass={'build_ext': BuildExt},
104
+ python_requires='~=3.7',
105
+ )
@@ -6,7 +6,7 @@
6
6
 
7
7
  #include <stdint.h>
8
8
 
9
- #include "util/util.h"
9
+ #include "absl/base/macros.h"
10
10
  #include "util/logging.h"
11
11
 
12
12
  namespace re2 {
@@ -27,15 +27,15 @@ int Bitmap256::FindNextSetBit(int c) const {
27
27
  case 1:
28
28
  if (words_[1] != 0)
29
29
  return (1 * 64) + FindLSBSet(words_[1]);
30
- FALLTHROUGH_INTENDED;
30
+ ABSL_FALLTHROUGH_INTENDED;
31
31
  case 2:
32
32
  if (words_[2] != 0)
33
33
  return (2 * 64) + FindLSBSet(words_[2]);
34
- FALLTHROUGH_INTENDED;
34
+ ABSL_FALLTHROUGH_INTENDED;
35
35
  case 3:
36
36
  if (words_[3] != 0)
37
37
  return (3 * 64) + FindLSBSet(words_[3]);
38
- FALLTHROUGH_INTENDED;
38
+ ABSL_FALLTHROUGH_INTENDED;
39
39
  default:
40
40
  return -1;
41
41
  }