addRoute adds a node with the given handle to the path. Not concurrency-safe!
(path string, handlers HandlersChain)
| 133 | // addRoute adds a node with the given handle to the path. |
| 134 | // Not concurrency-safe! |
| 135 | func (n *node) addRoute(path string, handlers HandlersChain) { |
| 136 | fullPath := path |
| 137 | n.priority++ |
| 138 | |
| 139 | // Empty tree |
| 140 | if len(n.path) == 0 && len(n.children) == 0 { |
| 141 | n.insertChild(path, fullPath, handlers) |
| 142 | n.nType = root |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | parentFullPathIndex := 0 |
| 147 | |
| 148 | walk: |
| 149 | for { |
| 150 | // Find the longest common prefix. |
| 151 | // This also implies that the common prefix contains no ':' or '*' |
| 152 | // since the existing key can't contain those chars. |
| 153 | i := longestCommonPrefix(path, n.path) |
| 154 | |
| 155 | // Split edge |
| 156 | if i < len(n.path) { |
| 157 | child := node{ |
| 158 | path: n.path[i:], |
| 159 | wildChild: n.wildChild, |
| 160 | nType: static, |
| 161 | indices: n.indices, |
| 162 | children: n.children, |
| 163 | handlers: n.handlers, |
| 164 | priority: n.priority - 1, |
| 165 | fullPath: n.fullPath, |
| 166 | } |
| 167 | |
| 168 | n.children = []*node{&child} |
| 169 | // []byte for proper unicode char conversion, see #65 |
| 170 | n.indices = bytesconv.BytesToString([]byte{n.path[i]}) |
| 171 | n.path = path[:i] |
| 172 | n.handlers = nil |
| 173 | n.wildChild = false |
| 174 | n.fullPath = fullPath[:parentFullPathIndex+i] |
| 175 | } |
| 176 | |
| 177 | // Make new node a child of this node |
| 178 | if i < len(path) { |
| 179 | path = path[i:] |
| 180 | c := path[0] |
| 181 | |
| 182 | // '/' after param |
| 183 | if n.nType == param && c == '/' && len(n.children) == 1 { |
| 184 | parentFullPathIndex += len(n.path) |
| 185 | n = n.children[0] |
| 186 | n.priority++ |
| 187 | continue walk |
| 188 | } |
| 189 | |
| 190 | // Check if a child with the next path byte exists |
| 191 | for i, max_ := 0, len(n.indices); i < max_; i++ { |
| 192 | if c == n.indices[i] { |