1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import operator
1615import re
17- from functools import reduce
1816
1917import celpy
2018from celpy import celtypes
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
5236def 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
0 commit comments