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

58 lines
733B

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