2016-01-08 20:14:31 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2016-01-12 14:06:59 +03:00
|
|
|
"unicode/utf8"
|
2016-01-08 20:14:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Contains struct {
|
|
|
|
Needle string
|
|
|
|
Not bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self Contains) Match(s string) bool {
|
|
|
|
return strings.Contains(s, self.Needle) != self.Not
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:06:59 +03:00
|
|
|
func (self Contains) Index(s string) (int, []int) {
|
|
|
|
var (
|
|
|
|
sub string
|
|
|
|
offset int
|
|
|
|
)
|
|
|
|
|
|
|
|
idx := strings.Index(s, self.Needle)
|
|
|
|
|
|
|
|
if !self.Not {
|
|
|
|
if idx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = idx + len(self.Needle)
|
|
|
|
|
|
|
|
if len(s) <= offset {
|
|
|
|
return 0, []int{offset}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub = s[offset:]
|
|
|
|
} else {
|
|
|
|
switch idx {
|
|
|
|
case -1:
|
|
|
|
sub = s
|
|
|
|
default:
|
|
|
|
sub = s[:idx]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
segments := make([]int, 0, utf8.RuneCountInString(sub)+1)
|
|
|
|
for i, _ := range sub {
|
|
|
|
segments = append(segments, offset+i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, append(segments, offset+len(sub))
|
|
|
|
}
|
|
|
|
|
2016-01-09 02:34:41 +03:00
|
|
|
func (self Contains) Len() int {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2016-01-08 20:14:31 +03:00
|
|
|
func (self Contains) Kind() Kind {
|
|
|
|
return KindContains
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|