2015-12-24 17:54:54 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-08 20:14:31 +03:00
|
|
|
"strings"
|
2015-12-24 17:54:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type PrefixSuffix struct {
|
|
|
|
Prefix, Suffix string
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:06:59 +03:00
|
|
|
func (self PrefixSuffix) Index(s string) (int, []int) {
|
|
|
|
prefixIdx := strings.Index(s, self.Prefix)
|
|
|
|
if prefixIdx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 19:50:12 +03:00
|
|
|
var resp []int
|
|
|
|
suffixLen := len(self.Suffix)
|
|
|
|
|
|
|
|
if suffixLen > 0 {
|
|
|
|
var segments []int
|
|
|
|
for sub := s[prefixIdx:]; ; {
|
|
|
|
suffixIdx := strings.LastIndex(sub, self.Suffix)
|
|
|
|
if suffixIdx == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
segments = append(segments, suffixIdx+suffixLen)
|
|
|
|
sub = sub[:suffixIdx]
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
|
|
|
|
2016-01-15 19:50:12 +03:00
|
|
|
segLen := len(segments)
|
|
|
|
if segLen == 0 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
2016-01-12 14:06:59 +03:00
|
|
|
|
2016-01-15 19:50:12 +03:00
|
|
|
resp = make([]int, segLen)
|
|
|
|
for i, s := range segments {
|
|
|
|
resp[segLen-i-1] = s
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resp = append(resp, len(s)-prefixIdx)
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return prefixIdx, resp
|
|
|
|
}
|
|
|
|
|
2016-01-09 02:34:41 +03:00
|
|
|
func (self PrefixSuffix) Len() int {
|
2016-01-14 18:29:13 +03:00
|
|
|
return lenNo
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2015-12-24 17:54:54 +03:00
|
|
|
func (self PrefixSuffix) Match(s string) bool {
|
|
|
|
return strings.HasPrefix(s, self.Prefix) && strings.HasSuffix(s, self.Suffix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self PrefixSuffix) String() string {
|
2016-01-13 01:26:48 +03:00
|
|
|
return fmt.Sprintf("<prefix_suffix:[%s,%s]>", self.Prefix, self.Suffix)
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|