Skip to content

Commit

Permalink
rune
Browse files Browse the repository at this point in the history
  • Loading branch information
nbari committed Oct 4, 2017
1 parent a225aa9 commit 7422335
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func benchRequest(b *testing.B, router http.Handler, r *http.Request) {
}
}

func BenchmarkRouterStatic(b *testing.B) {
// func BenchmarkRouterStatic(b *testing.B) {
func BenchmarkXX(b *testing.B) {
router := New()
router.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {}, "GET,HEAD")
r, _ := http.NewRequest("GET", "/hello", nil)
Expand Down
22 changes: 22 additions & 0 deletions trie.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package violetear

import (
"bytes"
"errors"
"net/http"
"strings"
Expand Down Expand Up @@ -97,3 +98,24 @@ func (t *Trie) Get(path []string, version string) (trie *Trie, p []string, leaf

return t, path, false, nil
}

// Split path by "/"
func (t *Trie) Split(path string) []string {
if path == "" {
return []string{"/"}
}
var key bytes.Buffer
for i, rune := range path {
if rune == '/' && i > 0 {
return []string{key.String(), path[i:]}
} else if rune == '*' {
return []string{"*"}
} else if rune != '/' {
key.WriteRune(rune)
}
}
if key.Len() > 0 {
return []string{key.String()}
}
return nil
}
2 changes: 1 addition & 1 deletion violetear.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}

// _ path never empty, defaults to ("/")
node, path, leaf, _ := r.routes.Get(r.splitPath(req.URL.Path), version)
node, path, leaf, _ := r.routes.Get(r.routes.Split(req.URL.Path), version)

// h http.Handler
h, p := r.match(node, path, leaf, nil, req.Method, version)
Expand Down

0 comments on commit 7422335

Please sign in to comment.