2016-01-08 20:14:31 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Contains struct {
|
2018-02-16 17:36:02 +03:00
|
|
|
s string
|
|
|
|
not bool
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func NewContains(needle string) Contains {
|
|
|
|
return Contains{needle, false}
|
2016-02-24 12:36:15 +03:00
|
|
|
}
|
|
|
|
|
2018-11-24 21:47:12 +03:00
|
|
|
func NewNotContains(needle string) Contains {
|
|
|
|
return Contains{needle, true}
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (c Contains) Match(s string) bool {
|
|
|
|
return strings.Contains(s, c.s) != c.not
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (c Contains) Index(s string) (int, []int) {
|
2016-02-02 22:03:37 +03:00
|
|
|
var offset int
|
2016-01-12 14:06:59 +03:00
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
idx := strings.Index(s, c.s)
|
2016-01-12 14:06:59 +03:00
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
if !c.not {
|
2016-01-12 14:06:59 +03:00
|
|
|
if idx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
offset = idx + len(c.s)
|
2016-01-12 14:06:59 +03:00
|
|
|
if len(s) <= offset {
|
2016-02-05 17:29:41 +03:00
|
|
|
return 0, []int{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-23 00:26:06 +03:00
|
|
|
segments := acquireSegments(len(s) + 1)
|
2016-08-15 07:02:39 +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
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (c Contains) MinLen() int {
|
|
|
|
return 0
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (c Contains) String() string {
|
2016-01-13 01:26:48 +03:00
|
|
|
var not string
|
2018-02-16 17:36:02 +03:00
|
|
|
if c.not {
|
2016-01-13 01:26:48 +03:00
|
|
|
not = "!"
|
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
return fmt.Sprintf("<contains:%s[%s]>", not, c.s)
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|