Merge pull request #2101 from ewanmellor/swift-fix-uuid-extension
Implement UUID.init(mostSigBits, leastSigBits) in the Swift runtime.
This commit is contained in:
commit
a7af99b17f
|
@ -1,41 +0,0 @@
|
|||
///
|
||||
/// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
/// Use of this file is governed by the BSD 3-clause license that
|
||||
/// can be found in the LICENSE.txt file in the project root.
|
||||
///
|
||||
|
||||
//
|
||||
// NSUUIDExtension.swift
|
||||
// objc2swiftwithswith
|
||||
//
|
||||
// Created by janyou on 15/9/8.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
extension UUID {
|
||||
|
||||
public init(mostSigBits: Int64, leastSigBits: Int64) {
|
||||
let uuid: String = ""
|
||||
self.init(uuidString: uuid)!
|
||||
}
|
||||
|
||||
private func toUUID(_ mostSigBits: Int64, _ leastSigBits: Int64) -> String {
|
||||
|
||||
return (digits(mostSigBits >> 32, 8) + "-" +
|
||||
digits(mostSigBits >> 16, 4) + "-" +
|
||||
digits(mostSigBits, 4) + "-" +
|
||||
digits(leastSigBits >> 48, 4) + "-" +
|
||||
digits(leastSigBits, 12))
|
||||
}
|
||||
|
||||
private func digits(_ val: Int64, _ digits: Int) -> String {
|
||||
let hi = Int64(1) << Int64(digits * 4)
|
||||
let intLiteral = hi | (val & (hi - 1))
|
||||
let s: String = String(Character(UnicodeScalar(UInt32(intLiteral))!))
|
||||
return s[1 ..< s.length]
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
///
|
||||
/// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
|
||||
/// Use of this file is governed by the BSD 3-clause license that
|
||||
/// can be found in the LICENSE.txt file in the project root.
|
||||
///
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
extension UUID {
|
||||
public init(mostSigBits: Int64, leastSigBits: Int64) {
|
||||
let bytes = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
|
||||
defer {
|
||||
bytes.deallocate(capacity: 16)
|
||||
}
|
||||
bytes.withMemoryRebound(to: Int64.self, capacity: 2) {
|
||||
$0.pointee = leastSigBits
|
||||
$0.advanced(by: 1).pointee = mostSigBits
|
||||
}
|
||||
let u = NSUUID(uuidBytes: bytes)
|
||||
self.init(uuidString: u.uuidString)!
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue