perf(date_parser): bound regex quantifiers for deterministic parsing performance (#36983)

This commit is contained in:
Jean Massucatto
2026-01-12 06:52:19 -03:00
committed by GitHub
parent d914b35cc0
commit 459b4cb23d
2 changed files with 52 additions and 5 deletions

View File

@@ -418,9 +418,9 @@ def get_since_until( # pylint: disable=too-many-arguments,too-many-locals,too-m
if time_range and separator in time_range:
time_range_lookup = [
(
r"^(start of|beginning of|end of)\s+"
r"(this|last|next|prior)\s+"
r"([0-9]+)?\s*"
r"^(start of|beginning of|end of)\s{1,5}"
r"(this|last|next|prior)\s{1,5}"
r"([0-9]+)?\s{0,5}"
r"(day|week|month|quarter|year)s?$", # Matches phrases like "start of next month" # noqa: E501
lambda modifier, scope, delta, unit: handle_modifier_and_unit(
modifier,
@@ -431,8 +431,8 @@ def get_since_until( # pylint: disable=too-many-arguments,too-many-locals,too-m
),
),
(
r"^(this|last|next|prior)\s+"
r"([0-9]+)?\s*"
r"^(this|last|next|prior)\s{1,5}"
r"([0-9]+)?\s{0,5}"
r"(second|minute|day|week|month|quarter|year)s?$", # Matches "next 5 days" or "last 2 weeks" # noqa: E501
lambda scope, delta, unit: handle_scope_and_unit(
scope, delta, unit, get_relative_base(unit, relative_start)