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

78 lines
1.1KB

  1. package match
  2. import (
  3. "fmt"
  4. )
  5. type Row struct {
  6. Matchers Matchers
  7. RunesLength int
  8. Segments []int
  9. }
  10. func NewRow(len int, m ...Matcher) Row {
  11. return Row{
  12. Matchers: Matchers(m),
  13. RunesLength: len,
  14. Segments: []int{len},
  15. }
  16. }
  17. func (self Row) matchAll(s string) bool {
  18. var idx int
  19. for _, m := range self.Matchers {
  20. length := m.Len()
  21. var next, i int
  22. for next = range s[idx:] {
  23. i++
  24. if i == length {
  25. break
  26. }
  27. }
  28. if i < length || !m.Match(s[idx:idx+next+1]) {
  29. return false
  30. }
  31. idx += next + 1
  32. }
  33. return true
  34. }
  35. func (self Row) lenOk(s string) bool {
  36. var i int
  37. for range s {
  38. i++
  39. if i > self.RunesLength {
  40. return false
  41. }
  42. }
  43. return self.RunesLength == i
  44. }
  45. func (self Row) Match(s string) bool {
  46. return self.lenOk(s) && self.matchAll(s)
  47. }
  48. func (self Row) Len() (l int) {
  49. return self.RunesLength
  50. }
  51. func (self Row) Index(s string) (int, []int) {
  52. for i := range s {
  53. if len(s[i:]) < self.RunesLength {
  54. break
  55. }
  56. if self.matchAll(s[i:]) {
  57. return i, self.Segments
  58. }
  59. }
  60. return -1, nil
  61. }
  62. func (self Row) String() string {
  63. return fmt.Sprintf("<row_%d:[%s]>", self.RunesLength, self.Matchers)
  64. }
上海开阖软件有限公司 沪ICP备12045867号-1