-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathULogFormat.swift
286 lines (228 loc) · 8.22 KB
/
ULogFormat.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// ULogFormat.swift
// ulogReader
//
// Created by Gustaf Kugelberg on 2017-08-11.
// Copyright © 2017 Andreas Okholm. All rights reserved.
//
import Foundation
struct ULogFormat: CustomStringConvertible {
// MARK: - Initialisers -
let typeName: String
private let properties: [(String, ULogProperty)]
// MARK: - Initialisers -
init(_ typeName: String, _ properties: [(String, ULogProperty)]) {
self.typeName = typeName
self.properties = properties
}
init(_ formatString: String) {
let nameAndContents = formatString.components(separatedBy: ":")
self.typeName = nameAndContents[0]
self.properties = nameAndContents[1].components(separatedBy: ";").filter { $0 != "" }.map { string in
let formatAndName = string.components(separatedBy: " ")
return (formatAndName[1], ULogProperty(formatAndName[0]))
}
}
// MARK: - Recursive Expansion API -
public func expanded(with customType: ULogFormat) -> ULogFormat {
if customType.typeName == typeName {
return customType
}
return ULogFormat(typeName, properties.map { ($0, $1.expanded(with: customType)) })
}
// MARK: - Information API -
public func property(at path: [String]) -> ULogProperty? {
guard !path.isEmpty else {
return .custom(self)
}
let (name, index) = nameAndNumber(path[0])
guard let property = properties.first(where: { $0.0 == name })?.1 else {
return nil
}
let propertyToRecurse: ULogProperty?
switch (index, path.count, property) {
case let (i?, _, .builtins(type, n)) where i < n: propertyToRecurse = .builtin(type)
case let (i?, _, .customs(type, n)) where i < n: propertyToRecurse = .custom(type)
case (nil, _, .builtin), (nil, _, .custom): propertyToRecurse = property
case (nil, 1, .builtins), (nil, 1, .customs): propertyToRecurse = property
default: propertyToRecurse = nil
}
return propertyToRecurse?.property(at: Array(path.dropFirst()))
}
public func byteOffset(to path: [String]) -> UInt? {
guard !path.isEmpty else {
return 0
}
var offsetToProperty: UInt = 0
for property in properties {
let (name, index) = nameAndNumber(path[0])
if property.0 == name {
guard let offsetInsideProperty = property.1.byteOffset(to: Array(path.dropFirst())) else {
return nil
}
let offset: UInt?
switch (index, path.count, property.1) {
case let (i?, _, .builtins(type, n)) where i < n: offset = offsetToProperty + i*type.byteCount + offsetInsideProperty
case let (i?, _, .customs(type, n)) where i < n: offset = offsetToProperty + i*type.byteCount + offsetInsideProperty
case (nil, _, .builtin), (nil, _, .custom): offset = offsetToProperty + offsetInsideProperty
case (nil, 1, .builtins), (nil, 1, .customs): offset = offsetToProperty + offsetInsideProperty
default: return nil
}
return offset
}
else {
offsetToProperty += property.1.byteCount
}
}
return nil
}
public var byteCount: UInt {
return properties.reduce(0) { $0 + $1.1.byteCount }
}
// MARK: - Description API -
public var description: String {
return ([typeName] + indent(formatDescription)).joined(separator: "\n")
}
public var formatDescription: [String] {
return properties.flatMap { name, property in ["\(name): \(property.typeName)"] + indent(property.formatDescription) }
}
// MARK: - Helper methods -
private func indent(_ list: [String]) -> [String] {
return list.map { " " + $0 }
}
}
enum ULogProperty {
case builtin(ULogPrimitive)
case custom(ULogFormat)
case builtins(ULogPrimitive, UInt)
case customs(ULogFormat, UInt)
// MARK: - Initialiser -
init(_ formatString: String) {
let (name, arraySize) = nameAndNumber(formatString)
if let arraySize = arraySize {
if let builtin = ULogPrimitive(rawValue: name) {
self = .builtins(builtin, arraySize)
}
else {
self = .customs(.init(name, []), arraySize)
}
}
else {
if let builtin = ULogPrimitive(rawValue: name) {
self = .builtin(builtin)
}
else {
self = .custom(.init(name, []))
}
}
}
// MARK: - Recursive Expansion API -
public func expanded(with customType: ULogFormat) -> ULogProperty {
switch self {
case .custom(let format): return .custom(format.expanded(with: customType))
case .customs(let format, let n): return .customs(format.expanded(with: customType), n)
default: return self
}
}
// MARK: - Information API -
// MARK: Non-recursive
public var isArray: Bool {
switch self {
case .customs, .builtins: return true
default: return false
}
}
public var isBuiltin: Bool {
switch self {
case .builtin, .builtins: return true
default: return false
}
}
public var byteCount: UInt {
switch self {
case .builtin(let primitiveType): return primitiveType.byteCount
case .builtins(let primitiveType, let n): return n*primitiveType.byteCount
case .custom(let customType): return customType.byteCount
case .customs(let customType, let n): return n*customType.byteCount
}
}
public var format: ULogFormat? {
switch self {
case .custom(let format), .customs(let format, _): return format
default: return nil
}
}
// MARK: Recursive
public func property(at path: [String]) -> ULogProperty? {
switch (path.count, self) {
case (0, _): return self
case (_, .customs(let format, _)), (_, .custom(let format)): return format.property(at: path)
default: return nil
}
}
public func byteOffset(to path: [String]) -> UInt? {
switch (path.count, self) {
case (0, _): return 0
case (_, .customs(let format, _)), (_, .custom(let format)): return format.byteOffset(to: path)
default: return nil
}
}
// MARK: - Description API -
public var typeName: String {
switch self {
case .builtin(let builtin): return builtin.typeName
case .custom(let format): return format.typeName
case .builtins(let builtin, let n): return builtin.typeName + "[\(n)]"
case .customs(let format, let n): return format.typeName + "[\(n)]"
}
}
public var formatDescription: [String] {
switch self {
case .builtin, .builtins: return []
case .custom(let format), .customs(let format, _): return format.formatDescription
}
}
}
enum ULogPrimitive: String {
case uint8 = "uint8_t"
case uint16 = "uint16_t"
case uint32 = "uint32_t"
case uint64 = "uint64_t"
case int8 = "int8_t"
case int16 = "int16_t"
case int32 = "int32_t"
case int64 = "int64_t"
case float = "float"
case double = "double"
case bool = "bool"
case char = "char"
// MARK: - Information API -
public var byteCount: UInt {
switch self {
case .uint8: return 1
case .uint16: return 2
case .uint32: return 4
case .uint64: return 8
case .int8: return 1
case .int16: return 2
case .int32: return 4
case .int64: return 8
case .float: return 4
case .double: return 8
case .bool: return 1
case .char: return 1
}
}
// MARK: - Description API -
public var typeName: String {
return rawValue
}
}
// MARK: - Extensions -
private func nameAndNumber(_ formatString: String) -> (name: String, number: UInt?) {
let parts = formatString.components(separatedBy: ["[", "]"])
guard parts.count == 3, let count = UInt(parts[1]) else {
return (parts[0], nil)
}
return (parts[0], count)
}