Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gee-web-fix:修复trie前缀路径树中查找node模糊匹配的bug #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cryacry
Copy link

@cryacry cryacry commented May 30, 2024

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
}

bug:添加"/hello/:name"路径后再添加"/hello/b/c"路径,匹配"/hello/bvv/c"该路径时,本来不存在的路径会被匹配为"/hello/b/c"
@cryacry cryacry changed the title fix:修复trie前缀路径树中查找node模糊匹配的bug gee-web-fix:修复trie前缀路径树中查找node模糊匹配的bug May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant