本站源代码
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.

51 lines
810B

  1. package match
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode/utf8"
  6. )
  7. type Prefix struct {
  8. Prefix string
  9. }
  10. func NewPrefix(p string) Prefix {
  11. return Prefix{p}
  12. }
  13. func (self Prefix) Index(s string) (int, []int) {
  14. idx := strings.Index(s, self.Prefix)
  15. if idx == -1 {
  16. return -1, nil
  17. }
  18. length := len(self.Prefix)
  19. var sub string
  20. if len(s) > idx+length {
  21. sub = s[idx+length:]
  22. } else {
  23. sub = ""
  24. }
  25. segments := acquireSegments(len(sub) + 1)
  26. segments = append(segments, length)
  27. for i, r := range sub {
  28. segments = append(segments, length+i+utf8.RuneLen(r))
  29. }
  30. return idx, segments
  31. }
  32. func (self Prefix) Len() int {
  33. return lenNo
  34. }
  35. func (self Prefix) Match(s string) bool {
  36. return strings.HasPrefix(s, self.Prefix)
  37. }
  38. func (self Prefix) String() string {
  39. return fmt.Sprintf("<prefix:%s>", self.Prefix)
  40. }
上海开阖软件有限公司 沪ICP备12045867号-1