From 03f1a7389afeadf9aa2b1d62a38e5cb0a2a7e60d Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sat, 13 Dec 2025 01:17:52 +0100 Subject: Initial implementation --- Sources/WmapParser/DependencyParser.swift | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Sources/WmapParser/DependencyParser.swift (limited to 'Sources/WmapParser/DependencyParser.swift') diff --git a/Sources/WmapParser/DependencyParser.swift b/Sources/WmapParser/DependencyParser.swift new file mode 100644 index 0000000..9964d5a --- /dev/null +++ b/Sources/WmapParser/DependencyParser.swift @@ -0,0 +1,50 @@ +/// Looks for -- or ->. If found, returns where this arrow starts. +@inline(__always) +func hasDependency(_ bytes: T, _ start: Int, _ end: Int) -> Int? +where T.Element == UInt8, T.Index == Int { + var index = start + while index < end { + if bytes[index] == kOpenParenthesis { return nil } + if bytes[index] == kDash && index + 1 < end { + let next = bytes[index + 1] + if next == kGreaterThan || next == kDash { return index } + } + index += 1 + } + return nil +} + +/// Finds a dependency and whether it's directed. It assumes we previously +/// checked for the location of our separator to speed things up. +func parseDependency(_ bytes: T, _ start: Int, _ end: Int, _ separatorIndex: Int) + -> Dependency? +where T.Element == UInt8, T.Index == Int { + guard let isDirected = extractIsDirected(bytes, separatorIndex, end) else { + return nil + } + + guard let fromComponent = extractTrimmedString(bytes, start, separatorIndex) else { + return nil + } + + guard let toComponent = extractTrimmedString(bytes, separatorIndex + 2, end) else { + return nil + } + + return Dependency(fromComponent: fromComponent, toComponent: toComponent, isDirected: isDirected) +} + +/// Checks the second character of the separator to see if it's directed. +/// It assumes we already checked for -- or ->, so it only checks for +/// Whether it's greaterThan or not. +@inline(__always) +private func extractIsDirected(_ bytes: T, _ separatorIndex: Int, _ end: Int) + -> Bool? +where T.Element == UInt8, T.Index == Int { + let followingCharacter = separatorIndex + 1 + guard followingCharacter < end else { return nil } + if bytes[followingCharacter] == kGreaterThan { + return true + } + return false +} -- cgit