diff --git a/Sources/apk/Index/ApkIndexDigest.swift b/Sources/apk/Index/ApkIndexDigest.swift index e560503..968488f 100644 --- a/Sources/apk/Index/ApkIndexDigest.swift +++ b/Sources/apk/Index/ApkIndexDigest.swift @@ -105,28 +105,7 @@ extension ApkIndexDigest.DigestType: CustomStringConvertible { } extension ApkIndexDigest: CustomStringConvertible { -#if DEBUG - private static let hex = Array("0123456789ABCDEF".unicodeScalars) public var description: String { - var s = "[\(self.type)] " - s.reserveCapacity(10 + self.data.count * 2) - Self.hex.withUnsafeBufferPointer { hp in - for b in self.data { - s.unicodeScalars.append(hp[Int(b >> 4)]) - s.unicodeScalars.append(hp[Int(b & 15)]) - } - } - return s + return "[\(self.type)] \(self.data.asHexString)" } -#else - private static let hex = "0123456789ABCDEF".map(\.asciiValue!) - public var description: String { - Self.hex.withUnsafeBufferPointer { hp in - let hexChars = self.data.flatMap { b in - [hp[Int(b >> 4)], hp[Int(b & 15)]] - } - return "[\(self.type)] \(String(bytes: hexChars, encoding: .ascii)!)" - } - } -#endif } diff --git a/Sources/apk/Utility/DataExtensions.swift b/Sources/apk/Utility/DataExtensions.swift index bf5c2eb..d5ed7b6 100644 --- a/Sources/apk/Utility/DataExtensions.swift +++ b/Sources/apk/Utility/DataExtensions.swift @@ -29,4 +29,29 @@ extension Data { self.append(byte) } } + +#if DEBUG + private static let hex = Array("0123456789ABCDEF".unicodeScalars) + var asHexString: String { + var s = "" + s.reserveCapacity(self.count * 2) + Self.hex.withUnsafeBufferPointer { hp in + for b in self { + s.unicodeScalars.append(hp[Int(b >> 4)]) + s.unicodeScalars.append(hp[Int(b & 15)]) + } + } + return s + } +#else + private static let hex = "0123456789ABCDEF".map(\.asciiValue!) + var asHexString: String { + Self.hex.withUnsafeBufferPointer { hp in + let hexChars = self.flatMap { b in + [hp[Int(b >> 4)], hp[Int(b & 15)]] + } + return String(bytes: hexChars, encoding: .ascii)! + } + } +#endif }