From c8139ce0df24362b393f325031044bbc63cb51aa Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Fri, 23 Mar 2018 14:13:44 -0400 Subject: [PATCH 1/3] add hexEncoded + lossless data --- Sources/Core/CoreError.swift | 8 ++++ Sources/Core/Data+Hex.swift | 16 +++++++ Sources/Core/LosslessDataConvertible.swift | 49 ++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 Sources/Core/CoreError.swift create mode 100644 Sources/Core/Data+Hex.swift create mode 100644 Sources/Core/LosslessDataConvertible.swift diff --git a/Sources/Core/CoreError.swift b/Sources/Core/CoreError.swift new file mode 100644 index 00000000..05e2f110 --- /dev/null +++ b/Sources/Core/CoreError.swift @@ -0,0 +1,8 @@ +public struct CoreError: Debuggable, Error { + public var identifier: String + public var reason: String + public init(identifier: String, reason: String) { + self.identifier = identifier + self.reason = reason + } +} diff --git a/Sources/Core/Data+Hex.swift b/Sources/Core/Data+Hex.swift new file mode 100644 index 00000000..6fbb8e05 --- /dev/null +++ b/Sources/Core/Data+Hex.swift @@ -0,0 +1,16 @@ +extension Data { + public func hexEncodedString() -> String { + var bytes = Data() + bytes.reserveCapacity(self.count * 2) + + for byte in self { + bytes.append(radix16table[Int(byte / 16)]) + bytes.append(radix16table[Int(byte % 16)]) + } + + return String(bytes: bytes, encoding: .utf8)! + } +} + + +fileprivate let radix16table: [UInt8] = [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66] diff --git a/Sources/Core/LosslessDataConvertible.swift b/Sources/Core/LosslessDataConvertible.swift new file mode 100644 index 00000000..5a4b12e3 --- /dev/null +++ b/Sources/Core/LosslessDataConvertible.swift @@ -0,0 +1,49 @@ +/// A type that can be represented as Data in a lossless, unambiguous way. +public protocol LosslessDataConvertible { + /// Losslessly converts this type to `Data`. + func convertToData() throws -> Data + + /// Losslessly converts `Data` to this type. + static func convertFromData(_ data: Data) throws -> Self +} + +extension String: LosslessDataConvertible { + /// Converts this `String` to data using `.utf8`. + public func convertToData() -> Data { + return Data(utf8) + } + + /// Converts `Data` to a `utf8` encoded String. + /// + /// - throws: Error if String is not UTF8 encoded. + public static func convertFromData(_ data: Data) throws -> String { + guard let string = String(data: data, encoding: .utf8) else { + throw CoreError(identifier: "stringData", reason: "String not UTF-8 encoded") + } + return string + } +} + +extension Array: LosslessDataConvertible where Element == UInt8 { + /// Converts this `[UInt8]` to `Data`. + public func convertToData() -> Data { + return Data(bytes: self) + } + + /// Converts `Data` to `[UInt8]`. + public static func convertFromData(_ data: Data) -> Array { + return .init(data) + } +} + +extension Data: LosslessDataConvertible { + /// `LosslessDataConvertible` conformance. + public func convertToData() throws -> Data { + return self + } + + /// `LosslessDataConvertible` conformance. + public static func convertFromData(_ data: Data) -> Data { + return data + } +} From ae23745d48459994790c329d5c79b4c65dcdca1f Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Fri, 23 Mar 2018 15:42:10 -0400 Subject: [PATCH 2/3] trigger ci From f8ed0489c1ff08b079eea50e4820a348f0244366 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Fri, 23 Mar 2018 17:01:44 -0400 Subject: [PATCH 3/3] trigger ci