Skip to content

Commit f6001af

Browse files
authored
Compile regexps to make them more efficient. (#98)
Fixes flag check for regexp logic (never encountered before as they were all strings, not compiled patterns).
1 parent 9edbd36 commit f6001af

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

python/cucumber_expressions/parameter_type.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from __future__ import annotations
22

33
import re
4-
from typing import List, Pattern
4+
from typing import List, Pattern, Union
55

66
from cucumber_expressions.errors import CucumberExpressionError
77

8-
ILLEGAL_PARAMETER_NAME_PATTERN = r"([\[\]()$.|?*+])"
9-
UNESCAPE_PATTERN = r"(\\([\[$.|?*+\]]))"
8+
ILLEGAL_PARAMETER_NAME_PATTERN = re.compile(r"([\[\]()$.|?*+])")
109

1110

1211
class ParameterType:
@@ -21,7 +20,7 @@ def _check_parameter_type_name(self, type_name):
2120

2221
@staticmethod
2322
def _is_valid_parameter_type_name(type_name):
24-
return not bool(re.compile(ILLEGAL_PARAMETER_NAME_PATTERN).match(type_name))
23+
return not bool(ILLEGAL_PARAMETER_NAME_PATTERN.match(type_name))
2524

2625
def transform(self, group_values: list[str]):
2726
"""Transform values according to the lambda expression provided"""
@@ -56,7 +55,7 @@ def __init__(
5655
:param name: name of the parameter type
5756
:type name: Optional[str]
5857
:param regexp: regexp or list of regexps for capture groups
59-
:type regexp: list[str] or str
58+
:type regexp: list[str], str, list[Pattern] or Pattern
6059
:param type: the return type of the transformed
6160
:type type: class
6261
:param transformer: transforms a str to (possibly) another type
@@ -73,7 +72,7 @@ def __init__(
7372
self.transformer = transformer
7473
self._use_for_snippets = use_for_snippets
7574
self._prefer_for_regexp_match = prefer_for_regexp_match
76-
self.regexps = self.string_array(regexp)
75+
self.regexps = self.to_array(regexp)
7776

7877
@property
7978
def prefer_for_regexp_match(self):
@@ -87,14 +86,20 @@ def use_for_snippets(self):
8786
def _get_regexp_source(regexp_pattern: Pattern) -> str:
8887
invalid_flags = [re.I, re.M]
8988
for invalid_flag in invalid_flags:
90-
if invalid_flag in regexp_pattern.flags:
89+
_regexp_flags = regexp_pattern.flags
90+
_regexp_flags = (
91+
_regexp_flags if isinstance(_regexp_flags, list) else [_regexp_flags]
92+
)
93+
if invalid_flag.real in _regexp_flags:
9194
raise CucumberExpressionError(
92-
f"ParameterType Regexps can't use flag: {invalid_flag}"
95+
f"ParameterType Regexps can't use flag: {str(invalid_flag)}"
9396
)
9497
return regexp_pattern.pattern
9598

96-
def string_array(self, regexps):
97-
"""Make a list of string regexps if not already"""
99+
def to_array(
100+
self, regexps: Union[List[str], str, List[Pattern], Pattern]
101+
) -> List[str]:
102+
"""Make a list of regexps if not already"""
98103
array: List = regexps if isinstance(regexps, list) else [regexps]
99104
return [
100105
regexp if isinstance(regexp, str) else self._get_regexp_source(regexp)

python/cucumber_expressions/parameter_type_registry.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import re
23
from decimal import Decimal
34
from typing import Optional, List
45

@@ -9,11 +10,14 @@
910
AmbiguousParameterTypeError,
1011
)
1112

12-
INTEGER_REGEXPS = [r"-?\d+", r"\d+"]
13-
FLOAT_REGEXP = r"(?=.*\d.*)[-+]?\d*(?:\.(?=\d.*))?\d*(?:\d+[E][+-]?\d+)?"
14-
WORD_REGEXP = r"[^\s]+"
15-
STRING_REGEXPS = [r'"([^\"\\]*(\\.[^\"\\]*)*)"', r"'([^'\\]*(\\.[^'\\]*)*)'"]
16-
ANONYMOUS_REGEXP = r".*"
13+
INTEGER_REGEXPS = [re.compile(r"-?\d+"), re.compile(r"\d+")]
14+
FLOAT_REGEXP = re.compile(r"(?=.*\d.*)[-+]?\d*(?:\.(?=\d.*))?\d*(?:\d+[E][+-]?\d+)?")
15+
WORD_REGEXP = re.compile(r"[^\s]+")
16+
STRING_REGEXPS = [
17+
re.compile(r'"([^\"\\]*(\\.[^\"\\]*)*)"'),
18+
re.compile(r"'([^'\\]*(\\.[^'\\]*)*)'"),
19+
]
20+
ANONYMOUS_REGEXP = re.compile(r".*")
1721

1822

1923
class ParameterTypeRegistry:

0 commit comments

Comments
 (0)