From 8bd0cac4649df464d2587f297e57d05844b92000 Mon Sep 17 00:00:00 2001 From: matthias314 Date: Thu, 16 Jan 2025 12:38:22 -0500 Subject: [PATCH] combined `findUp` and `findDown` to `findNext` --- internal/buffer/search.go | 65 +++++++++++++-------------------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/internal/buffer/search.go b/internal/buffer/search.go index ac9c60ac83..c886afbcf3 100644 --- a/internal/buffer/search.go +++ b/internal/buffer/search.go @@ -50,7 +50,7 @@ func findLineParams(b *Buffer, start, end Loc, i int) ([]byte, int, int) { return l, charpos, padMode } -func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) { +func (b *Buffer) findNext(r *regexp.Regexp, start, end Loc, down bool) ([2]Loc, bool) { lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1)) if start.Y > b.LinesNum()-1 { start.X = lastcn - 1 @@ -67,50 +67,27 @@ func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) { rPadded := padRegexp(r) - for i := start.Y; i <= end.Y; i++ { - l, charpos, padMode := findLineParams(b, start, end, i) - - match := rPadded[padMode].FindIndex(l) - - if match != nil { - start := Loc{charpos + util.RunePos(l, match[0]), i} - if padMode&padStart != 0 { - start = start.Move(1, b) - } - end := Loc{charpos + util.RunePos(l, match[1]), i} - if padMode&padEnd != 0 { - end = end.Move(-1, b) - } - return [2]Loc{start, end}, true - } - } - return [2]Loc{}, false -} - -func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) { - lastcn := util.CharacterCount(b.LineBytes(b.LinesNum() - 1)) - if start.Y > b.LinesNum()-1 { - start.X = lastcn - 1 - } - if end.Y > b.LinesNum()-1 { - end.X = lastcn - } - start.Y = util.Clamp(start.Y, 0, b.LinesNum()-1) - end.Y = util.Clamp(end.Y, 0, b.LinesNum()-1) - - if start.GreaterThan(end) { - start, end = end, start + var i0, i1, is int + if down { + i0, i1, is = start.Y, end.Y, 1 + } else { + i0, i1, is = end.Y, start.Y, -1 } - rPadded := padRegexp(r) - - for i := end.Y; i >= start.Y; i-- { + for i := i0; is*i <= is*i1; i += is { l, charpos, padMode := findLineParams(b, start, end, i) - allMatches := rPadded[padMode].FindAllIndex(l, -1) + var match []int + if down { + match = rPadded[padMode].FindIndex(l) + } else { + allMatches := rPadded[padMode].FindAllIndex(l, -1) + if allMatches != nil { + match = allMatches[len(allMatches)-1] + } + } - if allMatches != nil { - match := allMatches[len(allMatches)-1] + if match != nil { start := Loc{charpos + util.RunePos(l, match[0]), i} if padMode&padStart != 0 { start = start.Move(1, b) @@ -154,14 +131,14 @@ func (b *Buffer) FindNext(s string, start, end, from Loc, down bool, useRegex bo var found bool var l [2]Loc if down { - l, found = b.findDown(r, from, end) + l, found = b.findNext(r, from, end, true) if !found { - l, found = b.findDown(r, start, end) + l, found = b.findNext(r, start, end, true) } } else { - l, found = b.findUp(r, from, start) + l, found = b.findNext(r, from, start, false) if !found { - l, found = b.findUp(r, end, start) + l, found = b.findNext(r, end, start, false) } } return l, found, nil