...
1 package match
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 type Contains struct {
9 Needle string
10 Not bool
11 }
12
13 func NewContains(needle string, not bool) Contains {
14 return Contains{needle, not}
15 }
16
17 func (self Contains) Match(s string) bool {
18 return strings.Contains(s, self.Needle) != self.Not
19 }
20
21 func (self Contains) Index(s string) (int, []int) {
22 var offset int
23
24 idx := strings.Index(s, self.Needle)
25
26 if !self.Not {
27 if idx == -1 {
28 return -1, nil
29 }
30
31 offset = idx + len(self.Needle)
32 if len(s) <= offset {
33 return 0, []int{offset}
34 }
35 s = s[offset:]
36 } else if idx != -1 {
37 s = s[:idx]
38 }
39
40 segments := acquireSegments(len(s) + 1)
41 for i := range s {
42 segments = append(segments, offset+i)
43 }
44
45 return 0, append(segments, offset+len(s))
46 }
47
48 func (self Contains) Len() int {
49 return lenNo
50 }
51
52 func (self Contains) String() string {
53 var not string
54 if self.Not {
55 not = "!"
56 }
57 return fmt.Sprintf("<contains:%s[%s]>", not, self.Needle)
58 }
59
View as plain text