Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Write hexadecimal with a table, in one place

Țugui Dragoș3f61aa23d4e4
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/ContentDigest.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/ContentDigest.swift
index 2624c7ae..a7222de8 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/ContentDigest.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/ContentDigest.swift
@@ -1,5 +1,6 @@
import CryptoKit
import Foundation
+import PeelPrivileged
/// A SHA-256 digest stored as its 32 bytes, so a digest read back from disk equals a freshly computed one.
/// `SHA256.Digest` itself cannot be created from bytes.
@@ -32,6 +33,6 @@ struct ContentDigest: Hashable, Sendable {
}
var hexadecimal: String {
- bytes.map { String(format: "%02x", $0) }.joined()
+ bytes.hexadecimal
}
}
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
index b3e35ddd..a88361a2 100644
--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/FolderDuplicates.swift
@@ -1,6 +1,7 @@
import CryptoKit
import Darwin
import Foundation
+import PeelPrivileged
/// Finds folders whose whole contents are identical. It lists the folders itself instead of reusing the file
/// scan, which keeps one candidate per inode and so cannot tell what a folder holds. Anything the walk cannot
@@ -121,7 +122,7 @@ struct FolderDuplicates: Sendable {
guard lstat(url.path(percentEncoded: false), &info) == 0, info.st_mode & S_IFMT == S_IFDIR else { return nil }
guard let folder = try? list(url, identity: FileIdentity(info), depth: 0, isNeverOffered: false, onListing: {})
else { return nil }
- return identity(of: folder).map(Self.hexadecimal)
+ return identity(of: folder).map(\.hexadecimal)
}
private func identity(of folder: Folder) -> SHA256.Digest? {
@@ -326,7 +327,7 @@ struct FolderDuplicates: Sendable {
}
let folders = sorted.compactMap(duplicate)
guard folders.count > 1 else { return nil }
- return DuplicateFolderGroup(id: Self.hexadecimal(group.digest), folders: folders)
+ return DuplicateFolderGroup(id: group.digest.hexadecimal, folders: folders)
}
.sorted { ($0.reclaimableSize, $0.size, $1.id) > ($1.reclaimableSize, $1.size, $0.id) }
}
@@ -338,7 +339,7 @@ struct FolderDuplicates: Sendable {
fileCount: folder.fileCount,
size: folder.size,
reclaimableSize: Self.reclaimable(folder),
- contents: Self.hexadecimal(contents),
+ contents: contents.hexadecimal,
neverProjects: neverProjects
)
}
@@ -377,10 +378,6 @@ struct FolderDuplicates: Sendable {
Dictionary(digests.map { ($0.item.identity, $0.digest) }, uniquingKeysWith: { first, _ in first })
}
- private static func hexadecimal(_ digest: SHA256.Digest) -> String {
- digest.map { String(format: "%02x", $0) }.joined()
- }
-
/// Encodes one entry as a tag, the name's length, the name, and `tail`. The length comes first so a name
/// cannot be misread as the end of one entry and the start of the next.
private static func field(_ tag: String, _ name: String, _ tail: [UInt8]) -> [UInt8] {
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalProfile.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalProfile.swift
index 7b0f4ca2..c070cf15 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalProfile.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalProfile.swift
@@ -1,5 +1,6 @@
import AppKit
import CryptoKit
+import PeelPrivileged
public enum TerminalProfile {
public static func settings(for theme: TerminalTheme, options: Set<TerminalOption> = []) throws -> [String: Any] {
@@ -35,7 +36,7 @@ public enum TerminalProfile {
public static func fingerprint(of settings: [String: Any]) -> String? {
guard let data = try? PropertyListSerialization.data(fromPropertyList: settings, format: .xml, options: 0)
else { return nil }
- return SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
+ return SHA256.hash(data: data).hexadecimal
}
private static let ansiNames = ["Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"]
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/BrowserWallets.swift b/Packages/PeelCore/Sources/PeelPrivileged/BrowserWallets.swift
index 7ebfe17e..e5905065 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/BrowserWallets.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/BrowserWallets.swift
@@ -216,7 +216,7 @@ extension ProtectedData {
/// The SHA-256 of `id`, in lowercase hex, which is how the lists above hold each ID.
static func fingerprint(_ id: String) -> String {
- SHA256.hash(data: Data(id.utf8)).map { String(format: "%02x", $0) }.joined()
+ SHA256.hash(data: Data(id.utf8)).hexadecimal
}
/// True for a path at or inside a wallet extension's storage: `Local Extension Settings/<ID>` or an IndexedDB
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/Hexadecimal.swift b/Packages/PeelCore/Sources/PeelPrivileged/Hexadecimal.swift
new file mode 100644
index 00000000..03fc4d73
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelPrivileged/Hexadecimal.swift
@@ -0,0 +1,15 @@
+private let hexadecimalDigits = Array("0123456789abcdef".utf8)
+
+extension Sequence<UInt8> {
+ /// The bytes as lowercase hexadecimal, two digits each, as `String(format: "%02x")` writes them, which costs a
+ /// formatter per byte.
+ public var hexadecimal: String {
+ var text: [UInt8] = []
+ text.reserveCapacity(underestimatedCount * 2)
+ for byte in self {
+ text.append(hexadecimalDigits[Int(byte >> 4)])
+ text.append(hexadecimalDigits[Int(byte & 0x0F)])
+ }
+ return String(decoding: text, as: UTF8.self)
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HexadecimalTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HexadecimalTests.swift
new file mode 100644
index 00000000..37e0449e
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HexadecimalTests.swift
@@ -0,0 +1,15 @@
+import CryptoKit
+import Foundation
+import PeelPrivileged
+import Testing
+
+struct HexadecimalTests {
+ @Test func writesEachByteAsFormatDoes() {
+ let every = (0...255).map(UInt8.init)
+
+ #expect([0x00, 0x0F, 0xA5, 0xFF].hexadecimal == "000fa5ff")
+ #expect(every.hexadecimal == every.map { String(format: "%02x", $0) }.joined())
+ #expect(SHA256.hash(data: Data("Peel".utf8)).hexadecimal.count == 64)
+ #expect([UInt8]().hexadecimal.isEmpty)
+ }
+}