glob/match/contains.go

54 lines
846 B
Go
Raw Permalink Normal View History

2016-01-08 20:14:31 +03:00
package match
import (
"fmt"
"strings"
)
type Contains struct {
Needle string
Not bool
}
func (self Contains) Match(s string) bool {
return strings.Contains(s, self.Needle) != self.Not
}
2016-02-02 22:03:37 +03:00
func (self Contains) Index(s string, segments []int) (int, []int) {
var offset int
2016-01-12 14:06:59 +03:00
idx := strings.Index(s, self.Needle)
if !self.Not {
if idx == -1 {
return -1, nil
}
offset = idx + len(self.Needle)
if len(s) <= offset {
2016-02-02 22:03:37 +03:00
return 0, append(segments, offset)
2016-01-12 14:06:59 +03:00
}
2016-02-02 22:03:37 +03:00
s = s[offset:]
} else if idx != -1 {
s = s[:idx]
2016-01-12 14:06:59 +03:00
}
2016-02-02 22:03:37 +03:00
for i, _ := range s {
2016-01-12 14:06:59 +03:00
segments = append(segments, offset+i)
}
2016-02-02 22:03:37 +03:00
return 0, append(segments, offset+len(s))
2016-01-12 14:06:59 +03:00
}
2016-01-09 02:34:41 +03:00
func (self Contains) Len() int {
2016-01-14 18:29:13 +03:00
return lenNo
2016-01-09 02:34:41 +03:00
}
2016-01-08 20:14:31 +03:00
func (self Contains) String() string {
2016-01-13 01:26:48 +03:00
var not string
if self.Not {
not = "!"
}
return fmt.Sprintf("<contains:%s[%s]>", not, self.Needle)
2016-01-08 20:14:31 +03:00
}