From 73b6e703fbb1c3154ee90ff132e74f63cf45335c Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Wed, 13 Nov 2024 21:20:50 +1100 Subject: [PATCH] Optimise hex digest string conversion --- Sources/apk/Index/ApkIndexDigest.swift | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Sources/apk/Index/ApkIndexDigest.swift b/Sources/apk/Index/ApkIndexDigest.swift index ac0d3d9..2de5798 100644 --- a/Sources/apk/Index/ApkIndexDigest.swift +++ b/Sources/apk/Index/ApkIndexDigest.swift @@ -105,5 +105,28 @@ extension ApkIndexDigest.DigestType: CustomStringConvertible { } extension ApkIndexDigest: CustomStringConvertible { - var description: String { "[\(self.type)] \(self.data.map { String(format: "%02X", $0) }.joined())" } +#if DEBUG + private static let hex = Array("0123456789ABCDEF".unicodeScalars) + 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 + } +#else + private static let hex = "0123456789ABCDEF".map(\.asciiValue!) + 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 }