glob/match/max.go

49 lines
627 B
Go
Raw Normal View History

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 {
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 NewMax(n int) Max {
return Max{n}
}
2018-02-16 17:36:02 +03:00
func (m Max) 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 false
}
}
return true
2016-01-09 02:34:41 +03:00
}
2018-02-16 17:36:02 +03:00
func (m Max) Index(s string) (int, []int) {
segments := acquireSegments(m.n + 1)
2016-01-12 14:06:59 +03:00
segments = append(segments, 0)
var count int
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
break
}
segments = append(segments, i+utf8.RuneLen(r))
}
return 0, segments
}
2018-02-16 17:36:02 +03:00
func (m Max) MinLen() int {
return 0
2016-01-08 20:14:31 +03:00
}
2018-02-16 17:36:02 +03:00
func (m Max) String() string {
return fmt.Sprintf("<max:%d>", m.n)
2016-01-08 20:14:31 +03:00
}