Skip to content

Commit

Permalink
removed padRegexp
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias314 committed Jan 20, 2025
1 parent 3e16d5b commit 31a4d1c
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions internal/buffer/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@ import (
"github.com/zyedidia/micro/v2/internal/util"
)

const (
padStart = 1 << iota
padEnd
)

// We want "^" and "$" to match only the beginning/end of a line, not the
// beginning/end of the search region if it is in the middle of a line.
// In that case we use padded regexps to require a rune before or after
// the match. (This also affects other empty-string patters like "\\b".)
// The function padRegexp creates these padded regexps.
func padRegexp(r *regexp.Regexp) [4]*regexp.Regexp {
rPadStart := regexp.MustCompile(".(?:" + r.String() + ")")
rPadEnd := regexp.MustCompile("(?:" + r.String() + ").")
rPadBoth := regexp.MustCompile(".(?:" + r.String() + ").")
return [4]*regexp.Regexp{r, rPadStart, rPadEnd, rPadBoth}
}
// The following two flags indicate the padding used.
const (
padStart = 1 << iota
padEnd
)

func findLineParams(b *Buffer, start, end Loc, i int) ([]byte, int, int) {
func findLineParams(b *Buffer, start, end Loc, i int, r *regexp.Regexp) ([]byte, int, int, *regexp.Regexp) {
l := b.LineBytes(i)
charpos := 0
padMode := 0
Expand All @@ -47,7 +40,15 @@ func findLineParams(b *Buffer, start, end Loc, i int) ([]byte, int, int) {
}
}

return l, charpos, padMode
if padMode == padStart {
r = regexp.MustCompile(".(?:" + r.String() + ")")
} else if padMode == padEnd {
r = regexp.MustCompile("(?:" + r.String() + ").")
} else if padMode == padStart|padEnd {
r = regexp.MustCompile(".(?:" + r.String() + ").")
}

return l, charpos, padMode, r
}

func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
Expand All @@ -65,12 +66,10 @@ func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
start, end = end, start
}

rPadded := padRegexp(r)

for i := start.Y; i <= end.Y; i++ {
l, charpos, padMode := findLineParams(b, start, end, i)
l, charpos, padMode, rPadded := findLineParams(b, start, end, i, r)

match := rPadded[padMode].FindIndex(l)
match := rPadded.FindIndex(l)

if match != nil {
start := Loc{charpos + util.RunePos(l, match[0]), i}
Expand Down Expand Up @@ -102,12 +101,10 @@ func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
start, end = end, start
}

rPadded := padRegexp(r)

for i := end.Y; i >= start.Y; i-- {
l, charpos, padMode := findLineParams(b, start, end, i)
l, charpos, padMode, rPadded := findLineParams(b, start, end, i, r)

allMatches := rPadded[padMode].FindAllIndex(l, -1)
allMatches := rPadded.FindAllIndex(l, -1)

if allMatches != nil {
match := allMatches[len(allMatches)-1]
Expand Down

0 comments on commit 31a4d1c

Please sign in to comment.