Skip to content

Commit

Permalink
0 3
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Oct 5, 2017
1 parent a6132c8 commit 5605872
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion bench_test.go
Expand Up @@ -26,7 +26,7 @@ func benchRequest(b *testing.B, router http.Handler, r *http.Request) {
func BenchmarkRouterStatic(b *testing.B) {
router := New()
router.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {}, "GET,HEAD")
r, _ := http.NewRequest("GET", "/hello/", nil)
r, _ := http.NewRequest("GET", "/hello", nil)
benchRequest(b, router, r)
}

Expand Down
50 changes: 27 additions & 23 deletions trie.go
Expand Up @@ -80,29 +80,7 @@ func (t *Trie) Set(path []string, handler http.Handler, method, version string)

// Get returns a node
func (t *Trie) Get(path, version string) (*Trie, string, string, bool) {
var key string
if path != "" {
for i := 0; i < len(path); i++ {
if path[i] == '/' && i > 0 {
key = path[1:i]
path = path[i:]
break
} else if path[i] == '*' {
key = "*"
path = ""
break
}
}
} else {
key = "/"
}
if key == "" && len(path) > 0 {
key = path[1:]
path = ""
}
if path == "/" {
path = ""
}
key, path := t.SplitPath(path)
// search the key recursively on the tree
if node, ok := t.contains(key, version); ok {
if path == "" {
Expand All @@ -113,3 +91,29 @@ func (t *Trie) Get(path, version string) (*Trie, string, string, bool) {
// if not fount check for catchall or regex
return t, key, path, false
}

func (t *Trie) SplitPath(path string) (string, string) {
var key string
if path == "" {
return key, path
}
for i := 0; i < len(path); i++ {
if path[i] == '/' {
if i == 0 {
return t.SplitPath(path[1:])
}
if i > 0 {
key = path[:i]
path = path[i:]
if key == "" && path != "" {
return t.SplitPath(path)
}
if path == "/" {
return key, ""
}
return key, path
}
}
}
return path, ""
}
5 changes: 1 addition & 4 deletions violetear.go
Expand Up @@ -183,10 +183,7 @@ func (r *Router) dispatch(node *Trie, key, path, method, version string, leaf bo
params = Params{}
}
params.Add(n.path, key)
if path == "" {
return r.checkMethod(n, method), params
}
node, key, path, leaf := n.Get(n.path+path, version)
node, key, path, leaf := node.Get(n.path+path, version)
return r.dispatch(node, key, path, method, version, leaf, params)
}
}
Expand Down

0 comments on commit 5605872

Please sign in to comment.