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

49 lines
773B

  1. package match
  2. import (
  3. "fmt"
  4. "unicode/utf8"
  5. )
  6. type Range struct {
  7. Lo, Hi rune
  8. Not bool
  9. }
  10. func NewRange(lo, hi rune, not bool) Range {
  11. return Range{lo, hi, not}
  12. }
  13. func (self Range) Len() int {
  14. return lenOne
  15. }
  16. func (self Range) Match(s string) bool {
  17. r, w := utf8.DecodeRuneInString(s)
  18. if len(s) > w {
  19. return false
  20. }
  21. inRange := r >= self.Lo && r <= self.Hi
  22. return inRange == !self.Not
  23. }
  24. func (self Range) Index(s string) (int, []int) {
  25. for i, r := range s {
  26. if self.Not != (r >= self.Lo && r <= self.Hi) {
  27. return i, segmentsByRuneLength[utf8.RuneLen(r)]
  28. }
  29. }
  30. return -1, nil
  31. }
  32. func (self Range) String() string {
  33. var not string
  34. if self.Not {
  35. not = "!"
  36. }
  37. return fmt.Sprintf("<range:%s[%s,%s]>", not, string(self.Lo), string(self.Hi))
  38. }
上海开阖软件有限公司 沪ICP备12045867号-1