glob/match/list.go

53 lines
798 B
Go
Raw Normal View History

2015-12-24 17:54:54 +03:00
package match
import (
"fmt"
2016-02-02 22:03:37 +03:00
"github.com/gobwas/glob/runes"
2016-01-09 02:34:41 +03:00
"unicode/utf8"
2015-12-24 17:54:54 +03:00
)
2016-01-08 20:14:31 +03:00
type List struct {
2016-02-02 22:03:37 +03:00
List []rune
2015-12-24 17:54:54 +03:00
Not bool
}
2016-02-23 14:46:20 +03:00
func NewList(list []rune, not bool) List {
return List{
List: list,
Not: not,
}
}
2016-01-08 20:14:31 +03:00
func (self List) Match(s string) bool {
2016-02-02 22:03:37 +03:00
r, w := utf8.DecodeRuneInString(s)
if len(s) > w {
2016-01-14 21:32:02 +03:00
return false
}
2016-02-02 22:03:37 +03:00
inList := runes.IndexRune(self.List, r) != -1
2016-01-08 20:14:31 +03:00
return inList == !self.Not
}
2015-12-24 17:54:54 +03:00
2016-01-09 02:34:41 +03:00
func (self List) Len() int {
2016-01-14 18:29:13 +03:00
return lenOne
2016-01-09 02:34:41 +03:00
}
2016-02-05 17:29:41 +03:00
func (self List) Index(s string) (int, []int) {
2016-01-09 02:34:41 +03:00
for i, r := range s {
2016-02-02 22:03:37 +03:00
if self.Not == (runes.IndexRune(self.List, 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
}
2016-01-08 20:14:31 +03:00
func (self List) String() string {
2016-01-13 01:26:48 +03:00
var not string
if self.Not {
not = "!"
}
return fmt.Sprintf("<list:%s[%s]>", not, self.List)
2015-12-24 17:54:54 +03:00
}