Repository: vapor/vapor
File: Sources/Routing/TrieRouter.swift (Swift)

Code before:
                // find the child node matching this constant
                if let node = current.findConstantNode(at: s) {
                    current = node
                } else {
                    // if no child node matches this constant,
                    // we must create a new one
                    let new = ConstantNode(constant: s)
                    current.constantChildren.append(new)
                    current = new
                }
            case .parameter(let p):
                // there can only ever be one parameter node at
                // a given node in the tree, so always create a new one
                let new = ParameterNode(parameter: p)
                current.parameterChild = new
                current = new
            }
        }

        // set the resolved nodes responder
        current.responder = route.responder
    }

    /// See Router.route()
    public func route(request: Request, parameters: inout ParameterBag) -> Responder? {
        let path = [request.method.string] + request.uri.path.split(separator: "/").map(String.init)
        
        // always start at the root node
        var current: TrieRouterNode = root

        // traverse the constant path supplied
        var iterator = path.makeIterator()
        while let path = iterator.next() {
            if let node = current.findConstantNode(at: path) {
                // if we find a constant route path that matches this component,
                // then we should use it.
                current = node
            } else if let node = current.parameterChild {
                // if no constant routes were found that match the path, but
                // a dynamic parameter child was found, we can use it
                let lazy = LazyParameter(type: node.parameter, value: path)
                parameters.parameters.append(lazy)
                current = node
            } else {
                // no constant routes were found, and this node doesn't have
                // a dynamic parameter child. so no match.
                return nil
            }
        }

        // return the resolved responder if there hasn't

Review comment (suggestion):
I wonder if there's any way we can store the request URI as an array from the HTTP parser? It could be more convenient this way and making routing faster. It would also help make URL decoding more obvious (if the path contains an encoded `/` for example, it would be able to be decoded)

Code after:
        // traverse the path components supplied
        var iterator = path.makeIterator()
        while let path = iterator.next() {
            switch path {
            case .constant(let s):
                // find the child node matching this constant
                if let node = current.findConstantNode(at: s) {
                    current = node
                } else {
                    // if no child node matches this constant,
                    // we must create a new one
                    let new = ConstantNode(constant: s)
                    current.constantChildren.append(new)
                    current = new
                }
            case .parameter(let p):
                // there can only ever be one parameter node at
                // a given node in the tree, so always create a new one
                let new = ParameterNode(parameter: p)
                current.parameterChild = new
                current = new
            }
        }

        // set the resolved nodes responder
        current.responder = route.responder
    }

    /// See Router.route()
    public func route(request: Request) -> Responder? {
        let path = [request.method.string] + request.uri.path.split(separator: "/").map(String.init)
        
        // always start at the root node
        var current: TrieRouterNode = root

        // traverse the constant path supplied
        var iterator = path.makeIterator()
        while let path = iterator.next() {
            if let node = current.findConstantNode(at: path) {
                // if we find a constant route path that matches this component,
                // then we should use it.
                current = node
            } else if let node = current.parameterChild {
                // if no constant routes were found that match the path, but
                // a dynamic parameter child was found, we can use it
                let lazy = LazyParameter(type: node.parameter, value: path)
                request.parameters.parameters.append(lazy)
                current = node
            } else {
                // no constant routes were found, and this node doesn't have
                // a dynamic parameter child. so no match.