aboutsummaryrefslogtreecommitdiff
path: root/Sources/WmapParser/Utils.swift
blob: d79578b92b2dd336acd38d0bda0e9757dbd05157 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
extension WmapParser {
    /// Check if a byte is a digit
    @inline(__always)
        static func isDigit(_ byte: UInt8) -> Bool {
            byte >= kZero && byte <= kNine
        }

    /// Looks for x and y separated by a comma.
    static func parseCoordinates<T: Collection>(_ bytes: T, _ start: Int, _ end: Int) -> (
            MapPoint, Int
            )?
        where T.Element == UInt8, T.Index == Int {
            var index = skipWhitespace(bytes, end, start)

                guard let (xCoordinate, nextIndex) = parseSingleCoordinate(bytes, index, end) else {
                    return nil
                }
            index = nextIndex

                guard expectComma(bytes, &index, end) else { return nil }

            guard let (yCoordinate, nextIndex) = parseSingleCoordinate(bytes, index, end) else {
                return nil
            }
            index = nextIndex

                guard expectClosingParen(bytes, &index, end) else { return nil }

            return (MapPoint(xCoordinate: xCoordinate, yCoordinate: yCoordinate), index)
        }

    /// Checks if a candidate for a coordinate is a digit, and parses it.
    @inline(__always)
        static func parseSingleCoordinate<T: Collection>(_ bytes: T, _ start: Int, _ end: Int) -> (Double, Int)?
        where T.Element == UInt8, T.Index == Int {
            var index = start
                while index < end && (isDigit(bytes[index]) || bytes[index] == 46) { index += 1 }
            guard index > start else { return nil }
            guard let value = parseDouble(bytes, start, index) else { return nil }
            return (value, index)
        }

    /// Advances the index until we find a comma.
    @inline(__always)
        static func expectComma<T: Collection>(_ bytes: T, _ index: inout Int, _ end: Int) -> Bool
        where T.Element == UInt8, T.Index == Int {
            index = skipWhitespace(bytes, end, index)
                guard index < end && bytes[index] == kComma else { return false }
            index += 1
                index = skipWhitespace(bytes, end, index)
                return true
        }

    /// Advances the index until we find a closing parenthesis
    @inline(__always)
        static func expectClosingParen<T: Collection>(_ bytes: T, _ index: inout Int, _ end: Int) -> Bool
        where T.Element == UInt8, T.Index == Int {
            index = skipWhitespace(bytes, end, index)
                guard index < end && bytes[index] == kCloseParenthesis else { return false }
            index += 1
                return true
        }

    /// Parses a double
    @inline(__always)
        static func parseDouble<T: Collection>(_ bytes: T, _ start: Int, _ end: Int) -> Double?
        where T.Element == UInt8, T.Index == Int {
            var index = skipWhitespace(bytes, end, start)

                let numStart = index
                while index < end && (isDigit(bytes[index]) || bytes[index] == kPeriod) { index += 1 }

            guard index > numStart else { return nil }

            // Why not String(data:encoding:)? This is much faster.
            // swiftlint:disable:next optional_data_string_conversion
            let str = String(decoding: bytes[numStart..<index], as: UTF8.self)
                return Double(str)
        }

    /// Advances an index by looking for whitespace left-to-right
    static func skipWhitespace<T: Collection>(_ bytes: T, _ end: Int, _ start: Int) -> Int
        where T.Element == UInt8, T.Index == Int {
            var index = start
                while index < end && (bytes[index] == kSpace || bytes[index] == kTab) { index += 1 }
            return index
        }

    /// Decreases an index by looking for whitespace right-to-left
    static func shaveWhitespace<T: Collection>(_ bytes: T, _ start: Int, _ end: Int) -> Int
        where T.Element == UInt8, T.Index == Int {
            var index = end
                while index > start && (bytes[index - 1] == kSpace || bytes[index - 1] == kTab) { index -= 1 }
            return index
        }

    /// Given the bytes buffer, finds the next non-empty line.
    @inline(__always)
        static func extractLine<T: Collection>(_ bytes: T, _ index: inout Int, _ length: Int) -> (
                Int, Int
                )?
        where T.Element == UInt8, T.Index == Int {
            while index < length && (bytes[index] == kNewline || bytes[index] == kCarriageReturn) {
                index += 1
            }
            let lineStart = index

                while index < length && bytes[index] != kNewline && bytes[index] != kCarriageReturn { index += 1 }
            if lineStart == index { return nil }

            var start = lineStart
                var end = index

                start = skipWhitespace(bytes, end, start)
                end = shaveWhitespace(bytes, start, end)

                if start == end { return nil }

            return (start, end)
        }

    /// Trims a byte collection and returns a string
    @inline(__always)
        static func extractTrimmedString<T: Collection>(_ bytes: T, _ start: Int, _ end: Int) -> String?
        where T.Element == UInt8, T.Index == Int {
            var trimStart = start
                var trimEnd = end

                trimStart = skipWhitespace(bytes, trimEnd, trimStart)
                trimEnd = shaveWhitespace(bytes, trimStart, trimEnd)

                guard trimStart < trimEnd else { return nil }

            // Why not String(data:encoding:)? This is much faster.
            // swiftlint:disable:next optional_data_string_conversion
            return String(decoding: bytes[trimStart..<trimEnd], as: UTF8.self)
        }
}