glob/match/row.go

63 lines
878 B
Go
Raw Normal View History

2016-01-09 02:34:41 +03:00
package match
import (
"fmt"
2016-01-14 18:29:13 +03:00
"unicode/utf8"
2016-01-09 02:34:41 +03:00
)
type Row struct {
Matchers Matchers
2016-01-14 18:29:13 +03:00
Length int
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
func (self Row) matchAll(s string) bool {
2016-01-09 02:34:41 +03:00
var idx int
for _, m := range self.Matchers {
l := m.Len()
if !m.Match(s[idx : idx+l]) {
return false
}
idx += l
}
return true
}
2016-01-14 18:29:13 +03:00
func (self Row) Match(s string) bool {
if utf8.RuneCountInString(s) < self.Length {
return false
2016-01-09 02:34:41 +03:00
}
2016-01-14 18:29:13 +03:00
return self.matchAll(s)
}
func (self Row) Len() (l int) {
return self.Length
2016-01-09 02:34:41 +03:00
}
func (self Row) Index(s string) (int, []int) {
2016-01-14 18:29:13 +03:00
l := utf8.RuneCountInString(s)
if l < self.Length {
return -1, nil
}
2016-01-09 02:34:41 +03:00
for i := range s {
sub := s[i:]
2016-01-14 18:29:13 +03:00
if self.matchAll(sub) {
return i, []int{self.Length}
}
l -= 1
if l < self.Length {
return -1, nil
2016-01-09 02:34:41 +03:00
}
}
return -1, nil
}
func (self Row) String() string {
2016-01-14 18:29:13 +03:00
return fmt.Sprintf("<row_%d:[%s]>", self.Length, self.Matchers)
2016-01-09 02:34:41 +03:00
}