update countParams

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-05-07 15:40:25 +08:00
parent ac41b9b486
commit dc85754c12
2 changed files with 10 additions and 12 deletions

18
tree.go
View File

@ -72,17 +72,15 @@ func longestCommonPrefix(a, b string) int {
return i return i
} }
func countParams(path string) uint8 { func countParams(path string) uint16 {
var n uint var n uint
for i := 0; i < len(path); i++ { for i := range []byte(path) {
if path[i] == ':' || path[i] == '*' { switch path[i] {
case ':', '*':
n++ n++
} }
} }
if n >= 255 { return uint16(n)
return 255
}
return uint8(n)
} }
type nodeType uint8 type nodeType uint8
@ -101,7 +99,7 @@ type node struct {
handlers HandlersChain handlers HandlersChain
priority uint32 priority uint32
nType nodeType nType nodeType
maxParams uint8 maxParams uint16
wildChild bool wildChild bool
// fullPath string // fullPath string
} }
@ -266,7 +264,7 @@ walk:
} }
// Search for a wildcard segment and check the name for invalid characters. // Search for a wildcard segment and check the name for invalid characters.
// Returns -1 as index, if no wildcard war found. // Returns -1 as index, if no wildcard was found.
func findWildcard(path string) (wildcard string, i int, valid bool) { func findWildcard(path string) (wildcard string, i int, valid bool) {
// Find start // Find start
for start, c := range []byte(path) { for start, c := range []byte(path) {
@ -290,7 +288,7 @@ func findWildcard(path string) (wildcard string, i int, valid bool) {
return "", -1, false return "", -1, false
} }
func (n *node) insertChild(numParams uint8, path string, fullPath string, handlers HandlersChain) { func (n *node) insertChild(numParams uint16, path string, fullPath string, handlers HandlersChain) {
for numParams > 0 { for numParams > 0 {
// Find prefix until first wildcard // Find prefix until first wildcard
wildcard, i, valid := findWildcard(path) wildcard, i, valid := findWildcard(path)

View File

@ -76,8 +76,8 @@ func checkPriorities(t *testing.T, n *node) uint32 {
return prio return prio
} }
func checkMaxParams(t *testing.T, n *node) uint8 { func checkMaxParams(t *testing.T, n *node) uint16 {
var maxParams uint8 var maxParams uint16
for i := range n.children { for i := range n.children {
params := checkMaxParams(t, n.children[i]) params := checkMaxParams(t, n.children[i])
if params > maxParams { if params > maxParams {