1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
@_implementationOnly import CoreFoundation
open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding, @unchecked Sendable {
internal var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
deinit {
buffer.deinitialize(count: 1)
buffer.deallocate()
}
public override init() {
_cf_uuid_generate_random(buffer)
}
public convenience init?(uuidString string: String) {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
defer {
buffer.deinitialize(count: 1)
buffer.deallocate()
}
if _cf_uuid_parse(string, buffer) != 0 {
return nil
}
self.init(uuidBytes: buffer)
}
public init(uuidBytes bytes: UnsafePointer<UInt8>) {
memcpy(UnsafeMutableRawPointer(buffer), UnsafeRawPointer(bytes), 16)
}
open func getBytes(_ uuid: UnsafeMutablePointer<UInt8>) {
_cf_uuid_copy(uuid, buffer)
}
open var uuidString: String {
let strPtr = UnsafeMutablePointer<Int8>.allocate(capacity: 37)
defer {
strPtr.deinitialize(count: 1)
strPtr.deallocate()
}
_cf_uuid_unparse_upper(buffer, strPtr)
return String(cString: strPtr)
}
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any {
return self
}
public static var supportsSecureCoding: Bool {
return true
}
public convenience required init?(coder: NSCoder) {
guard coder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let decodedData : Data? = coder.withDecodedUnsafeBufferPointer(forKey: "NS.uuidbytes") {
guard let buffer = $0 else { return nil }
return Data(buffer: buffer)
}
guard let data = decodedData else { return nil }
guard data.count == 16 else { return nil }
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
defer {
buffer.deinitialize(count: 1)
buffer.deallocate()
}
data.copyBytes(to: buffer, count: 16)
self.init(uuidBytes: buffer)
}
open func encode(with aCoder: NSCoder) {
aCoder.encodeBytes(buffer, length: 16, forKey: "NS.uuidbytes")
}
open override func isEqual(_ value: Any?) -> Bool {
switch value {
case let other as FoundationEssentials.UUID:
return other.uuid.0 == buffer[0] &&
other.uuid.1 == buffer[1] &&
other.uuid.2 == buffer[2] &&
other.uuid.3 == buffer[3] &&
other.uuid.4 == buffer[4] &&
other.uuid.5 == buffer[5] &&
other.uuid.6 == buffer[6] &&
other.uuid.7 == buffer[7] &&
other.uuid.8 == buffer[8] &&
other.uuid.9 == buffer[9] &&
other.uuid.10 == buffer[10] &&
other.uuid.11 == buffer[11] &&
other.uuid.12 == buffer[12] &&
other.uuid.13 == buffer[13] &&
other.uuid.14 == buffer[14] &&
other.uuid.15 == buffer[15]
case let other as NSUUID:
return other === self || _cf_uuid_compare(buffer, other.buffer) == 0
default:
return false
}
}
open override var hash: Int {
return Int(bitPattern: CFHashBytes(buffer, 16))
}
open override var description: String {
return uuidString
}
}
extension NSUUID : _StructTypeBridgeable {
public typealias _StructType = FoundationEssentials.UUID
public func _bridgeToSwift() -> FoundationEssentials.UUID {
return FoundationEssentials.UUID._unconditionallyBridgeFromObjectiveC(self)
}
}
|