mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-09 01:44:17 +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%
86 lines
1.2 KiB
Go
86 lines
1.2 KiB
Go
package match
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
type AnyOf struct {
|
|
Matchers Matchers
|
|
}
|
|
|
|
func NewAnyOf(m ...Matcher) AnyOf {
|
|
return AnyOf{Matchers(m)}
|
|
}
|
|
|
|
func (self *AnyOf) Add(m Matcher) error {
|
|
self.Matchers = append(self.Matchers, m)
|
|
return nil
|
|
}
|
|
|
|
func (self AnyOf) Match(s string) bool {
|
|
for _, m := range self.Matchers {
|
|
if m.Match(s) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (self AnyOf) Index(s string) (int, []int) {
|
|
index := -1
|
|
|
|
segments := acquireSegments(len(s))
|
|
for _, m := range self.Matchers {
|
|
idx, seg := m.Index(s)
|
|
if idx == -1 {
|
|
continue
|
|
}
|
|
|
|
if index == -1 || idx < index {
|
|
index = idx
|
|
segments = append(segments[:0], seg...)
|
|
continue
|
|
}
|
|
|
|
if idx > index {
|
|
continue
|
|
}
|
|
|
|
// here idx == index
|
|
segments = appendMerge(segments, seg)
|
|
}
|
|
|
|
if index == -1 {
|
|
releaseSegments(segments)
|
|
return -1, nil
|
|
}
|
|
|
|
return index, segments
|
|
}
|
|
|
|
func (self AnyOf) Len() (l int) {
|
|
l = -1
|
|
for _, m := range self.Matchers {
|
|
ml := m.Len()
|
|
if ml == -1 {
|
|
return -1
|
|
}
|
|
|
|
if l == -1 {
|
|
l = ml
|
|
continue
|
|
}
|
|
|
|
if l != ml {
|
|
return -1
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (self AnyOf) String() string {
|
|
return fmt.Sprintf("<any_of:[%s]>", self.Matchers)
|
|
}
|