diff --git a/internal/matcher/location.go b/internal/matcher/location.go index 42203e9..07a12dd 100644 --- a/internal/matcher/location.go +++ b/internal/matcher/location.go @@ -198,8 +198,8 @@ func (e *LocationEngine) AddNamed(name string, handler fasthttp.RequestHandler) // // 返回值: // - *MatchResult: 匹配结果,无匹配时返回 nil -func (e *LocationEngine) Match(path string) *MatchResult { - if m, ok := e.exactMatchers[path]; ok { +func (e *LocationEngine) Match(path []byte) *MatchResult { + if m, ok := e.exactMatchers[string(path)]; ok { return m.Result() } @@ -209,10 +209,11 @@ func (e *LocationEngine) Match(path string) *MatchResult { } ReleaseMatchResult(prefixPriorityResult) + pathStr := string(path) for _, m := range e.regexMatchers { - if m.Match(path) { + if m.Match(pathStr) { result := m.Result() - result.Captures = m.GetCaptures(path) + result.Captures = m.GetCaptures(pathStr) return result } } diff --git a/internal/matcher/radix.go b/internal/matcher/radix.go index dbf8d46..3606ea5 100644 --- a/internal/matcher/radix.go +++ b/internal/matcher/radix.go @@ -9,6 +9,7 @@ package matcher import ( + "bytes" "errors" "strings" "sync" @@ -224,7 +225,7 @@ func (t *RadixTree) insertNode(parent *RadixNode, node *RadixNode, path string, // // 返回值: // - *MatchResult: 最长前缀匹配结果,无匹配时返回 nil -func (t *RadixTree) FindLongestPrefix(path string) *MatchResult { +func (t *RadixTree) FindLongestPrefix(path []byte) *MatchResult { bestNode := t.searchLongest(t.root, path, nil) if bestNode == nil { return nil @@ -238,12 +239,12 @@ func (t *RadixTree) FindLongestPrefix(path string) *MatchResult { return result } -func (t *RadixTree) searchLongest(node *RadixNode, path string, bestNode *RadixNode) *RadixNode { - if node == nil || path == "" { +func (t *RadixTree) searchLongest(node *RadixNode, path []byte, bestNode *RadixNode) *RadixNode { + if node == nil || len(path) == 0 { return bestNode } - if !strings.HasPrefix(path, node.prefix) { + if !bytes.HasPrefix(path, []byte(node.prefix)) { return bestNode }