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 Min struct {
|
2018-02-16 17:36:02 +03:00
|
|
|
n int
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func NewMin(n int) Min {
|
|
|
|
return Min{n}
|
2016-02-24 12:36:15 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (m Min) Match(s string) bool {
|
|
|
|
var n int
|
2016-08-15 07:02:39 +03:00
|
|
|
for range s {
|
2018-02-16 17:36:02 +03:00
|
|
|
n += 1
|
|
|
|
if n >= m.n {
|
2016-01-14 18:29:13 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (m Min) Index(s string) (int, []int) {
|
2016-01-12 14:06:59 +03:00
|
|
|
var count int
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
c := len(s) - m.n + 1
|
2016-02-24 12:36:15 +03:00
|
|
|
if c <= 0 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
segments := acquireSegments(c)
|
2016-01-12 14:06:59 +03:00
|
|
|
for i, r := range s {
|
|
|
|
count++
|
2018-02-16 17:36:02 +03:00
|
|
|
if count >= m.n {
|
2016-01-12 14:06:59 +03:00
|
|
|
segments = append(segments, i+utf8.RuneLen(r))
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 17:29:41 +03:00
|
|
|
if len(segments) == 0 {
|
2016-02-02 22:03:37 +03:00
|
|
|
return -1, nil
|
|
|
|
}
|
2016-01-12 14:06:59 +03:00
|
|
|
return 0, segments
|
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (m Min) MinLen() int {
|
|
|
|
return m.n
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (m Min) String() string {
|
|
|
|
return fmt.Sprintf("<min:%d>", m.n)
|
2016-01-08 20:14:31 +03:00
|
|
|
}
|