本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
717B

  1. package match
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode/utf8"
  6. )
  7. // raw represents raw string to match
  8. type Text struct {
  9. Str string
  10. RunesLength int
  11. BytesLength int
  12. Segments []int
  13. }
  14. func NewText(s string) Text {
  15. return Text{
  16. Str: s,
  17. RunesLength: utf8.RuneCountInString(s),
  18. BytesLength: len(s),
  19. Segments: []int{len(s)},
  20. }
  21. }
  22. func (self Text) Match(s string) bool {
  23. return self.Str == s
  24. }
  25. func (self Text) Len() int {
  26. return self.RunesLength
  27. }
  28. func (self Text) Index(s string) (int, []int) {
  29. index := strings.Index(s, self.Str)
  30. if index == -1 {
  31. return -1, nil
  32. }
  33. return index, self.Segments
  34. }
  35. func (self Text) String() string {
  36. return fmt.Sprintf("<text:`%v`>", self.Str)
  37. }
上海开阖软件有限公司 沪ICP备12045867号-1