glob/match/text.go

53 lines
719 B
Go
Raw Permalink Normal View History

2016-01-14 21:32:02 +03:00
package match
import (
"fmt"
"strings"
"unicode/utf8"
)
// raw represents raw string to match
type Text struct {
2018-02-16 17:36:02 +03:00
s string
runes int
bytes int
seg []int
2016-01-14 21:32:02 +03:00
}
func NewText(s string) Text {
return Text{
2018-02-16 17:36:02 +03:00
s: s,
runes: utf8.RuneCountInString(s),
bytes: len(s),
seg: []int{len(s)},
2016-01-14 21:32:02 +03:00
}
}
2018-02-16 17:36:02 +03:00
func (t Text) Match(s string) bool {
return t.s == s
2016-01-14 21:32:02 +03:00
}
2018-02-16 17:36:02 +03:00
func (t Text) Index(s string) (int, []int) {
i := strings.Index(s, t.s)
if i == -1 {
2016-02-02 22:03:37 +03:00
return -1, nil
2016-01-14 21:32:02 +03:00
}
2018-02-16 17:36:02 +03:00
return i, t.seg
}
func (t Text) MinLen() int {
return t.runes
}
func (t Text) BytesCount() int {
return t.bytes
}
2016-01-14 21:32:02 +03:00
2018-02-16 17:36:02 +03:00
func (t Text) RunesCount() int {
return t.runes
2016-01-14 21:32:02 +03:00
}
2018-02-16 17:36:02 +03:00
func (t Text) String() string {
return fmt.Sprintf("<text:`%v`>", t.s)
2016-01-14 21:32:02 +03:00
}