Skip to content

Commit 64b202c

Browse files
committed
Cleaned up UUID related meterials
1 parent aac40b7 commit 64b202c

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

Sources/PerfectLib/Utilities.swift

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,53 +264,66 @@ extension String {
264264
}
265265
}
266266

267-
extension String {
268-
/// Parse uuid string
269-
/// Results undefined if the string is not a valid UUID
270-
public func asUUID() -> uuid_t {
267+
public struct UUID {
268+
let uuid: uuid_t
269+
270+
public init() {
271271
let u = UnsafeMutablePointer<UInt8>(allocatingCapacity: sizeof(uuid_t.self))
272272
defer {
273273
u.deallocateCapacity(sizeof(uuid_t.self))
274274
}
275-
uuid_parse(self, u)
276-
return uuid_fromPointer(u)
275+
uuid_generate_random(u)
276+
self.uuid = uuid_t(u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15])
277277
}
278-
279-
/// Returns a String representing the given uuid_t
280-
public static func fromUUID(uuid: uuid_t) -> String {
278+
279+
public init(_ string: String) {
280+
let u = UnsafeMutablePointer<UInt8>(allocatingCapacity: sizeof(uuid_t.self))
281+
defer {
282+
u.deallocateCapacity(sizeof(uuid_t.self))
283+
}
284+
uuid_parse(string, u)
285+
self.uuid = UUID.uuid_fromPointer(u)
286+
}
287+
288+
private static func uuid_fromPointer(_ u: UnsafeMutablePointer<UInt8>) -> uuid_t {
289+
// is there a better way?
290+
return uuid_t(u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15])
291+
}
292+
293+
init(_ uuid: uuid_t) {
294+
self.uuid = uuid
295+
}
296+
297+
public var string: String {
281298
let u = UnsafeMutablePointer<UInt8>(allocatingCapacity: sizeof(uuid_t.self))
282299
let unu = UnsafeMutablePointer<Int8>(allocatingCapacity: 37) // as per spec. 36 + null
283-
284300
defer {
285301
u.deallocateCapacity(sizeof(uuid_t.self))
286302
unu.deallocateCapacity(37)
287303
}
288304
u[0] = uuid.0;u[1] = uuid.1;u[2] = uuid.2;u[3] = uuid.3;u[4] = uuid.4;u[5] = uuid.5;u[6] = uuid.6;u[7] = uuid.7
289305
u[8] = uuid.8;u[9] = uuid.9;u[10] = uuid.10;u[11] = uuid.11;u[12] = uuid.12;u[13] = uuid.13;u[14] = uuid.14;u[15] = uuid.15
290306
uuid_unparse_lower(u, unu)
291-
292307
return String(validatingUTF8: unu)!
293308
}
294309
}
295310

296-
private func uuid_fromPointer(_ u: UnsafeMutablePointer<UInt8>) -> uuid_t {
297-
// is there a better way?
298-
return uuid_t(u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7], u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15])
299-
}
300-
301-
/// Returns an empty all zeros uuid_t
302-
public func empty_uuid() -> uuid_t {
303-
return uuid_t(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
311+
extension String {
312+
313+
@available(*, unavailable, message: "Use UUID(_:String).uuid")
314+
public func asUUID() -> uuid_t {
315+
return UUID(self).uuid
316+
}
317+
318+
@available(*, unavailable, message: "Use UUID(_:String)")
319+
public static func fromUUID(uuid: uuid_t) -> String {
320+
return UUID(uuid).string
321+
}
304322
}
305323

306-
/// Generate and return a random uuid_t
324+
@available(*, unavailable, renamed: "UUID()")
307325
public func random_uuid() -> uuid_t {
308-
let u = UnsafeMutablePointer<UInt8>(allocatingCapacity: sizeof(uuid_t.self))
309-
defer {
310-
u.deallocateCapacity(sizeof(uuid_t.self))
311-
}
312-
uuid_generate_random(u)
313-
return uuid_fromPointer(u)
326+
return UUID().uuid
314327
}
315328

316329
extension String {

0 commit comments

Comments
 (0)