Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-05-08 13:12:41 +08:00
parent 71f841a1f5
commit fe436bf2c0
2 changed files with 124 additions and 125 deletions

5
gin.go
View File

@ -253,6 +253,7 @@ func (engine *Engine) rebuild405Handlers() {
}
func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
// log.Println(method, path)
assert1(path[0] == '/', "path must begin with '/'")
assert1(method != "", "HTTP method can not be empty")
assert1(len(handlers) > 0, "there must be at least one handler")
@ -269,13 +270,15 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
root.addRoute(path, handlers)
// Update maxParams
if paramsCount := countParams(path); paramsCount+varsCount > root.maxParams {
// log.Println("countParams(path):", countParams(path))
if paramsCount := countParams(path); paramsCount+varsCount > engine.maxParams {
engine.maxParams = paramsCount + varsCount
}
// Lazy-init paramsPool alloc func
if engine.paramsPool.New == nil && engine.maxParams > 0 {
engine.paramsPool.New = func() interface{} {
// log.Println("engine.maxParams:", engine.maxParams)
ps := make(Params, 0, engine.maxParams)
return &ps
}

12
tree.go
View File

@ -5,7 +5,6 @@
package gin
import (
"log"
"strings"
"unicode"
"unicode/utf8"
@ -424,8 +423,8 @@ func (n *node) getValue(path string, params func() *Params, unescape bool) (valu
walk: // Outer loop for walking the tree
for {
prefix := n.path
if len(path) > len(prefix) && path[:len(prefix)] == prefix {
if len(path) > len(prefix) {
if path[:len(prefix)] == prefix {
path = path[len(prefix):]
// If this node does not have a wildcard (param or catchAll)
// child, we can just look up the next child node and continue
@ -478,8 +477,6 @@ walk: // Outer loop for walking the tree
}
// Expand slice within preallocated capacity
i := len(*value.params)
log.Println(i)
log.Println(*value.params)
*value.params = (*value.params)[:i+1]
// val := path[:end]
// if unescape {
@ -489,7 +486,7 @@ walk: // Outer loop for walking the tree
// }
(*value.params)[i] = Param{
Key: n.path[1:],
Value: path,
Value: path[:end],
}
}
@ -562,8 +559,7 @@ walk: // Outer loop for walking the tree
panic("invalid node type")
}
}
if path == prefix {
} else if path == prefix {
// We should have reached the node containing the handle.
// Check if this node has a handle registered.
if value.handlers = n.handlers; value.handlers != nil {