From 8998f1605448272979ab3a94cd8fe4d84e2e02c4 Mon Sep 17 00:00:00 2001 From: heyanfu <1145291570@qq.com> Date: Sat, 22 May 2021 22:56:06 +0800 Subject: [PATCH] optimize nested conditional (#708) Co-authored-by: heyanfu --- core/search/tree.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/core/search/tree.go b/core/search/tree.go index c49754b9..d0dc2230 100644 --- a/core/search/tree.go +++ b/core/search/tree.go @@ -97,22 +97,21 @@ func (t *Tree) next(n *node, route string, result *Result) bool { } for i := range route { - if route[i] == slash { - token := route[:i] - return n.forEach(func(k string, v *node) bool { - if r := match(k, token); r.found { - if t.next(v, route[i+1:], result) { - if r.named { - addParam(result, r.key, r.value) - } - - return true - } - } - - return false - }) + if route[i] != slash { + continue } + token := route[:i] + return n.forEach(func(k string, v *node) bool { + r := match(k, token) + if !r.found || !t.next(v, route[i+1:], result) { + return false + } + if r.named { + addParam(result, r.key, r.value) + } + + return true + }) } return n.forEach(func(k string, v *node) bool {