Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281import CryptoKitimport Foundation
struct NamePolicy: Sendable { let salt: String let always: Set<String> let contextual: Set<String> private let saltBytes: Data
init(_ data: Data) throws { let object = try DevJSON.decode(data).requireObject() guard Set(object.keys) == ["version", "salt", "always", "contextual"] else { throw DevError("Invalid hash policy fields") } guard object["version"]?.number == 1 else { throw DevError("Unsupported hash policy version") } guard let salt = object["salt"]?.string, try NameRegex.matches("[0-9a-f]{32}", salt) else { throw DevError("Invalid hash policy salt") } self.salt = salt var saltBytes = Data() let bytes = Array(salt.utf8) for offset in stride(from: 0, to: bytes.count, by: 2) { let high = bytes[offset] <= 57 ? bytes[offset] - 48 : bytes[offset] - 87 let low = bytes[offset + 1] <= 57 ? bytes[offset + 1] - 48 : bytes[offset + 1] - 87 saltBytes.append(high * 16 + low) } self.saltBytes = saltBytes func hashes(_ key: String) throws -> [String] { guard let values = object[key]?.array else { throw DevError("Invalid hash policy entries") } return try values.map { guard let hash = $0.string, try NameRegex.matches("[0-9a-f]{64}", hash) else { throw DevError("Invalid hash policy entries") } return hash } } let always = try hashes("always") let contextual = try hashes("contextual") let all = always + contextual guard !all.isEmpty, Set(all).count == all.count else { throw DevError("Hash policy must be nonempty with no duplicate entries") } self.always = Set(always) self.contextual = Set(contextual) }
func digest(_ value: String) -> String { var data = saltBytes data.append(0) data.append(contentsOf: value.utf8) return DevHex.string(SHA256.hash(data: data)) }}
enum NameRegex { static func matches(_ pattern: String, _ value: String) throws -> Bool { let expression = try NSRegularExpression(pattern: "\\A(?:" + pattern + ")\\z") return expression.firstMatch(in: value, range: NSRange(value.startIndex..., in: value)) != nil } static func contains(_ pattern: String, _ value: String) throws -> Bool { try NSRegularExpression(pattern: pattern).firstMatch(in: value, range: NSRange(value.startIndex..., in: value)) != nil }}
struct NameAllowanceKey: Hashable { let scope: String let source: String? let line: String let token: String}
struct NameAllowances { let entries: [NameAllowanceKey: String] init(_ data: Data) throws { let document = try DevJSON.decode(data).requireObject() guard Set(document.keys) == ["version", "entries"], document["version"]?.number == 1 else { throw DevError("Invalid allowlist version or fields") } guard let values = document["entries"]?.array else { throw DevError("Invalid allowlist entries") } var entries: [NameAllowanceKey: String] = [:] for value in values { guard let object = value.object else { throw DevError("Invalid allowlist entry fields") } var fields: Set<String> = ["scope", "line_hash", "token_hash", "reason"] if object["scope"]?.string != "artifact" || object["source_hash"] != nil { fields.insert("source_hash") } guard Set(object.keys) == fields else { throw DevError("Invalid allowlist entry fields") } var strings: [String: String] = [:] for (key, value) in object { guard let text = value.string, !text.isEmpty else { throw DevError("Allowlist fields must be nonempty strings") } strings[key] = text } guard let scope = strings["scope"], ["content", "path", "commit", "artifact"].contains(scope), let line = strings["line_hash"], let token = strings["token_hash"], let reason = strings["reason"], try NameRegex.matches("[0-9a-f]{64}", line), try NameRegex.matches("[0-9a-f]{64}", token) else { throw DevError("Invalid allowlist scope or hash") } if let source = strings["source_hash"], try !NameRegex.matches("[0-9a-f]{64}", source) { throw DevError("Invalid allowlist scope or hash") } let key = NameAllowanceKey(scope: scope, source: strings["source_hash"], line: line, token: token) guard entries.updateValue(reason, forKey: key) == nil else { throw DevError("Duplicate allowlist entry") } } self.entries = entries }}
struct NameHit: Codable { let scope: String let source: String let sourceHash: String let line: Int let column: Int let lineHash: String let tokenHash: String let reason: String? enum CodingKeys: String, CodingKey { case scope, source, line, column, reason case sourceHash = "source_hash", lineHash = "line_hash", tokenHash = "token_hash" }}
struct NameWord { let start: Int let end: Int let value: String let folded: String}
final class NameScanner { let policy: NamePolicy let allowances: NameAllowances var hits: [NameHit] = [] var allowed: [NameHit] = [] var artifacts = 0 private var cache: [String: String] = [:]
init(policy: NamePolicy, allowances: NameAllowances) { self.policy = policy self.allowances = allowances } func hash(_ value: String) -> String { if let value = cache[value] { return value } let digest = policy.digest(value) if cache.count >= 65_536 { cache.removeAll(keepingCapacity: true) } cache[value] = digest return digest }
static func words(_ scalars: [Unicode.Scalar]) -> [NameWord] { func isWord(_ scalar: Unicode.Scalar) -> Bool { let value = scalar.value if value < 128 { return (48...57).contains(value) || (65...90).contains(value) || (97...122).contains(value) } switch scalar.properties.generalCategory { case .uppercaseLetter, .lowercaseLetter, .titlecaseLetter, .modifierLetter, .otherLetter, .decimalNumber, .letterNumber, .otherNumber: return true default: return false } } var words: [NameWord] = [] var index = 0 while index < scalars.count { guard isWord(scalars[index]) else { index += 1; continue } let start = index repeat { index += 1 } while index < scalars.count && isWord(scalars[index]) let value = String(String.UnicodeScalarView(scalars[start..<index])) words.append(NameWord(start: start, end: index, value: value, folded: value.lowercased())) } return words }
func scan(_ scope: String, source: String, text: String, lineOffset: Int = 0, binary: Bool = false) throws { let scalars = Array(text.unicodeScalars) let words = Self.words(scalars) var starts = [0] for (index, scalar) in scalars.enumerated() where scalar == "\n" { starts.append(index + 1) } func row(at position: Int) -> Int { var low = 0 var high = starts.count while low < high { let middle = (low + high) / 2 if starts[middle] <= position { low = middle + 1 } else { high = middle } } return low } func slice(_ range: Range<Int>) -> String { String(String.UnicodeScalarView(scalars[range])) } let sourceHash = hash("source\0" + source) for (index, word) in words.enumerated() { for width in 1...2 { let endIndex = index + width - 1 guard endIndex < words.count else { continue } let final = words[endIndex] if width == 2 { let separator = scalars[word.end..<final.start] guard !separator.isEmpty, separator.allSatisfy({ CharacterSet.whitespacesAndNewlines.contains($0) || "._/-".unicodeScalars.contains($0) }) else { continue } } let token = width == 1 ? word.folded : word.folded + " " + final.folded let tokenHash = hash(token) guard policy.always.contains(tokenHash) || policy.contextual.contains(tokenHash) else { continue } let short = binary && token.unicodeScalars.count < 4 let reference = try (policy.always.contains(tokenHash) && !short) || productContext( scalars, words: words, first: index, last: endIndex, isPath: scope == "path", capitalization: !short) let startRow = row(at: word.start) let endRow = row(at: final.end - 1) let lineEnd = endRow < starts.count ? starts[endRow] - 1 : scalars.count let line = slice(starts[startRow - 1]..<lineEnd).trimmingCharacters(in: .whitespacesAndNewlines) let lineHash = hash("allow\0" + line) var reason = allowances.entries[ NameAllowanceKey(scope: scope, source: sourceHash, line: lineHash, token: tokenHash)] if reason == nil && scope == "artifact" { reason = allowances.entries[ NameAllowanceKey(scope: scope, source: nil, line: lineHash, token: tokenHash)] } let hit = NameHit( scope: scope, source: source, sourceHash: sourceHash, line: startRow + lineOffset, column: word.start - starts[startRow - 1] + 1, lineHash: lineHash, tokenHash: tokenHash, reason: reason) if reason != nil { allowed.append(hit) } else if reference { hits.append(hit) } } } }
private func productContext( _ text: [Unicode.Scalar], words: [NameWord], first: Int, last: Int, isPath: Bool, capitalization: Bool ) throws -> Bool { if isPath || (capitalization && words[first...last].contains(where: { word in guard let scalar = word.value.unicodeScalars.first else { return false } return CharacterSet.uppercaseLetters.contains(scalar) })) { return true } let before = words[max(0, first - 2)..<first].map { $0.value.lowercased() } let after = words[(last + 1)..<min(words.count, last + 3)].map { $0.value.lowercased() } if let word = before.last, ["app", "wm", "like"].contains(word) { return true } if let word = after.first, ["app", "wm"].contains(word) { return true } if before == ["inspired", "by"] || before == ["window", "manager"] || after == ["window", "manager"] { return true } var left = words[first].start var right = words[last].end let stops = "\"'`<>(){}[],;".unicodeScalars while left > 0 && !CharacterSet.whitespacesAndNewlines.contains(text[left - 1]) && !stops.contains(text[left - 1]) { left -= 1 } while right < text.count && !CharacterSet.whitespacesAndNewlines.contains(text[right]) && !stops.contains(text[right]) { right += 1 } let component = String(String.UnicodeScalarView(text[left..<right])).trimmingCharacters( in: CharacterSet(charactersIn: ".!?:")) if try NameRegex.contains("[\\w.-]/[\\w.-]|/[\\w.-]", component) { return true } let quoted = left > 0 && "\"'`".unicodeScalars.contains(text[left - 1]) return try (quoted && (try NameRegex.matches("[\\w-]+(?:\\.[\\w-]+){2,}", component))) || (try NameRegex.matches("(?i:(?:com|org|net|io|dev|app|edu)\\.[\\w.-]+)", component)) || (try NameRegex.matches("(?i:[\\w-]+\\.(?:app|com|org|net|io|dev|swift|md|png|json|toml))", component)) }
func binary(_ scope: String, source: String, path: URL, checkout: String? = nil) throws { var text = String( decoding: try DevProcess.checked("/usr/bin/strings", ["-n", "4", "--", path.path]), as: UTF8.self) if let checkout { text = text.replacingOccurrences(of: checkout, with: ".") } for (index, line) in text.components(separatedBy: "\n").enumerated() { try scan(scope, source: source, text: line, lineOffset: index, binary: true) } if scope == "artifact" { artifacts += 1 } }}