mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-14 01:04:14 +00: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%
51 lines
810 B
Go
51 lines
810 B
Go
package match
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
type Prefix struct {
|
|
Prefix string
|
|
}
|
|
|
|
func NewPrefix(p string) Prefix {
|
|
return Prefix{p}
|
|
}
|
|
|
|
func (self Prefix) Index(s string) (int, []int) {
|
|
idx := strings.Index(s, self.Prefix)
|
|
if idx == -1 {
|
|
return -1, nil
|
|
}
|
|
|
|
length := len(self.Prefix)
|
|
var sub string
|
|
if len(s) > idx+length {
|
|
sub = s[idx+length:]
|
|
} else {
|
|
sub = ""
|
|
}
|
|
|
|
segments := acquireSegments(len(sub) + 1)
|
|
segments = append(segments, length)
|
|
for i, r := range sub {
|
|
segments = append(segments, length+i+utf8.RuneLen(r))
|
|
}
|
|
|
|
return idx, segments
|
|
}
|
|
|
|
func (self Prefix) Len() int {
|
|
return lenNo
|
|
}
|
|
|
|
func (self Prefix) Match(s string) bool {
|
|
return strings.HasPrefix(s, self.Prefix)
|
|
}
|
|
|
|
func (self Prefix) String() string {
|
|
return fmt.Sprintf("<prefix:%s>", self.Prefix)
|
|
}
|