Skip to content

Commit

Permalink
combined findUp and findDown to findNext
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias314 committed Jan 16, 2025
1 parent 539dbf2 commit 8bd0cac
Showing 1 changed file with 21 additions and 44 deletions.
65 changes: 21 additions & 44 deletions internal/buffer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8bd0cac

Please sign in to comment.