7days-golang
7days-golang copied to clipboard
gee-web-fix:修复trie前缀路径树中查找node模糊匹配的bug
gee-web
bug
添加"/hello/:name"
路径后,再添加"/hello/b/c"
路径,匹配"/hello/bvv/c"
该路径时,本来不存在的路径会被匹配为"/hello/b/c"
fix
现将 matchChild
函数改为完全强匹配,在插入路径时,只进行强匹配来找到要添加的节点位置
// Find a matching child node
func (n *node) matchChild(part string) *node {
for _, child := range n.children {
if child.part == part {
return child
}
}
return nil
}
现将matchChildren
函数修改,只匹配一个路径,若是有强匹配,则返回强匹配的,没有则返回模糊匹配,在查找路径时先进行强匹配,没有强匹配则尝试返回弱匹配
func (n *node) search(parts []string, height int) *node {
if len(parts) == height || strings.HasPrefix(n.part, "*") {
if n.pattern == "" {
return nil
}
return n
}
part := parts[height]
child := n.matchChildren(part)
if child != nil {
result := child.search(parts, height+1)
if result != nil {
return result
}
}
return nil
}
// Find all eligible child nodes
func (n *node) matchChildren(part string) *node {
//var nodeMatch *node
var nodeWild *node
for _, child := range n.children {
if child.part == part {
return child
} else if child.isWild {
nodeWild = child
}
}
return nodeWild
}