From 0b90075f76b6f2df77c01c303322323b1395641a Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sun, 14 Dec 2025 16:16:22 +0100 Subject: Use WmapParser namespace --- Sources/WmapParser/ComponentParser.swift | 127 ++++++++++++++++--------------- 1 file changed, 65 insertions(+), 62 deletions(-) (limited to 'Sources/WmapParser/ComponentParser.swift') diff --git a/Sources/WmapParser/ComponentParser.swift b/Sources/WmapParser/ComponentParser.swift index c899637..66979eb 100644 --- a/Sources/WmapParser/ComponentParser.swift +++ b/Sources/WmapParser/ComponentParser.swift @@ -1,78 +1,81 @@ -/// Parses a component with its coordinates. -func parseComponent(_ bytes: T, _ start: Int, _ end: Int) -> Component? -where T.Element == UInt8, T.Index == Int { - guard let (label, parenthesisIndex) = extractComponentLabel(bytes, start, end) else { - return nil - } +extension WmapParser { - guard - let (coordinates, endIndex) = parseCoordinates( - bytes, parenthesisIndex + 1, end) - else { - return nil - } + /// Parses a component with its coordinates. + static func parseComponent(_ bytes: T, _ start: Int, _ end: Int) -> Component? + where T.Element == UInt8, T.Index == Int { + guard let (label, parenthesisIndex) = extractComponentLabel(bytes, start, end) else { + return nil + } - let shape = extractComponentShape(bytes, endIndex, end) + guard + let (coordinates, endIndex) = parseCoordinates( + bytes, parenthesisIndex + 1, end) + else { + return nil + } - return Component(label: label, coordinates: coordinates, shape: shape) -} + let shape = extractComponentShape(bytes, endIndex, end) -/// Looks for a component label by taking the string before the first open -/// parenthesis. -@inline(__always) -private func extractComponentLabel(_ bytes: T, _ start: Int, _ end: Int) -> ( - String, Int -)? -where T.Element == UInt8, T.Index == Int { - var index = start - while index < end { - if bytes[index] == kOpenParenthesis { - guard index > start else { return nil } - guard let label = extractTrimmedString(bytes, start, index) else { return nil } - return (label, index) + return Component(label: label, coordinates: coordinates, shape: shape) } - index += 1 - } - return nil -} -/// Extracts a shape by looking for a shape label in square brackets. -/// If missing, incomplete or invalid, it returns a circle -@inline(__always) -private func extractComponentShape(_ bytes: T, _ start: Int, _ end: Int) -> Shape -where T.Element == UInt8, T.Index == Int { - var index = skipWhitespace(bytes, end, start) + /// Looks for a component label by taking the string before the first open + /// parenthesis. + @inline(__always) + private static func extractComponentLabel(_ bytes: T, _ start: Int, _ end: Int) -> ( + String, Int + )? + where T.Element == UInt8, T.Index == Int { + var index = start + while index < end { + if bytes[index] == kOpenParenthesis { + guard index > start else { return nil } + guard let label = extractTrimmedString(bytes, start, index) else { return nil } + return (label, index) + } + index += 1 + } + return nil + } - guard index < end && bytes[index] == kOpenSquareBracket else { return .circle } + /// Extracts a shape by looking for a shape label in square brackets. + /// If missing, incomplete or invalid, it returns a circle + @inline(__always) + private static func extractComponentShape(_ bytes: T, _ start: Int, _ end: Int) -> Shape + where T.Element == UInt8, T.Index == Int { + var index = skipWhitespace(bytes, end, start) - index += 1 - let shapeStart = index - while index < end && bytes[index] != kCloseSquareBracket { index += 1 } + guard index < end && bytes[index] == kOpenSquareBracket else { return .circle } - guard index > shapeStart && index < end else { return .circle } + index += 1 + let shapeStart = index + while index < end && bytes[index] != kCloseSquareBracket { index += 1 } - let shapeName = bytes[shapeStart.. shapeStart && index < end else { return .circle } -/// Given the contents of a shape label, return the right type. -@inline(__always) -private func parseShapeFromBytes(_ shapeName: T) -> Shape -where T.Element == UInt8, T.Index == Int { - var trimStart = shapeName.startIndex - var trimEnd = shapeName.endIndex + let shapeName = bytes[shapeStart..(_ shapeName: T) -> Shape + where T.Element == UInt8, T.Index == Int { + var trimStart = shapeName.startIndex + var trimEnd = shapeName.endIndex - trimStart = skipWhitespace(shapeName, trimEnd, trimStart) - trimEnd = shaveWhitespace(shapeName, trimStart, trimEnd) + trimStart = skipWhitespace(shapeName, trimEnd, trimStart) + trimEnd = shaveWhitespace(shapeName, trimStart, trimEnd) - let trimmedName = shapeName[trimStart..