Skip to content

Commit

Permalink
convert loop to map lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
issmirnov committed Mar 11, 2017
1 parent f815861 commit cdca9d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 14 additions & 15 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,23 @@ func expand(c *gabs.Container, token *list.Element, res *bytes.Buffer) {
}
res.WriteString("/")
children, _ := c.ChildrenMap()
for key, child := range children {
if key == token.Value {
p, skip, err := getPrefix(child)
if err != nil {
fmt.Println(err.Error())
child, ok := children[token.Value.(string)]
if ok {
p, skip, err := getPrefix(child)
if err != nil {
fmt.Println(err.Error())
return
}
res.WriteString(p)
if skip {
token = token.Next()
if token == nil {
return
}
res.WriteString(p)
if skip {
token = token.Next()
if token == nil {
return
}
res.WriteString(token.Value.(string))
}
expand(child, token.Next(), res)
return
res.WriteString(token.Value.(string))
}
expand(child, token.Next(), res)
return
}
// handle base case if no keys matched
res.WriteString(token.Value.(string))
Expand Down
4 changes: 2 additions & 2 deletions web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func BenchmarkIndexHandler(b *testing.B) {
context := &context{config: c}
appHandler := &ctxWrapper{context, IndexHandler}
handler := http.Handler(appHandler)
req, _ := http.NewRequest("GET", "/p", nil)
req.Host = "sd"
req, _ := http.NewRequest("GET", "/z", nil)
req.Host = "g"
rr := httptest.NewRecorder()
for n := 0; n < b.N; n++ {
handler.ServeHTTP(rr, req)
Expand Down

0 comments on commit cdca9d5

Please sign in to comment.