forked from ole/HexHexHex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHEXFile.swift
More file actions
47 lines (43 loc) · 1.68 KB
/
Copy pathHEXFile.swift
File metadata and controls
47 lines (43 loc) · 1.68 KB
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
/// A value representing the contents of an Intel Hexadecimal Object File Format (.hex).
///
/// - SeeAlso: https://en.wikipedia.org/wiki/Intel_HEX
public struct HEXFile {
/// The records the file contains. Each line in the .hex file corresponds to one record.
public var records: [Record]
/// Initializes a `HEXFile` value with the text contents of a .hex file.
///
/// - Parameter text: A string containing the contents of a .hex file.
/// Example of a .hex file containing one data record and one end-of-file record:
///
/// :10010000214601360121470136007EFE09D2190140
/// :00000001FF
///
/// - Throws: Throws a `HEXParser.Error` if `text` does not contain a valid .hex file
/// or the string cannot be parsed.
public init(text: String) throws {
let parser = HEXParser(text: text)
self.records = try parser.parse()
}
/// Initializes a `HEXFile` value with a collection of ASCII bytes.
///
/// - Parameter bytes: A collection of bytes containing the contents of a .hex file.
/// The bytes must be an ASCII-encoded string.
///
/// - Throws: Throws a `HEXParser.Error` if `bytes` does not contain a valid .hex file
/// or the string cannot be parsed.
public init<C: Collection>(bytes: C) throws where C.Element == UInt8 {
let text = String(decoding: bytes, as: UTF8.self)
try self.init(text: text)
}
}
extension HEXFile: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
"HEXFile (\(records.count) records)"
}
public var debugDescription: String {
"""
HEXFile (\(records.count) records)
\(records.map(String.init(describing:)).joined(separator: "\n "))
"""
}
}