...
1 package match
2
3 import (
4 "fmt"
5 "unicode/utf8"
6 )
7
8 type Range struct {
9 Lo, Hi rune
10 Not bool
11 }
12
13 func NewRange(lo, hi rune, not bool) Range {
14 return Range{lo, hi, not}
15 }
16
17 func (self Range) Len() int {
18 return lenOne
19 }
20
21 func (self Range) Match(s string) bool {
22 r, w := utf8.DecodeRuneInString(s)
23 if len(s) > w {
24 return false
25 }
26
27 inRange := r >= self.Lo && r <= self.Hi
28
29 return inRange == !self.Not
30 }
31
32 func (self Range) Index(s string) (int, []int) {
33 for i, r := range s {
34 if self.Not != (r >= self.Lo && r <= self.Hi) {
35 return i, segmentsByRuneLength[utf8.RuneLen(r)]
36 }
37 }
38
39 return -1, nil
40 }
41
42 func (self Range) String() string {
43 var not string
44 if self.Not {
45 not = "!"
46 }
47 return fmt.Sprintf("<range:%s[%s,%s]>", not, string(self.Lo), string(self.Hi))
48 }
49
View as plain text