Skip to content

Commit

Permalink
improve latest fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Oct 21, 2018
2 parents a0bb721 + 712688b commit 7dab685
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (n *Node) addChild(s string, child *Node) {

func (n *Node) getChild(s string) *Node {
if n.children == nil {
n.children = make(map[string]*Node)
return nil
}

return n.children[s]
Expand Down
16 changes: 12 additions & 4 deletions trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type Trie struct {
// if true then it will handle any path if not other parent wildcard exists,
// so even 404 (on http services) is up to it, see Trie#Insert.
hasRootWildcard bool

hasRootSlash bool
}

// NewTrie returns a new, empty Trie.
Expand Down Expand Up @@ -120,6 +122,10 @@ func (t *Trie) insert(key, tag string, optionalData interface{}, handler http.Ha
input := slowPathSplit(key)

n := t.root
if key == pathSep {
t.hasRootSlash = true
}

var paramKeys []string

for _, s := range input {
Expand Down Expand Up @@ -241,12 +247,14 @@ func (t *Trie) Search(q string, params ParamsSetter) *Node {

if end == 0 || (end == 1 && q[0] == pathSepB) {
// fixes only root wildcard but no / registered at.
if n := t.root.getChild(pathSep); n != nil {
return n
if t.hasRootSlash {
return t.root.getChild(pathSep)
} else if t.hasRootWildcard {
// no need to going through setting parameters, this one has not but it is wildcard.
return t.root.getChild(WildcardParamStart)
}

q = pathSep
//
return nil
}

n := t.root
Expand Down

0 comments on commit 7dab685

Please sign in to comment.