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

50 lines
673B

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