2018-02-09 00:02:47 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
"github.com/gobwas/glob/util/runes"
|
2018-02-09 00:02:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type PrefixAny struct {
|
2018-02-16 17:36:02 +03:00
|
|
|
s string
|
|
|
|
sep []rune
|
|
|
|
minLen int
|
2018-02-09 00:02:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPrefixAny(s string, sep []rune) PrefixAny {
|
2018-02-16 17:36:02 +03:00
|
|
|
return PrefixAny{s, sep, utf8.RuneCountInString(s)}
|
2018-02-09 00:02:47 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p PrefixAny) Index(s string) (int, []int) {
|
|
|
|
idx := strings.Index(s, p.s)
|
2018-02-09 00:02:47 +03:00
|
|
|
if idx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
n := len(p.s)
|
2018-02-09 00:02:47 +03:00
|
|
|
sub := s[idx+n:]
|
2018-02-16 17:36:02 +03:00
|
|
|
i := runes.IndexAnyRune(sub, p.sep)
|
2018-02-09 00:02:47 +03:00
|
|
|
if i > -1 {
|
|
|
|
sub = sub[:i]
|
|
|
|
}
|
|
|
|
|
|
|
|
seg := acquireSegments(len(sub) + 1)
|
|
|
|
seg = append(seg, n)
|
|
|
|
for i, r := range sub {
|
|
|
|
seg = append(seg, n+i+utf8.RuneLen(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, seg
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p PrefixAny) MinLen() int {
|
|
|
|
return p.minLen
|
2018-02-09 00:02:47 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p PrefixAny) Match(s string) bool {
|
|
|
|
if !strings.HasPrefix(s, p.s) {
|
2018-02-09 00:02:47 +03:00
|
|
|
return false
|
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
return runes.IndexAnyRune(s[len(p.s):], p.sep) == -1
|
2018-02-09 00:02:47 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (p PrefixAny) String() string {
|
|
|
|
return fmt.Sprintf("<prefix_any:%s![%s]>", p.s, string(p.sep))
|
2018-02-09 00:02:47 +03:00
|
|
|
}
|