-
Notifications
You must be signed in to change notification settings - Fork 8
/
CollectionScanner.swift
267 lines (241 loc) · 8.58 KB
/
CollectionScanner.swift
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import Foundation
public struct CollectionScanner<C> where C: Collection {
public typealias Element = C.Element
public typealias SubSequence = C.SubSequence
public typealias Index = C.Index
public let elements: C
public init(elements: C) {
self.elements = elements
current = self.elements.startIndex
}
public var current: Index {
willSet {
assert(current >= elements.startIndex)
assert(current <= elements.endIndex)
}
}
public var remaining: SubSequence {
elements[current ..< elements.endIndex]
}
public var remainingCount: Int {
elements.distance(from: current, to: elements.endIndex)
}
public var atEnd: Bool {
elements.distance(from: current, to: elements.endIndex) == 0
}
public mutating func scan(count: Int) -> SubSequence? {
guard !atEnd else {
return nil
}
guard remainingCount >= count else {
return nil
}
let result = elements[current ..< elements.index(current, offsetBy: count)]
current = elements.index(current, offsetBy: count)
return result
}
public func peek() -> Element? {
if atEnd {
return nil
}
return elements[current]
}
}
public extension CollectionScanner where Element: Equatable {
mutating func scan(value: Element) -> Bool {
guard !atEnd else {
return false
}
guard elements[current] == value else {
return false
}
current = elements.index(current, offsetBy: 1)
return true
}
mutating func scan(value: [Element]) -> Bool {
let saved = current
guard let scanned = scan(count: value.count) else {
return false
}
guard Array(scanned) == value else {
current = saved
return false
}
return true
}
mutating func scanUpTo(value: Element, consuming: Bool = false) -> SubSequence? {
guard !atEnd else {
return nil
}
let index = remaining.firstIndex(of: value) ?? elements.endIndex
let result = elements[current ..< index]
current = index
if consuming && !atEnd {
current = elements.index(current, offsetBy: 1)
}
return result
}
// TODO: add a flag to control whether remaining to count as a match even if it doesnt end in a token
mutating func scanUpTo(value: [Element], consuming: Bool = false) -> SubSequence? {
assert(!value.isEmpty)
guard !atEnd else {
return nil
}
// TODO: Replace with https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm where possible
var remainingToSearch = remaining
while !atEnd {
if value.count > remainingToSearch.count {
break
}
if let index = remainingToSearch.firstIndex(of: value.first!) {
guard remainingToSearch.distance(from: index, to: remainingToSearch.endIndex) >= value.count else {
return nil
}
let hit = remainingToSearch[index ..< remainingToSearch.index(index, offsetBy: value.count)]
// TODO: Is converting to an array slice efficient?
if ArraySlice<Element>(hit) == value[value.startIndex ..< value.endIndex] {
current = index
let result = remainingToSearch[..<index]
if consuming && !atEnd {
current = elements.index(current, offsetBy: value.count)
return result
}
return result
}
remainingToSearch = remainingToSearch[index...]
} else {
break
}
}
let result = remaining
current = elements.endIndex
return result
}
mutating func scan(componentsSeparatedBy separator: Element) -> [SubSequence] {
guard !atEnd else {
return []
}
let it = iterator(forComponentsSeparatedBy: separator)
let result = it.collect()
current = elements.endIndex // TODO: Is this always correct?
return result
}
mutating func scan(anyOf elements: [Element]) -> Element? {
guard let element = peek(), elements.contains(element) else {
return nil
}
current = self.elements.index(current, offsetBy: 1)
return element
}
}
extension CollectionScanner where Element: Equatable {
// TODO: This operates on a COPY of the Scanner
func iterator(for block: @escaping (inout Self) -> SubSequence?) -> AnyIterator<SubSequence> {
CollectionScannerIterator(scanner: self, block: block).eraseToAnyIterator()
}
// TODO: This operates on a COPY of the Scanner
func iterator(forComponentsSeparatedBy separator: Element) -> AnyIterator<SubSequence> {
iterator { scanner in
if let result = scanner.scanUpTo(value: separator, consuming: true) {
return result
}
return nil
}
}
// TODO: This operates on a COPY of the Scanner
func iterator(forComponentsSeparatedBy separator: [Element]) -> AnyIterator<SubSequence> {
iterator { scanner in
if let result = scanner.scanUpTo(value: separator, consuming: true) {
return result
}
return nil
}
}
}
// TODO: This operates on a COPY of the Scanner
public struct CollectionScannerIterator<C>: IteratorProtocol where C: Collection, C.Element: Equatable {
public typealias Scanner = CollectionScanner<C>
var scanner: Scanner
let block: (inout Scanner) -> Scanner.SubSequence?
public mutating func next() -> Scanner.SubSequence? {
if scanner.atEnd {
return nil
}
return block(&scanner)
}
}
extension CollectionScanner: CustomDebugStringConvertible {
public var debugDescription: String {
let startIndex = elements.distance(from: elements.startIndex, to: elements.startIndex)
let endIndex = elements.distance(from: elements.startIndex, to: elements.endIndex)
let current = elements.distance(from: elements.startIndex, to: current)
return "\(startIndex) / \(endIndex) / \(current)"
}
}
public extension CollectionScanner where Element == UInt8 {
mutating func scan<T>(type t: T.Type) -> T? where T: BinaryFloatingPoint {
let saved = current
guard let array = scan(count: MemoryLayout<T>.size) else {
return nil
}
return Array(array).withUnsafeBufferPointer { buffer -> T? in
guard let pointer = buffer.baseAddress else {
current = saved
return nil
}
return pointer.withMemoryRebound(to: t, capacity: 1) { pointer in
pointer.pointee
}
}
}
mutating func scan<T>(type t: T.Type) -> T? where T: BinaryInteger {
let saved = current
guard let array = scan(count: MemoryLayout<T>.size) else {
return nil
}
return Array(array).withUnsafeBufferPointer { buffer in
guard let pointer = buffer.baseAddress else {
current = saved
return nil
}
return pointer.withMemoryRebound(to: t, capacity: 1) { pointer in
pointer.pointee
}
}
}
mutating func scan<T>(type t: T.Type, count: Int) -> [T]? where T: BinaryInteger {
let values = (0 ..< count).compactMap { _ in
scan(type: t)
}
assert(values.count == count)
return values
}
mutating func scan<T>(type t: T.Type) -> T? where T: SIMD, T.Scalar: BinaryInteger {
scan(type: t.Scalar.self, count: t.scalarCount).map { T($0) }
}
}
public extension CollectionScanner {
mutating func scan(until block: (Element) -> Bool) -> SubSequence? {
guard !atEnd else {
return nil
}
for index in remaining.indices {
let element = remaining[index]
if block(element) == true {
let result = elements[current ..< index]
current = index
return result
}
}
let result = remaining
current = elements.endIndex
return result
}
}
public extension CollectionScanner {
// Similar to SwiftUI.Path.path
func scan<R>(block: (inout CollectionScanner) throws -> R) rethrows -> R {
var scanner = self
return try block(&scanner)
}
}