2015-12-24 17:54:54 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-09 02:34:41 +03:00
|
|
|
"unicode/utf8"
|
2018-02-16 17:36:02 +03:00
|
|
|
|
|
|
|
"github.com/gobwas/glob/util/runes"
|
2015-12-24 17:54:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// single represents ?
|
|
|
|
type Single struct {
|
2018-02-16 17:36:02 +03:00
|
|
|
sep []rune
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 12:36:15 +03:00
|
|
|
func NewSingle(s []rune) Single {
|
|
|
|
return Single{s}
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Single) Match(v string) bool {
|
|
|
|
r, w := utf8.DecodeRuneInString(v)
|
|
|
|
if len(v) > w {
|
2016-01-15 19:50:12 +03:00
|
|
|
return false
|
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
return runes.IndexRune(s.sep, r) == -1
|
|
|
|
}
|
2016-01-15 19:50:12 +03:00
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Single) MinLen() int {
|
|
|
|
return 1
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Single) RunesCount() int {
|
|
|
|
return 1
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Single) Index(v string) (int, []int) {
|
|
|
|
for i, r := range v {
|
|
|
|
if runes.IndexRune(s.sep, r) == -1 {
|
2016-02-23 14:46:20 +03:00
|
|
|
return i, segmentsByRuneLength[utf8.RuneLen(r)]
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
2016-01-09 02:34:41 +03:00
|
|
|
return -1, nil
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Single) String() string {
|
2019-02-06 23:43:38 +03:00
|
|
|
if len(s.sep) == 0 {
|
|
|
|
return "<single>"
|
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
return fmt.Sprintf("<single:![%s]>", string(s.sep))
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|