2015-12-24 17:54:54 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-09 02:34:41 +03:00
|
|
|
"strings"
|
2016-01-12 14:06:59 +03:00
|
|
|
"unicode/utf8"
|
2015-12-24 17:54:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Prefix struct {
|
|
|
|
Prefix string
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:36:15 +03:00
|
|
|
func NewPrefix(p string) Prefix {
|
|
|
|
return Prefix{p}
|
|
|
|
}
|
|
|
|
|
2016-02-05 17:29:41 +03:00
|
|
|
func (self Prefix) Index(s string) (int, []int) {
|
2016-01-12 14:06:59 +03:00
|
|
|
idx := strings.Index(s, self.Prefix)
|
|
|
|
if idx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
length := len(self.Prefix)
|
|
|
|
var sub string
|
|
|
|
if len(s) > idx+length {
|
|
|
|
sub = s[idx+length:]
|
|
|
|
} else {
|
|
|
|
sub = ""
|
|
|
|
}
|
|
|
|
|
2016-02-23 00:26:06 +03:00
|
|
|
segments := acquireSegments(len(sub) + 1)
|
2016-01-12 14:06:59 +03:00
|
|
|
segments = append(segments, length)
|
|
|
|
for i, r := range sub {
|
|
|
|
segments = append(segments, length+i+utf8.RuneLen(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, segments
|
|
|
|
}
|
|
|
|
|
2016-01-09 02:34:41 +03:00
|
|
|
func (self Prefix) 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 Prefix) Match(s string) bool {
|
|
|
|
return strings.HasPrefix(s, self.Prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self Prefix) String() string {
|
2016-01-13 01:26:48 +03:00
|
|
|
return fmt.Sprintf("<prefix:%s>", self.Prefix)
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|