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

59 lines
943B

  1. package match
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type Contains struct {
  7. Needle string
  8. Not bool
  9. }
  10. func NewContains(needle string, not bool) Contains {
  11. return Contains{needle, not}
  12. }
  13. func (self Contains) Match(s string) bool {
  14. return strings.Contains(s, self.Needle) != self.Not
  15. }
  16. func (self Contains) Index(s string) (int, []int) {
  17. var offset int
  18. idx := strings.Index(s, self.Needle)
  19. if !self.Not {
  20. if idx == -1 {
  21. return -1, nil
  22. }
  23. offset = idx + len(self.Needle)
  24. if len(s) <= offset {
  25. return 0, []int{offset}
  26. }
  27. s = s[offset:]
  28. } else if idx != -1 {
  29. s = s[:idx]
  30. }
  31. segments := acquireSegments(len(s) + 1)
  32. for i := range s {
  33. segments = append(segments, offset+i)
  34. }
  35. return 0, append(segments, offset+len(s))
  36. }
  37. func (self Contains) Len() int {
  38. return lenNo
  39. }
  40. func (self Contains) String() string {
  41. var not string
  42. if self.Not {
  43. not = "!"
  44. }
  45. return fmt.Sprintf("<contains:%s[%s]>", not, self.Needle)
  46. }
上海开阖软件有限公司 沪ICP备12045867号-1