| 1 |
package match |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
"unicode/utf8" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
type Text struct { |
| 11 |
Str string |
| 12 |
RunesLength int |
| 13 |
BytesLength int |
| 14 |
Segments []int |
| 15 |
} |
| 16 |
|
| 17 |
func NewText(s string) Text { |
| 18 |
return Text{ |
| 19 |
Str: s, |
| 20 |
RunesLength: utf8.RuneCountInString(s), |
| 21 |
BytesLength: len(s), |
| 22 |
Segments: []int{len(s)}, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func (self Text) Match(s string) bool { |
| 27 |
return self.Str == s |
| 28 |
} |
| 29 |
|
| 30 |
func (self Text) Len() int { |
| 31 |
return self.RunesLength |
| 32 |
} |
| 33 |
|
| 34 |
func (self Text) Index(s string) (int, []int) { |
| 35 |
index := strings.Index(s, self.Str) |
| 36 |
if index == -1 { |
| 37 |
return -1, nil |
| 38 |
} |
| 39 |
|
| 40 |
return index, self.Segments |
| 41 |
} |
| 42 |
|
| 43 |
func (self Text) String() string { |
| 44 |
return fmt.Sprintf("<text:`%v`>", self.Str) |
| 45 |
} |