mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-17 10:58:49 -07:00
4c3cd4c9e3
Because it's literally ten times faster: benchmark old ns/op new ns/op delta BenchmarkMatch-8 13842 1200 -91.33% BenchmarkMatchCached-8 139 147 +5.76% benchmark old allocs new allocs delta BenchmarkMatch-8 0 0 +0.00% BenchmarkMatchCached-8 0 0 +0.00% benchmark old bytes new bytes delta BenchmarkMatch-8 12 0 -100.00% BenchmarkMatchCached-8 0 0 +0.00%
59 lines
946 B
Go
59 lines
946 B
Go
package match
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Contains struct {
|
|
Needle string
|
|
Not bool
|
|
}
|
|
|
|
func NewContains(needle string, not bool) Contains {
|
|
return Contains{needle, not}
|
|
}
|
|
|
|
func (self Contains) Match(s string) bool {
|
|
return strings.Contains(s, self.Needle) != self.Not
|
|
}
|
|
|
|
func (self Contains) Index(s string) (int, []int) {
|
|
var offset int
|
|
|
|
idx := strings.Index(s, self.Needle)
|
|
|
|
if !self.Not {
|
|
if idx == -1 {
|
|
return -1, nil
|
|
}
|
|
|
|
offset = idx + len(self.Needle)
|
|
if len(s) <= offset {
|
|
return 0, []int{offset}
|
|
}
|
|
s = s[offset:]
|
|
} else if idx != -1 {
|
|
s = s[:idx]
|
|
}
|
|
|
|
segments := acquireSegments(len(s) + 1)
|
|
for i, _ := range s {
|
|
segments = append(segments, offset+i)
|
|
}
|
|
|
|
return 0, append(segments, offset+len(s))
|
|
}
|
|
|
|
func (self Contains) Len() int {
|
|
return lenNo
|
|
}
|
|
|
|
func (self Contains) String() string {
|
|
var not string
|
|
if self.Not {
|
|
not = "!"
|
|
}
|
|
return fmt.Sprintf("<contains:%s[%s]>", not, self.Needle)
|
|
}
|