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
|
2016-02-23 14:46:20 +03:00
|
|
|
Segments []int
|
2016-01-14 21:32:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewText(s string) Text {
|
|
|
|
return Text{
|
|
|
|
Str: s,
|
|
|
|
RunesLength: utf8.RuneCountInString(s),
|
|
|
|
BytesLength: len(s),
|
2016-02-23 14:46:20 +03:00
|
|
|
Segments: []int{len(s)},
|
2016-01-14 21:32:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self Text) Match(s string) bool {
|
|
|
|
return self.Str == s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self Text) Len() int {
|
|
|
|
return self.RunesLength
|
|
|
|
}
|
|
|
|
|
2016-02-05 17:29:41 +03:00
|
|
|
func (self Text) Index(s string) (int, []int) {
|
2016-02-02 22:03:37 +03:00
|
|
|
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-23 14:46:20 +03:00
|
|
|
return index, self.Segments
|
2016-01-14 21:32:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self Text) String() string {
|
|
|
|
return fmt.Sprintf("<text:%s>", self.Str)
|
|
|
|
}
|