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 {
|
2018-02-16 17:36:02 +03:00
|
|
|
s string
|
|
|
|
minSize int
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 12:36:15 +03:00
|
|
|
func NewPrefix(p string) Prefix {
|
2018-02-16 17:36:02 +03:00
|
|
|
return Prefix{
|
|
|
|
s: p,
|
|
|
|
minSize: utf8.RuneCountInString(p),
|
|
|
|
}
|
2016-02-24 12:36:15 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p Prefix) Index(s string) (int, []int) {
|
|
|
|
idx := strings.Index(s, p.s)
|
2016-01-12 14:06:59 +03:00
|
|
|
if idx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
length := len(p.s)
|
2016-01-12 14:06:59 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p Prefix) MinLen() int {
|
|
|
|
return p.minSize
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p Prefix) Match(s string) bool {
|
|
|
|
return strings.HasPrefix(s, p.s)
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p Prefix) String() string {
|
|
|
|
return fmt.Sprintf("<prefix:%s>", p.s)
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|