From cbf0bc9207b71e67d43f73b6e6474fd839ce3fbd Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 7 Sep 2024 02:51:14 -0400 Subject: Optimization to terminate pattern searches early if there's a greedy {..} at the end --- builtins/text.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'builtins/text.c') diff --git a/builtins/text.c b/builtins/text.c index f322deb3..fb71d8d0 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -1630,6 +1630,11 @@ int64_t match(Text_t text, int64_t text_index, Pattern_t pattern, int64_t patter } } + if (pat.tag == PAT_ANY && !pat.negated && pattern_index >= pattern.length) { + int64_t remaining = text.length - text_index; + return remaining >= pat.min ? MIN(remaining, pat.max) : -1; + } + int64_t capture_start = text_index; int64_t count = 0, capture_len = 0, next_match_len = 0; -- cgit v1.2.3