aboutsummaryrefslogtreecommitdiff
path: root/Sources/WmapParser/ComponentParser.swift
blob: d87abc57d89358cdf98ca8eaf0ce898e44eb1fe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
extension WmapParser {

  /// Parses a component with its coordinates. Assumes a trimmed line was
  /// provided.
  static func parseComponent<T: Collection>(_ 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
    }

    guard
      let (coordinates, endIndex) = parseCoordinates(
        bytes, parenthesisIndex + 1, end)
    else {
      return nil
    }

    let shape = extractComponentShape(bytes, endIndex, end)

    return Component(label: label, coordinates: coordinates, shape: shape)
  }

  /// Looks for a component label by taking the string before the first open
  /// parenthesis.
  @inline(__always)
  private static func extractComponentLabel<T: Collection>(_ 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
  }

  /// 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<T: Collection>(_ bytes: T, _ start: Int, _ end: Int)
    -> Shape
  where T.Element == UInt8, T.Index == Int {
    var index = skipWhitespace(bytes, end, start)

    guard index < end && bytes[index] == kOpenSquareBracket else { return .circle }

    index += 1
    let shapeStart = index
    while index < end && bytes[index] != kCloseSquareBracket { index += 1 }

    guard index > shapeStart && index < end else { return .circle }

    let shapeName = bytes[shapeStart..<index]
    return parseShapeFromBytes(shapeName)
  }

  /// Given the contents of a shape label, return the right type.
  @inline(__always)
  private static func parseShapeFromBytes<T: Collection>(_ 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)

    let trimmedName = shapeName[trimStart..<trimEnd]

    // "x"
    if matchesKeyword(trimmedName, [120]) { return .xMark }
    // "square"
    if matchesKeyword(trimmedName, [115, 113, 117, 97, 114, 101]) { return .square }
    // "triangle"
    if matchesKeyword(trimmedName, [116, 114, 105, 97, 110, 103, 108, 101]) { return .triangle }

    return .circle
  }
}