@@ -120,7 +120,7 @@ func (n *node) addPath(path string) (*node, error) {
120
120
return nil , fmt .Errorf ("* or : in middle of path component %s" , path )
121
121
}
122
122
123
- // Do we have an existing node that starts with the same letter ?
123
+ // Do we have an existing node that starts with the same byte ?
124
124
for i , index := range n .staticIndices {
125
125
if c == index {
126
126
// Yes. Split it based on the common prefix of the existing
@@ -132,7 +132,7 @@ func (n *node) addPath(path string) (*node, error) {
132
132
}
133
133
}
134
134
135
- // No existing node starting with this letter , so create it.
135
+ // No existing node starting with this byte , so create it.
136
136
child := & node {path : thisToken }
137
137
138
138
if n .staticIndices == nil {
@@ -158,16 +158,8 @@ func (n *node) splitCommonPrefix(existingNodeIndex int, path string) (*node, int
158
158
return childNode , len (childNode .path )
159
159
}
160
160
161
- var i int
162
161
// Find the length of the common prefix of the child node and the new path.
163
- for i = range childNode .path {
164
- if i == len (path ) {
165
- break
166
- }
167
- if path [i ] != childNode .path [i ] {
168
- break
169
- }
170
- }
162
+ i := commonPrefixLen (childNode .path , path )
171
163
172
164
commonPrefix := path [0 :i ]
173
165
childNode .path = childNode .path [i :]
@@ -177,7 +169,7 @@ func (n *node) splitCommonPrefix(existingNodeIndex int, path string) (*node, int
177
169
newNode := & node {
178
170
path : commonPrefix ,
179
171
priority : childNode .priority ,
180
- // Index is the first letter of the non-common part of the path.
172
+ // Index is the first byte of the non-common part of the path.
181
173
staticIndices : []byte {childNode .path [0 ]},
182
174
staticChild : []* node {childNode },
183
175
}
@@ -186,6 +178,14 @@ func (n *node) splitCommonPrefix(existingNodeIndex int, path string) (*node, int
186
178
return newNode , i
187
179
}
188
180
181
+ func commonPrefixLen (x , y string ) int {
182
+ n := 0
183
+ for n < len (x ) && n < len (y ) && x [n ] == y [n ] {
184
+ n ++
185
+ }
186
+ return n
187
+ }
188
+
189
189
func (n * node ) search (path string , m Matcher ) (found * node , params []string , value interface {}) {
190
190
pathLen := len (path )
191
191
if pathLen == 0 {
0 commit comments