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

56 lines
1008B

  1. package match
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode/utf8"
  6. sutil "github.com/gobwas/glob/util/strings"
  7. )
  8. type PrefixAny struct {
  9. Prefix string
  10. Separators []rune
  11. }
  12. func NewPrefixAny(s string, sep []rune) PrefixAny {
  13. return PrefixAny{s, sep}
  14. }
  15. func (self PrefixAny) Index(s string) (int, []int) {
  16. idx := strings.Index(s, self.Prefix)
  17. if idx == -1 {
  18. return -1, nil
  19. }
  20. n := len(self.Prefix)
  21. sub := s[idx+n:]
  22. i := sutil.IndexAnyRunes(sub, self.Separators)
  23. if i > -1 {
  24. sub = sub[:i]
  25. }
  26. seg := acquireSegments(len(sub) + 1)
  27. seg = append(seg, n)
  28. for i, r := range sub {
  29. seg = append(seg, n+i+utf8.RuneLen(r))
  30. }
  31. return idx, seg
  32. }
  33. func (self PrefixAny) Len() int {
  34. return lenNo
  35. }
  36. func (self PrefixAny) Match(s string) bool {
  37. if !strings.HasPrefix(s, self.Prefix) {
  38. return false
  39. }
  40. return sutil.IndexAnyRunes(s[len(self.Prefix):], self.Separators) == -1
  41. }
  42. func (self PrefixAny) String() string {
  43. return fmt.Sprintf("<prefix_any:%s![%s]>", self.Prefix, string(self.Separators))
  44. }
上海开阖软件有限公司 沪ICP备12045867号-1