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