2016-01-08 20:14:31 +03:00
|
|
|
package match
|
|
|
|
|
2016-01-09 02:34:41 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
2016-01-08 20:14:31 +03:00
|
|
|
|
|
|
|
type Max struct {
|
|
|
|
Limit int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self Max) Match(s string) bool {
|
2016-01-14 18:29:13 +03:00
|
|
|
var l int
|
|
|
|
for range s {
|
|
|
|
l += 1
|
|
|
|
if l > self.Limit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2016-01-12 14:06:59 +03:00
|
|
|
func (self Max) Index(s string) (int, []int) {
|
2016-01-14 18:29:13 +03:00
|
|
|
if !self.Match(s) {
|
2016-01-12 14:06:59 +03:00
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
segments := make([]int, 0, self.Limit+1)
|
|
|
|
segments = append(segments, 0)
|
|
|
|
var count int
|
|
|
|
for i, r := range s {
|
|
|
|
count++
|
|
|
|
if count > self.Limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
segments = append(segments, i+utf8.RuneLen(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, segments
|
|
|
|
}
|
|
|
|
|
2016-01-09 02:34:41 +03:00
|
|
|
func (self Max) Len() int {
|
2016-01-14 18:29:13 +03:00
|
|
|
return lenNo
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self Max) String() string {
|
2016-01-13 01:26:48 +03:00
|
|
|
return fmt.Sprintf("<max:%d>", self.Limit)
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|