mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-20 11:55:18 +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%
58 lines
733 B
Go
58 lines
733 B
Go
package match
|
|
|
|
import (
|
|
"fmt"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
type Min struct {
|
|
Limit int
|
|
}
|
|
|
|
func NewMin(l int) Min {
|
|
return Min{l}
|
|
}
|
|
|
|
func (self Min) Match(s string) bool {
|
|
var l int
|
|
for range s {
|
|
l += 1
|
|
if l >= self.Limit {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (self Min) Index(s string) (int, []int) {
|
|
var count int
|
|
|
|
c := len(s) - self.Limit + 1
|
|
if c <= 0 {
|
|
return -1, nil
|
|
}
|
|
|
|
segments := acquireSegments(c)
|
|
for i, r := range s {
|
|
count++
|
|
if count >= self.Limit {
|
|
segments = append(segments, i+utf8.RuneLen(r))
|
|
}
|
|
}
|
|
|
|
if len(segments) == 0 {
|
|
return -1, nil
|
|
}
|
|
|
|
return 0, segments
|
|
}
|
|
|
|
func (self Min) Len() int {
|
|
return lenNo
|
|
}
|
|
|
|
func (self Min) String() string {
|
|
return fmt.Sprintf("<min:%d>", self.Limit)
|
|
}
|