glob/match/list.go

53 lines
810 B
Go
Raw Permalink Normal View History

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
)
2016-01-08 20:14:31 +03:00
type List struct {
2018-02-16 17:36:02 +03:00
rs []rune
not bool
2015-12-24 17:54:54 +03:00
}
2018-02-16 17:36:02 +03:00
func NewList(rs []rune, not bool) List {
return List{rs, not}
2016-02-23 14:46:20 +03:00
}
2018-02-16 17:36:02 +03:00
func (l List) Match(s string) bool {
2016-02-02 22:03:37 +03:00
r, w := utf8.DecodeRuneInString(s)
if len(s) > w {
2018-02-16 17:36:02 +03:00
// Invalid rune.
2016-01-14 21:32:02 +03:00
return false
}
2018-02-16 17:36:02 +03:00
inList := runes.IndexRune(l.rs, r) != -1
return inList == !l.not
2016-01-08 20:14:31 +03:00
}
2015-12-24 17:54:54 +03:00
2018-02-16 17:36:02 +03:00
func (l List) MinLen() int {
return 1
2016-01-09 02:34:41 +03:00
}
2018-11-24 21:47:12 +03:00
func (l List) RunesCount() int {
return 1
}
2018-02-16 17:36:02 +03:00
func (l List) Index(s string) (int, []int) {
2016-01-09 02:34:41 +03:00
for i, r := range s {
2018-02-16 17:36:02 +03:00
if l.not == (runes.IndexRune(l.rs, 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 (l List) String() string {
2016-01-13 01:26:48 +03:00
var not string
2018-02-16 17:36:02 +03:00
if l.not {
2016-01-13 01:26:48 +03:00
not = "!"
}
2018-02-16 17:36:02 +03:00
return fmt.Sprintf("<list:%s[%s]>", not, string(l.rs))
2015-12-24 17:54:54 +03:00
}