patricia.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package cmux
  2. import (
  3. "bytes"
  4. "io"
  5. )
  6. // patriciaTree is a simple patricia tree that handles []byte instead of string
  7. // and cannot be changed after instantiation.
  8. type patriciaTree struct {
  9. root *ptNode
  10. }
  11. func newPatriciaTree(b ...[]byte) *patriciaTree {
  12. return &patriciaTree{
  13. root: newNode(b),
  14. }
  15. }
  16. func newPatriciaTreeString(strs ...string) *patriciaTree {
  17. b := make([][]byte, len(strs))
  18. for i, s := range strs {
  19. b[i] = []byte(s)
  20. }
  21. return &patriciaTree{
  22. root: newNode(b),
  23. }
  24. }
  25. func (t *patriciaTree) matchPrefix(r io.Reader) bool {
  26. return t.root.match(r, true)
  27. }
  28. func (t *patriciaTree) match(r io.Reader) bool {
  29. return t.root.match(r, false)
  30. }
  31. type ptNode struct {
  32. prefix []byte
  33. next map[byte]*ptNode
  34. terminal bool
  35. }
  36. func newNode(strs [][]byte) *ptNode {
  37. if len(strs) == 0 {
  38. return &ptNode{
  39. prefix: []byte{},
  40. terminal: true,
  41. }
  42. }
  43. if len(strs) == 1 {
  44. return &ptNode{
  45. prefix: strs[0],
  46. terminal: true,
  47. }
  48. }
  49. p, strs := splitPrefix(strs)
  50. n := &ptNode{
  51. prefix: p,
  52. }
  53. nexts := make(map[byte][][]byte)
  54. for _, s := range strs {
  55. if len(s) == 0 {
  56. n.terminal = true
  57. continue
  58. }
  59. nexts[s[0]] = append(nexts[s[0]], s[1:])
  60. }
  61. n.next = make(map[byte]*ptNode)
  62. for first, rests := range nexts {
  63. n.next[first] = newNode(rests)
  64. }
  65. return n
  66. }
  67. func splitPrefix(bss [][]byte) (prefix []byte, rest [][]byte) {
  68. if len(bss) == 0 || len(bss[0]) == 0 {
  69. return prefix, bss
  70. }
  71. if len(bss) == 1 {
  72. return bss[0], [][]byte{{}}
  73. }
  74. for i := 0; ; i++ {
  75. var cur byte
  76. eq := true
  77. for j, b := range bss {
  78. if len(b) <= i {
  79. eq = false
  80. break
  81. }
  82. if j == 0 {
  83. cur = b[i]
  84. continue
  85. }
  86. if cur != b[i] {
  87. eq = false
  88. break
  89. }
  90. }
  91. if !eq {
  92. break
  93. }
  94. prefix = append(prefix, cur)
  95. }
  96. rest = make([][]byte, 0, len(bss))
  97. for _, b := range bss {
  98. rest = append(rest, b[len(prefix):])
  99. }
  100. return prefix, rest
  101. }
  102. func readBytes(r io.Reader, n int) (b []byte, err error) {
  103. b = make([]byte, n)
  104. o := 0
  105. for o < n {
  106. nr, err := r.Read(b[o:])
  107. if err != nil && err != io.EOF {
  108. return b, err
  109. }
  110. o += nr
  111. if err == io.EOF {
  112. break
  113. }
  114. }
  115. return b[:o], nil
  116. }
  117. func (n *ptNode) match(r io.Reader, prefix bool) bool {
  118. if l := len(n.prefix); l > 0 {
  119. b, err := readBytes(r, l)
  120. if err != nil || len(b) != l || !bytes.Equal(b, n.prefix) {
  121. return false
  122. }
  123. }
  124. if prefix && n.terminal {
  125. return true
  126. }
  127. b := make([]byte, 1)
  128. for {
  129. nr, err := r.Read(b)
  130. if nr != 0 {
  131. break
  132. }
  133. if err == io.EOF {
  134. return n.terminal
  135. }
  136. if err != nil {
  137. return false
  138. }
  139. }
  140. nextN, ok := n.next[b[0]]
  141. return ok && nextN.match(r, prefix)
  142. }