Skip to content

Commit f21f48a

Browse files
author
Steve Ayers
committed
Merge main
2 parents a6353d1 + 4dd9232 commit f21f48a

File tree

2 files changed

+1
-48
lines changed

2 files changed

+1
-48
lines changed

protovalidate/internal/matches.py

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import operator
1615
import re
17-
from functools import reduce
1816

1917
import celpy
2018
from celpy import celtypes
@@ -34,20 +32,6 @@
3432
r"\[\\b.*\]", # Backspace eg: [\b]
3533
]
3634

37-
# Regex for searching a regex pattern for flags.
38-
flag_pattern = re.compile(r"^\(\?(?P<flags>[ims\-]+)\)")
39-
40-
# See https://docs.python.org/3/library/re.html#flags
41-
flag_mapping = {
42-
"a": re.A,
43-
"i": re.I,
44-
"L": re.L,
45-
"m": re.M,
46-
"s": re.S,
47-
"u": re.U,
48-
"x": re.X,
49-
}
50-
5135

5236
def cel_matches(text: celtypes.Value, pattern: celtypes.Value) -> celpy.Result:
5337
"""Return True if the given pattern matches text. False otherwise.
@@ -73,30 +57,9 @@ def cel_matches(text: celtypes.Value, pattern: celtypes.Value) -> celpy.Result:
7357
if r is not None:
7458
msg = f"error evaluating pattern {pattern}, invalid RE2 syntax"
7559
raise celpy.CELEvalError(msg)
76-
# The conformance tests use flags at the very beginning of the sequence, which
77-
# is likely the most common place where this rare feature will be used.
78-
#
79-
# So we check for the flags at the very beginning and if present, apply them
80-
# using Python re enums.
81-
flags = ""
82-
flag_matches = re.match(flag_pattern, pattern)
83-
if flag_matches is not None:
84-
flag_group = flag_matches.groupdict()["flags"]
85-
for fl in flag_group:
86-
# Flag removal, don't include it in the output
87-
if fl == "-":
88-
continue
89-
flags += fl
90-
# Grab the rest of the expression minus the flags
91-
pattern_str = pattern[len(flag_matches[0]) :]
92-
# Convert a string of flags (i.e. aiLm) into the actual re.A, re.I enums
93-
flags_enums = reduce(operator.or_, (flag_mapping[c] for c in flags if c in flag_mapping), 0)
94-
exp = re.compile(pattern_str, flags=flags_enums)
95-
else:
96-
exp = re.compile(pattern)
9760

9861
try:
99-
m = re.search(exp, text)
62+
m = re.search(pattern, text)
10063
except re.error as ex:
10164
return celpy.CELEvalError("match error", ex.__class__, ex.args)
10265

tests/matches_test.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,3 @@ def test_invalid_re2_syntax(self):
4242
self.fail(f"expected an error on pattern {cel_pattern}")
4343
except celpy.CELEvalError as e:
4444
self.assertEqual(str(e), f"error evaluating pattern {cel_pattern}, invalid RE2 syntax")
45-
46-
def test_flags(self) -> None:
47-
self.assertTrue(extra_func.cel_matches(celtypes.StringType("foobar"), celtypes.StringType("(?i:foo)(?-i:bar)")))
48-
self.assertTrue(extra_func.cel_matches(celtypes.StringType("FOObar"), celtypes.StringType("(?i:foo)(?-i:bar)")))
49-
self.assertFalse(
50-
extra_func.cel_matches(celtypes.StringType("fooBAR"), celtypes.StringType("(?i:foo)(?-i:bar)"))
51-
)
52-
self.assertFalse(
53-
extra_func.cel_matches(celtypes.StringType("FOOBAR"), celtypes.StringType("(?i:foo)(?-i:bar)"))
54-
)

0 commit comments

Comments
 (0)