glob/match/suffix_any.go

46 lines
797 B
Go
Raw Normal View History

package match
import (
"fmt"
"strings"
2018-02-16 17:36:02 +03:00
"unicode/utf8"
2018-02-16 17:36:02 +03:00
"github.com/gobwas/glob/util/runes"
)
type SuffixAny struct {
2018-02-16 17:36:02 +03:00
s string
sep []rune
minLen int
}
func NewSuffixAny(s string, sep []rune) SuffixAny {
2018-02-16 17:36:02 +03:00
return SuffixAny{s, sep, utf8.RuneCountInString(s)}
}
2018-02-16 17:36:02 +03:00
func (s SuffixAny) Index(v string) (int, []int) {
idx := strings.Index(v, s.s)
if idx == -1 {
return -1, nil
}
2018-02-16 17:36:02 +03:00
i := runes.LastIndexAnyRune(v[:idx], s.sep) + 1
2018-02-16 17:36:02 +03:00
return i, []int{idx + len(s.s) - i}
}
2018-02-16 17:36:02 +03:00
func (s SuffixAny) MinLen() int {
return s.minLen
}
2018-02-16 17:36:02 +03:00
func (s SuffixAny) Match(v string) bool {
if !strings.HasSuffix(v, s.s) {
return false
}
2018-02-16 17:36:02 +03:00
return runes.IndexAnyRune(v[:len(v)-len(s.s)], s.sep) == -1
}
2018-02-16 17:36:02 +03:00
func (s SuffixAny) String() string {
return fmt.Sprintf("<suffix_any:![%s]%s>", string(s.sep), s.s)
}