|
| 1 | +// |
| 2 | +// PBXGlobalID+Generator.swift |
| 3 | +// XcodeProject |
| 4 | +// |
| 5 | +// Created by Geoffrey Foster on 2018-12-02. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension PBXGlobalID { |
| 11 | + struct Generator { |
| 12 | + private struct GlobalIdentifier: CustomStringConvertible { |
| 13 | + let userHash: UInt8 |
| 14 | + let pidByte: UInt8 |
| 15 | + var random: UInt16 |
| 16 | + var time: UInt32 |
| 17 | + let zero: UInt8 = 0 |
| 18 | + let hostShift: UInt8 |
| 19 | + let hostHigh: UInt8 |
| 20 | + let hostLow: UInt8 |
| 21 | + |
| 22 | + var description: String { |
| 23 | + let components = [ |
| 24 | + userHash.hexRepresentation, |
| 25 | + pidByte.hexRepresentation, |
| 26 | + random.hexRepresentation, |
| 27 | + time.hexRepresentation, |
| 28 | + zero.hexRepresentation, |
| 29 | + hostShift.hexRepresentation, |
| 30 | + hostHigh.hexRepresentation, |
| 31 | + hostLow.hexRepresentation, |
| 32 | + ] |
| 33 | + return components.joined() |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private var gid: GlobalIdentifier |
| 38 | + |
| 39 | + private var randomSequence: UInt16 = 0 |
| 40 | + private let referenceDateGenerator: () -> Date |
| 41 | + |
| 42 | + private var lastTime: UInt32 = 0 |
| 43 | + private var firstSequence: UInt16 = 0 |
| 44 | + |
| 45 | + init(userName: String = NSUserName(), processId: pid_t = getpid(), random: inout RandomNumberGenerator, referenceDateGenerator: @escaping () -> Date = Generator.referenceDate) { |
| 46 | + self.referenceDateGenerator = referenceDateGenerator |
| 47 | + |
| 48 | + var hostId: UInt32 = UInt32(gethostid()) |
| 49 | + if hostId == 0 { |
| 50 | + hostId = random.next() |
| 51 | + } |
| 52 | + self.gid = GlobalIdentifier( |
| 53 | + userHash: Generator.userHash(userName: userName), |
| 54 | + pidByte: UInt8(truncatingIfNeeded: processId), |
| 55 | + random: random.next(), |
| 56 | + time: 0, |
| 57 | + hostShift: UInt8(truncatingIfNeeded: (hostId >> 0x10) & 0xff), |
| 58 | + hostHigh: UInt8(truncatingIfNeeded: (hostId & 0xff00) >> 0x8), |
| 59 | + hostLow: UInt8(truncatingIfNeeded: hostId & 0xff) |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + mutating func next() -> String { |
| 64 | + let randomValue = gid.random + 1 |
| 65 | + |
| 66 | + let time = UInt32(referenceDateGenerator().timeIntervalSinceReferenceDate) |
| 67 | + if time > lastTime { |
| 68 | + firstSequence = randomValue |
| 69 | + lastTime = time |
| 70 | + } else if firstSequence == randomValue { |
| 71 | + lastTime += 1 |
| 72 | + } |
| 73 | + |
| 74 | + gid.random = randomValue |
| 75 | + gid.time = lastTime |
| 76 | + |
| 77 | + return gid.description |
| 78 | + } |
| 79 | + |
| 80 | + static func referenceDate() -> Date { |
| 81 | + return Date() |
| 82 | + } |
| 83 | + |
| 84 | + static func userHash(userName: String) -> UInt8 { |
| 85 | + func hash(character: UnicodeScalar) -> UInt32 { |
| 86 | + let uppercaseA: UnicodeScalar = "A" |
| 87 | + let uppercaseZ: UnicodeScalar = "Z" |
| 88 | + let lowercaseA: UnicodeScalar = "a" |
| 89 | + let zero: UnicodeScalar = "0" |
| 90 | + switch character { |
| 91 | + case "A"..."Z": |
| 92 | + return character.value - uppercaseA.value |
| 93 | + case "a"..."z": |
| 94 | + return character.value - lowercaseA.value |
| 95 | + case "0"..."9": |
| 96 | + return uppercaseZ.value - uppercaseA.value + 1 + (UInt32(character.value - zero.value) % 5) |
| 97 | + default: |
| 98 | + return 31 |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + var userHash: UInt32 = 0 |
| 103 | + var hashValue: UInt32 = 0 |
| 104 | + userName.unicodeScalars.map { hash(character: $0) }.forEach { |
| 105 | + var h = $0 |
| 106 | + if h != 0 { |
| 107 | + h = ((h << hashValue) >> 8) | (h << hashValue) |
| 108 | + } |
| 109 | + hashValue = (hashValue + 5) & 7 |
| 110 | + userHash = userHash ^ h |
| 111 | + } |
| 112 | + return UInt8(truncatingIfNeeded: userHash) |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +extension BinaryInteger { |
| 118 | + var hexRepresentation: String { |
| 119 | + let hex = String(self, radix: 16, uppercase: true) |
| 120 | + let byteCount = bitWidth / 4 |
| 121 | + let padding = max(byteCount - hex.count, 0) |
| 122 | + return String(repeating: "0", count: padding) + hex |
| 123 | + } |
| 124 | +} |
0 commit comments