glob/match/text.go

44 lines
703 B
Go
Raw 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 {
Str string
RunesLength int
BytesLength int
}
func NewText(s string) Text {
return Text{
Str: s,
RunesLength: utf8.RuneCountInString(s),
BytesLength: len(s),
}
}
func (self Text) Match(s string) bool {
return self.Str == s
}
func (self Text) Len() int {
return self.RunesLength
}
2016-02-02 22:03:37 +03:00
func (self Text) Index(s string, segments []int) (int, []int) {
index := strings.Index(s, self.Str)
2016-01-14 21:32:02 +03:00
if index == -1 {
2016-02-02 22:03:37 +03:00
return -1, nil
2016-01-14 21:32:02 +03:00
}
2016-02-02 22:03:37 +03:00
return index, append(segments, self.BytesLength)
2016-01-14 21:32:02 +03:00
}
func (self Text) String() string {
return fmt.Sprintf("<text:%s>", self.Str)
}