Skip to content

Commit 43c9c30

Browse files
committed
removed padRegexp
1 parent c7b676c commit 43c9c30

1 file changed

Lines changed: 19 additions & 22 deletions

File tree

internal/buffer/search.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,17 @@ import (
77
"github.com/zyedidia/micro/v2/internal/util"
88
)
99

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

27-
func findLineParams(b *Buffer, start, end Loc, i int) ([]byte, int, int) {
20+
func findLineParams(b *Buffer, start, end Loc, i int, r *regexp.Regexp) ([]byte, int, int, *regexp.Regexp) {
2821
l := b.LineBytes(i)
2922
charpos := 0
3023
padMode := 0
@@ -48,7 +41,15 @@ func findLineParams(b *Buffer, start, end Loc, i int) ([]byte, int, int) {
4841
}
4942
}
5043

51-
return l, charpos, padMode
44+
if padMode == padStart {
45+
r = regexp.MustCompile(".(?:" + r.String() + ")")
46+
} else if padMode == padEnd {
47+
r = regexp.MustCompile("(?:" + r.String() + ").")
48+
} else if padMode == padStart|padEnd {
49+
r = regexp.MustCompile(".(?:" + r.String() + ").")
50+
}
51+
52+
return l, charpos, padMode, r
5253
}
5354

5455
func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
@@ -66,12 +67,10 @@ func (b *Buffer) findDown(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
6667
start, end = end, start
6768
}
6869

69-
rPadded := padRegexp(r)
70-
7170
for i := start.Y; i <= end.Y; i++ {
72-
l, charpos, padMode := findLineParams(b, start, end, i)
71+
l, charpos, padMode, rPadded := findLineParams(b, start, end, i, r)
7372

74-
match := rPadded[padMode].FindIndex(l)
73+
match := rPadded.FindIndex(l)
7574

7675
if match != nil {
7776
if padMode&padStart != 0 {
@@ -105,12 +104,10 @@ func (b *Buffer) findUp(r *regexp.Regexp, start, end Loc) ([2]Loc, bool) {
105104
start, end = end, start
106105
}
107106

108-
rPadded := padRegexp(r)
109-
110107
for i := end.Y; i >= start.Y; i-- {
111-
l, charpos, padMode := findLineParams(b, start, end, i)
108+
l, charpos, padMode, rPadded := findLineParams(b, start, end, i, r)
112109

113-
allMatches := rPadded[padMode].FindAllIndex(l, -1)
110+
allMatches := rPadded.FindAllIndex(l, -1)
114111

115112
if allMatches != nil {
116113
match := allMatches[len(allMatches)-1]

0 commit comments

Comments
 (0)