From 0ceca31ef19ef52d978a3ec1e079796a101f3df9 Mon Sep 17 00:00:00 2001 From: Artem Lytkin Date: Sat, 26 Sep 2026 23:34:46 +0300 Subject: [PATCH] hints kitten: Use Go's stdlib regexp for the builtin patterns regexp2 backtracks on the path pattern, retrying it at every position of a long line that has no spaces, so the path and linenum types took seconds on a screen full of such text (about 36s for a 380x100 window of hex). The builtin patterns dont need lookarounds, so use the linear time stdlib engine for them. User supplied --regex patterns stay on regexp2 so lookarounds keep working (#6265). --- docs/changelog.rst | 3 +++ kittens/hints/marks.go | 39 +++++++++++++++++++++++++++++++------ kittens/hints/marks_test.go | 7 +++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index ba5ccfb13..71e1f0a0b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -233,6 +233,9 @@ Detailed list of changes - macOS: Fix a custom dock icon reverting to the stock icon after a progress bar is cleared (:iss:`10545`) +- hints kitten: Fix delays of many seconds when selecting paths or line + numbers on a screen with long lines that contain no spaces + 0.49.1 [2026-09-24] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kittens/hints/marks.go b/kittens/hints/marks.go index 94be7d6ad..5a685990d 100644 --- a/kittens/hints/marks.go +++ b/kittens/hints/marks.go @@ -490,9 +490,25 @@ func find_all_matches(re *regexp2.Regexp, text string) (ans []Match, err error) return } -func mark(r *regexp2.Regexp, post_processors []PostProcessorFunc, group_processors []GroupProcessorFunc, text string, opts *Options) (ans []Mark) { +func find_all_matches_stdlib(re *regexp.Regexp, text string) (ans []Match) { + names := re.SubexpNames() + for _, loc := range re.FindAllStringSubmatchIndex(text, -1) { + match := Match{Groups: make([]Group, len(names))} + for i, name := range names { + match.Groups[i] = Group{Name: name, IsNamed: name != ""} + if s, e := loc[2*i], loc[2*i+1]; s > -1 { + c := Capture{Text: text[s:e]} + c.Byte_Offsets.Start, c.Byte_Offsets.End = s, e + match.Groups[i].Captures = []Capture{c} + } + } + ans = append(ans, match) + } + return +} + +func mark(all_matches []Match, post_processors []PostProcessorFunc, group_processors []GroupProcessorFunc, text string, opts *Options) (ans []Mark) { sanitize_pat := regexp.MustCompile("[\r\n\x00]") - all_matches, _ := find_all_matches(r, text) for i, m := range all_matches { full_capture := m.Groups[0].LastCapture() match_start, match_end := full_capture.Byte_Offsets.Start, full_capture.Byte_Offsets.End @@ -654,11 +670,22 @@ func find_marks(text string, opts *Options, cli_args ...string) (sanitized_text if err != nil { return err } - r, err := regexp2.Compile(pattern, regexp2.RE2) - if err != nil { - return fmt.Errorf("Failed to compile the regex pattern: %#v with error: %w", pattern, err) + var matches []Match + // the builtin patterns dont need lookarounds, so use the linear time stdlib engine for them + if pattern != opts.Regex { + r, err := regexp.Compile(pattern) + if err != nil { + return fmt.Errorf("Failed to compile the regex pattern: %#v with error: %w", pattern, err) + } + matches = find_all_matches_stdlib(r, sanitized_text) + } else { + r, err := regexp2.Compile(pattern, regexp2.RE2) + if err != nil { + return fmt.Errorf("Failed to compile the regex pattern: %#v with error: %w", pattern, err) + } + matches, _ = find_all_matches(r, sanitized_text) } - ans = mark(r, post_processors, group_processors, sanitized_text, opts) + ans = mark(matches, post_processors, group_processors, sanitized_text, opts) used_pattern = pattern return nil } diff --git a/kittens/hints/marks_test.go b/kittens/hints/marks_test.go index d48902335..059084196 100644 --- a/kittens/hints/marks_test.go +++ b/kittens/hints/marks_test.go @@ -98,6 +98,13 @@ func TestHintMarking(t *testing.T) { r("file.epub.", "file.epub") r("(file.epub)", "file.epub") r("some/path", "some/path") + // long lines without spaces must not cause quadratic backtracking + long_line := strings.Repeat("1,", 20000) + r(long_line) + r("漢字 "+long_line+" some/path", "some/path") + opts.Type = "linenum" + r(long_line) + m(long_line+" a/file.c:23", "a/file.c", 23) reset() cols = 60