WIP
diff --git a/tree.go b/tree.go
index edb7198..73fb22f 100644
--- a/tree.go
+++ b/tree.go
@@ -45,7 +45,7 @@
type node struct {
pfx string // first len(children) byte are indices, rest is prefix
- children []*node
+ children []node
handle Handle
wildChild bool
nType nodeType
@@ -121,7 +121,7 @@
}
}
- n.children = []*node{&child}
+ n.children = []node{child}
// []byte for proper unicode char conversion, see #65
n.pfx = string([]byte{prefix[i]}) + path[:i]
n.handle = nil
@@ -133,7 +133,7 @@
path = path[i:]
if n.wildChild {
- n = n.children[0]
+ n = &n.children[0]
prefix = n.pfx[len(n.children):]
n.priority++
@@ -169,7 +169,7 @@
// slash after param
if n.nType == param && c == '/' && len(n.children) == 1 {
- n = n.children[0]
+ n = &n.children[0]
prefix = n.pfx[len(n.children):]
n.priority++
continue walk
@@ -179,7 +179,7 @@
for i := 0; i < len(n.children); i++ {
if c == n.pfx[i] {
i = n.incrementChildPrio(i)
- n = n.children[i]
+ n = &n.children[i]
prefix = n.pfx[len(n.children):]
continue walk
}
@@ -189,10 +189,10 @@
if c != ':' && c != '*' {
// []byte for proper unicode char conversion, see #65
n.pfx = n.pfx[:len(n.children)] + string([]byte{c}) + n.pfx[len(n.children):]
- child := &node{
+ n.children = append(n.children, node{
maxParams: numParams,
- }
- n.children = append(n.children, child)
+ })
+ child := &n.children[len(n.children)-1]
n.incrementChildPrio(len(n.children) - 1)
n = child
}
@@ -257,13 +257,12 @@
n.pfx = ":" + n.pfx
}
- child := &node{
+ n.children = []node{node{
nType: param,
maxParams: numParams,
- }
- n.children = []*node{child}
+ }}
n.wildChild = true
- n = child
+ n = &n.children[0]
n.priority++
numParams--
@@ -273,12 +272,11 @@
n.pfx = "/" + path[offset:end]
offset = end
- child := &node{
+ n.children = []node{node{
maxParams: numParams,
priority: 1,
- }
- n.children = []*node{child}
- n = child
+ }}
+ n = &n.children[0]
}
} else { // catchAll
@@ -299,25 +297,23 @@
n.pfx = string(path[i]) + path[offset:i]
// first node: catchAll node with empty path
- child := &node{
+ n.children = []node{node{
pfx: "/",
wildChild: true,
nType: catchAll,
maxParams: 1,
- }
- n.children = []*node{child}
- n = child
+ }}
+ n = &n.children[0]
n.priority++
// second node: node holding the variable
- child = &node{
+ n.children = []node{node{
pfx: path[i:],
nType: catchAll,
maxParams: 1,
handle: handle,
priority: 1,
- }
- n.children = []*node{child}
+ }}
return
}
@@ -333,7 +329,7 @@
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
// made if a handle exists with an extra (without the) trailing slash for the
// given path.
-func (n *node) getValue(path string) (handle Handle, p Params, tsr bool) {
+func (n node) getValue(path string) (handle Handle, p Params, tsr bool) {
walk: // outer loop for walking the tree
for {
prefix := n.pfx[len(n.children):]
@@ -512,7 +508,7 @@
for i, max := 0, len(n.children); i < max; i++ {
if n.pfx[i] == rb[0] {
// continue with child node
- n = n.children[i]
+ n = &n.children[i]
prefix = n.pfx[len(n.children):]
continue walk
}
@@ -565,7 +561,7 @@
// uppercase matches
if n.pfx[i] == c {
// continue with child node
- n = n.children[i]
+ n = &n.children[i]
prefix = n.pfx[len(n.children):]
continue walk
}
@@ -578,7 +574,7 @@
return ciPath, (fixTrailingSlash && path == "/" && n.handle != nil)
}
- n = n.children[0]
+ n = &n.children[0]
switch n.nType {
case param:
// find param end (either '/' or path end)
@@ -594,7 +590,7 @@
if k < len(path) {
if len(n.children) > 0 {
// continue with child node
- n = n.children[0]
+ n = &n.children[0]
prefix = n.pfx[len(n.children):]
path = path[k:]
continue
@@ -612,7 +608,7 @@
} else if fixTrailingSlash && len(n.children) == 1 {
// No handle found. Check if a handle for this path + a
// trailing slash exists
- n = n.children[0]
+ n = &n.children[0]
if n.pfx[len(n.children):] == "/" && n.handle != nil {
return append(ciPath, '/'), true
}
@@ -637,7 +633,7 @@
if fixTrailingSlash {
for i, max := 0, len(n.children); i < max; i++ {
if n.pfx[i] == '/' {
- n = n.children[i]
+ n = &n.children[i]
if (len(n.pfx)-len(n.children) == 1 && n.handle != nil) ||
(n.nType == catchAll && n.children[0].handle != nil) {
return append(ciPath, '/'), true
diff --git a/tree_test.go b/tree_test.go
index c83c961..10c2059 100644
--- a/tree_test.go
+++ b/tree_test.go
@@ -21,7 +21,7 @@
prefix += " "
}
for _, child := range n.children {
- printChildren(child, prefix)
+ printChildren(&child, prefix)
}
}
@@ -67,7 +67,7 @@
func checkPriorities(t *testing.T, n *node) uint32 {
var prio uint32
for i := range n.children {
- prio += checkPriorities(t, n.children[i])
+ prio += checkPriorities(t, &n.children[i])
}
if n.handle != nil {
@@ -87,7 +87,7 @@
func checkMaxParams(t *testing.T, n *node) uint8 {
var maxParams uint8
for i := range n.children {
- params := checkMaxParams(t, n.children[i])
+ params := checkMaxParams(t, &n.children[i])
if params > maxParams {
maxParams = params
}