glob/match/range.go

63 lines
1.1 KiB
Go
Raw Normal View History

2016-01-08 20:14:31 +03:00
package match
import (
"fmt"
2016-01-09 02:34:41 +03:00
"unicode/utf8"
2019-02-06 23:43:38 +03:00
"github.com/gobwas/glob/internal/debug"
2016-01-08 20:14:31 +03:00
)
type Range struct {
Lo, Hi rune
Not bool
}
func NewRange(lo, hi rune, not bool) Range {
return Range{lo, hi, not}
}
2018-02-16 17:36:02 +03:00
func (self Range) MinLen() int {
return 1
2016-01-09 02:34:41 +03:00
}
2016-01-08 20:14:31 +03:00
2018-11-24 21:47:12 +03:00
func (self Range) RunesCount() int {
return 1
}
2019-02-06 23:43:38 +03:00
func (self Range) Match(s string) (ok bool) {
if debug.Enabled {
done := debug.Matching("range", s)
defer func() { done(ok) }()
}
2016-01-09 02:34:41 +03:00
r, w := utf8.DecodeRuneInString(s)
if len(s) > w {
2016-01-08 20:14:31 +03:00
return false
}
2016-01-09 02:34:41 +03:00
inRange := r >= self.Lo && r <= self.Hi
2016-01-08 20:14:31 +03:00
return inRange == !self.Not
}
2019-02-06 23:43:38 +03:00
func (self Range) Index(s string) (index int, segments []int) {
if debug.Enabled {
done := debug.Indexing("range", s)
defer func() { done(index, segments) }()
}
2016-01-09 02:34:41 +03:00
for i, r := range s {
2016-01-08 20:14:31 +03:00
if self.Not != (r >= self.Lo && r <= self.Hi) {
2016-02-23 14:46:20 +03:00
return i, segmentsByRuneLength[utf8.RuneLen(r)]
2016-01-08 20:14:31 +03:00
}
}
2016-01-09 02:34:41 +03:00
return -1, nil
2016-01-08 20:14:31 +03:00
}
func (self Range) String() string {
2016-01-13 01:26:48 +03:00
var not string
if self.Not {
not = "!"
}
return fmt.Sprintf("<range:%s[%s,%s]>", not, string(self.Lo), string(self.Hi))
2016-01-08 20:14:31 +03:00
}