Last active
August 4, 2018 20:28
-
-
Save mxcl/c7cebadf7effd130e353bb5e6ec0a774 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private extension UUID { | |
/// a more concise representation of UUIDs | |
var ascii85: String { | |
func convert(_ a: UInt8, _ b: UInt8, _ c: UInt8, _ d: UInt8) -> [UInt8] { | |
if a == 0, b == 0, c == 0, d == 0 { | |
return [122] // "z" | |
} | |
let x = UInt(a) * 52200625 + UInt(b) * 614125 + UInt(c) * 7225 + UInt(d) | |
let c0 = UInt8((x / 52200625) % 85) + 33 | |
let c1 = UInt8((x / 614125) % 85) + 33 | |
let c2 = UInt8((x / 7225) % 85) + 33 | |
let c3 = UInt8((x / 85) % 85) + 33 | |
let c4 = UInt8(x % 85) + 33 | |
return [c0,c1,c2,c3,c4] | |
} | |
var aa = convert(uuid.0, uuid.1, uuid.2, uuid.3) | |
aa += convert(uuid.4, uuid.5, uuid.6, uuid.7) | |
aa += convert(uuid.8, uuid.9, uuid.10, uuid.11) | |
aa += convert(uuid.12, uuid.13, uuid.14, uuid.15) | |
return String(cString: aa) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment