mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-13 00:36:28 +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%
49 lines
773 B
Go
49 lines
773 B
Go
package match
|
|
|
|
import (
|
|
"fmt"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
type Range struct {
|
|
Lo, Hi rune
|
|
Not bool
|
|
}
|
|
|
|
func NewRange(lo, hi rune, not bool) Range {
|
|
return Range{lo, hi, not}
|
|
}
|
|
|
|
func (self Range) Len() int {
|
|
return lenOne
|
|
}
|
|
|
|
func (self Range) Match(s string) bool {
|
|
r, w := utf8.DecodeRuneInString(s)
|
|
if len(s) > w {
|
|
return false
|
|
}
|
|
|
|
inRange := r >= self.Lo && r <= self.Hi
|
|
|
|
return inRange == !self.Not
|
|
}
|
|
|
|
func (self Range) Index(s string) (int, []int) {
|
|
for i, r := range s {
|
|
if self.Not != (r >= self.Lo && r <= self.Hi) {
|
|
return i, segmentsByRuneLength[utf8.RuneLen(r)]
|
|
}
|
|
}
|
|
|
|
return -1, nil
|
|
}
|
|
|
|
func (self Range) String() string {
|
|
var not string
|
|
if self.Not {
|
|
not = "!"
|
|
}
|
|
return fmt.Sprintf("<range:%s[%s,%s]>", not, string(self.Lo), string(self.Hi))
|
|
}
|