glob/match/range.go

45 lines
690 B
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"
2016-01-08 20:14:31 +03:00
)
type Range struct {
Lo, Hi rune
Not bool
}
func (self Range) Kind() Kind {
return KindRange
}
2016-01-09 02:34:41 +03:00
func (self Range) Len() int {
return 1
}
2016-01-08 20:14:31 +03:00
2016-01-09 02:34:41 +03:00
func (self Range) Match(s string) bool {
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
}
2016-01-09 02:34:41 +03:00
func (self Range) Index(s string) (int, []int) {
for i, r := range s {
2016-01-08 20:14:31 +03:00
if self.Not != (r >= self.Lo && r <= self.Hi) {
2016-01-09 02:34:41 +03:00
return i, []int{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-09 02:34:41 +03:00
return fmt.Sprintf("[range:%s-%s(%t)]", string(self.Lo), string(self.Hi), self.Not)
2016-01-08 20:14:31 +03:00
}