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 {
|
2016-01-14 21:32:02 +03:00
|
|
|
Matchers Matchers
|
|
|
|
RunesLength 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 {
|
2016-01-14 21:32:02 +03:00
|
|
|
if utf8.RuneCountInString(s) < self.RunesLength {
|
2016-01-14 18:29:13 +03:00
|
|
|
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) {
|
2016-01-14 21:32:02 +03:00
|
|
|
return self.RunesLength
|
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)
|
2016-01-14 21:32:02 +03:00
|
|
|
if l < self.RunesLength {
|
2016-01-14 18:29:13 +03:00
|
|
|
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) {
|
2016-01-14 21:32:02 +03:00
|
|
|
return i, []int{self.RunesLength}
|
2016-01-14 18:29:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
l -= 1
|
2016-01-14 21:32:02 +03:00
|
|
|
if l < self.RunesLength {
|
2016-01-14 18:29:13 +03:00
|
|
|
return -1, nil
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self Row) String() string {
|
2016-01-14 21:32:02 +03:00
|
|
|
return fmt.Sprintf("<row_%d:[%s]>", self.RunesLength, self.Matchers)
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|