diff --git a/Sources/Hummingbird/Router/RouterResponder.swift b/Sources/Hummingbird/Router/RouterResponder.swift index 745616d02..83945ce33 100644 --- a/Sources/Hummingbird/Router/RouterResponder.swift +++ b/Sources/Hummingbird/Router/RouterResponder.swift @@ -15,7 +15,7 @@ import NIOCore public struct RouterResponder: HTTPResponder { - let trie: BinaryTrie> + let trie: RouterTrie> let notFoundResponder: any HTTPResponder let options: RouterOptions @@ -25,7 +25,7 @@ public struct RouterResponder: HTTPResponder { options: RouterOptions, notFoundResponder: any HTTPResponder ) { - self.trie = BinaryTrie(base: trie) + self.trie = RouterTrie(base: trie) self.options = options self.notFoundResponder = notFoundResponder } diff --git a/Sources/Hummingbird/Router/BinaryTrie/BinaryTrie.swift b/Sources/Hummingbird/Router/Trie/RouterTrie.swift similarity index 79% rename from Sources/Hummingbird/Router/BinaryTrie/BinaryTrie.swift rename to Sources/Hummingbird/Router/Trie/RouterTrie.swift index 1597215d7..d2fd61c25 100644 --- a/Sources/Hummingbird/Router/BinaryTrie/BinaryTrie.swift +++ b/Sources/Hummingbird/Router/Trie/RouterTrie.swift @@ -12,31 +12,27 @@ // //===----------------------------------------------------------------------===// -enum BinaryTrieTokenKind: UInt8 { +enum TrieTokenKind: UInt8 { case null = 0 case path, capture, prefixCapture, suffixCapture, wildcard, prefixWildcard, suffixWildcard, recursiveWildcard case deadEnd } -struct BinaryTrieNode: Sendable { +struct TrieNode: Sendable { let valueIndex: UInt16 - let token: BinaryTrieTokenKind + let token: TrieTokenKind var nextSiblingNodeIndex: UInt16 var constant: UInt16? var parameter: UInt16? - - /// How many bytes a serialized BinaryTrieNode uses - static let serializedSize = 7 } struct Trie: Sendable { - var nodes = [BinaryTrieNode]() + var nodes = [TrieNode]() var parameters = [Substring]() var constants = [Substring]() } -@_spi(Internal) public final class BinaryTrie: Sendable { - typealias Integer = UInt8 +@_spi(Internal) public final class RouterTrie: Sendable { let trie: Trie let values: [Value?] @@ -51,7 +47,7 @@ struct Trie: Sendable { ) trie.nodes.append( - BinaryTrieNode( + TrieNode( valueIndex: 0, token: .deadEnd, nextSiblingNodeIndex: .max diff --git a/Sources/Hummingbird/Router/BinaryTrie/BinaryTrie+resolve.swift b/Sources/Hummingbird/Router/Trie/Trie+resolve.swift similarity index 97% rename from Sources/Hummingbird/Router/BinaryTrie/BinaryTrie+resolve.swift rename to Sources/Hummingbird/Router/Trie/Trie+resolve.swift index 6833aebaa..caf931558 100644 --- a/Sources/Hummingbird/Router/BinaryTrie/BinaryTrie+resolve.swift +++ b/Sources/Hummingbird/Router/Trie/Trie+resolve.swift @@ -14,7 +14,7 @@ import NIOCore -extension BinaryTrie { +extension RouterTrie { /// Resolve a path to a `Value` if available @_spi(Internal) public func resolve(_ path: String) -> (value: Value, parameters: Parameters)? { let pathComponents = path.split(separator: "/", omittingEmptySubsequences: true) @@ -65,7 +65,7 @@ extension BinaryTrie { _ component: Substring, atNodeIndex nodeIndex: inout Int, parameters: inout Parameters - ) -> BinaryTrieNode { + ) -> TrieNode { while nodeIndex < trie.nodes.count { let node = trie.nodes[nodeIndex] let result = self.matchComponent( @@ -83,7 +83,7 @@ extension BinaryTrie { } // should never get here - return BinaryTrieNode(valueIndex: 0, token: .deadEnd, nextSiblingNodeIndex: .max) + return TrieNode(valueIndex: 0, token: .deadEnd, nextSiblingNodeIndex: .max) } private enum MatchResult { @@ -92,7 +92,7 @@ extension BinaryTrie { private func matchComponent( _ component: Substring, - node: BinaryTrieNode, + node: TrieNode, parameters: inout Parameters ) -> MatchResult { switch node.token { diff --git a/Sources/Hummingbird/Router/BinaryTrie/BinaryTrie+serialize.swift b/Sources/Hummingbird/Router/Trie/Trie+serialize.swift similarity index 98% rename from Sources/Hummingbird/Router/BinaryTrie/BinaryTrie+serialize.swift rename to Sources/Hummingbird/Router/Trie/Trie+serialize.swift index e37c2cebe..9c9883b7d 100644 --- a/Sources/Hummingbird/Router/BinaryTrie/BinaryTrie+serialize.swift +++ b/Sources/Hummingbird/Router/Trie/Trie+serialize.swift @@ -14,7 +14,7 @@ import NIOCore -extension BinaryTrie { +extension RouterTrie { static func serialize( _ node: RouterPathTrieBuilder.Node, trie: inout Trie, @@ -24,7 +24,7 @@ extension BinaryTrie { let valueIndex = UInt16(values.count) values.append(node.value) - let token: BinaryTrieTokenKind + let token: TrieTokenKind let constant: UInt16? let parameter: UInt16? @@ -89,7 +89,7 @@ extension BinaryTrie { let nodeIndex = trie.nodes.count trie.nodes.append( - BinaryTrieNode( + TrieNode( valueIndex: valueIndex, token: token, nextSiblingNodeIndex: .max, diff --git a/Sources/Hummingbird/Router/TrieRouter.swift b/Sources/Hummingbird/Router/TrieRouter.swift index d805bef12..400eb5d89 100644 --- a/Sources/Hummingbird/Router/TrieRouter.swift +++ b/Sources/Hummingbird/Router/TrieRouter.swift @@ -40,7 +40,7 @@ import HummingbirdCore } } - @_spi(Internal) public func build() -> BinaryTrie { + @_spi(Internal) public func build() -> RouterTrie { .init(base: self) }