Refs #34691 -- Optimized system check for unmatched angle brackets in path().

Follow up to d1855c4847.
This commit is contained in:
Mariusz Felisiak 2023-07-11 22:45:31 +02:00 committed by GitHub
parent 7f2bc365b3
commit e5e9699e0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 23 deletions

View File

@ -336,30 +336,21 @@ class RoutePattern(CheckURLMixin):
def _check_pattern_unmatched_angle_brackets(self): def _check_pattern_unmatched_angle_brackets(self):
warnings = [] warnings = []
segments = self._route.split("/") msg = "Your URL pattern %s has an unmatched '%s' bracket."
for segment in segments: brackets = re.findall(r"[<>]", str(self._route))
open_bracket_counter = 0 open_bracket_counter = 0
unmatched_angle_bracket = None for bracket in brackets:
for char in segment: if bracket == "<":
if char == "<": open_bracket_counter += 1
open_bracket_counter += 1 elif bracket == ">":
elif char == ">": open_bracket_counter -= 1
open_bracket_counter -= 1 if open_bracket_counter < 0:
if open_bracket_counter < 0: warnings.append(
unmatched_angle_bracket = ">" Warning(msg % (self.describe(), ">"), id="urls.W010")
break
else:
if open_bracket_counter > 0:
unmatched_angle_bracket = "<"
if unmatched_angle_bracket is not None:
warnings.append(
Warning(
"Your URL pattern %s has an unmatched '%s' bracket."
% (self.describe(), unmatched_angle_bracket),
id="urls.W010",
) )
) open_bracket_counter = 0
if open_bracket_counter > 0:
warnings.append(Warning(msg % (self.describe(), "<"), id="urls.W010"))
return warnings return warnings
def _compile(self, route): def _compile(self, route):