payfrit-beacon-ios/PayfritBeacon/UUIDFormatting.swift
Schwifty 237ac38557 refactor: consolidate UUID formatting into shared String extension
Created UUIDFormatting.swift with .normalizedUUID and .uuidWithDashes
String extensions, replacing 4 duplicate formatUuidWithDashes() methods
and 6+ inline .replacingOccurrences(of: "-", with: "").uppercased() calls
across Api.swift, BeaconScanner.swift, ScanView.swift,
ServicePointListView.swift, BeaconBanList.swift, and BeaconProvisioner.swift.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-21 10:02:58 +00:00

27 lines
1.1 KiB
Swift

import Foundation
// MARK: - UUID Formatting Utilities
// Consolidated UUID normalization and formatting logic.
// Previously duplicated across Api.swift, BeaconScanner.swift, ScanView.swift,
// ServicePointListView.swift, and BeaconBanList.swift.
extension String {
/// Normalize a UUID string: strip dashes, uppercase.
/// Input: "e2c56db5-dffb-48d2-b060-d0f5a71096e0" or "E2C56DB5DFFB48D2B060D0F5A71096E0"
/// Output: "E2C56DB5DFFB48D2B060D0F5A71096E0"
var normalizedUUID: String {
replacingOccurrences(of: "-", with: "").uppercased()
}
/// Format a 32-char hex string into standard UUID format (8-4-4-4-12).
/// Input: "E2C56DB5DFFB48D2B060D0F5A71096E0"
/// Output: "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"
/// Returns the original string unchanged if it's not exactly 32 hex chars after normalization.
var uuidWithDashes: String {
let clean = normalizedUUID
guard clean.count == 32 else { return self }
let c = Array(clean)
return "\(String(c[0..<8]))-\(String(c[8..<12]))-\(String(c[12..<16]))-\(String(c[16..<20]))-\(String(c[20..<32]))"
}
}