2015-12-24 17:54:54 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-01-09 02:34:41 +03:00
|
|
|
"strings"
|
2018-02-16 17:36:02 +03:00
|
|
|
"unicode/utf8"
|
2015-12-24 17:54:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Suffix struct {
|
2018-02-16 17:36:02 +03:00
|
|
|
s string
|
|
|
|
minLen int
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 12:36:15 +03:00
|
|
|
func NewSuffix(s string) Suffix {
|
2018-02-16 17:36:02 +03:00
|
|
|
return Suffix{s, utf8.RuneCountInString(s)}
|
2016-01-12 14:06:59 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Suffix) MinLen() int {
|
|
|
|
return s.minLen
|
2016-01-09 02:34:41 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Suffix) Match(v string) bool {
|
|
|
|
return strings.HasSuffix(v, s.s)
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Suffix) Index(v string) (int, []int) {
|
|
|
|
idx := strings.Index(v, s.s)
|
2016-02-24 12:36:15 +03:00
|
|
|
if idx == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
2018-02-16 17:36:02 +03:00
|
|
|
return 0, []int{idx + len(s.s)}
|
2016-02-24 12:36:15 +03:00
|
|
|
}
|
|
|
|
|
2018-02-16 17:36:02 +03:00
|
|
|
func (s Suffix) String() string {
|
|
|
|
return fmt.Sprintf("<suffix:%s>", s.s)
|
2015-12-24 17:54:54 +03:00
|
|
|
}
|