Make router matcher lazy

This commit is contained in:
2025-07-19 04:12:46 +03:00
parent 15f6438407
commit af46710017

View File

@@ -42,8 +42,8 @@ class Route:
if '}' not in part:
raise ValueError(f'Invalid subpath substitute placeholder: {part}')
re_part = part.replace('(', '\\(').replace(')', '\\)')
names = re.findall(r'\{(.*)}', part)
re_part = re.sub(r'\{.*}', r'(.*)', re_part)
names = re.findall(r'\{(.*?)}', part)
re_part = re.sub(r'\{.*?}', r'(.*?)', re_part)
re_part = re.compile('^' + re_part + '$')
d = self._find_regexp_subroute(re_part)
@@ -100,10 +100,12 @@ class Router:
def add(self, method: MethodType, path_pattern: str, handler: InternalHandlerType) -> None:
method = typing.cast(MethodType, method.upper())
substitutes = re.findall(r'\{(.*)}', path_pattern)
substitutes = re.findall(r'\{(.*?)}', path_pattern)
if len(substitutes) > 0:
subst = []
for s in substitutes:
if isinstance(s, str):
s = [s]
subst += list(s)
if len(subst) != len(set(subst)):
raise ValueError('Duplicate path substitute names are prohibited')