glob/match/prefix_any.go

57 lines
953 B
Go
Raw Normal View History

package match
import (
"fmt"
"strings"
"unicode/utf8"
2018-02-16 17:36:02 +03:00
"github.com/gobwas/glob/util/runes"
)
type PrefixAny struct {
2018-02-16 17:36:02 +03:00
s string
sep []rune
minLen int
}
func NewPrefixAny(s string, sep []rune) PrefixAny {
2018-02-16 17:36:02 +03:00
return PrefixAny{s, sep, utf8.RuneCountInString(s)}
}
2018-02-16 17:36:02 +03:00
func (p PrefixAny) Index(s string) (int, []int) {
idx := strings.Index(s, p.s)
if idx == -1 {
return -1, nil
}
2018-02-16 17:36:02 +03:00
n := len(p.s)
sub := s[idx+n:]
2018-02-16 17:36:02 +03:00
i := runes.IndexAnyRune(sub, p.sep)
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-16 17:36:02 +03:00
func (p PrefixAny) Match(s string) bool {
if !strings.HasPrefix(s, p.s) {
return false
}
2018-02-16 17:36:02 +03:00
return runes.IndexAnyRune(s[len(p.s):], p.sep) == -1
}
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))
}