Skip to content

Commit aa720c6

Browse files
committed
Merge pull request #69 from 0xced/NSUUID
NSUUID improvements
2 parents 107d140 + 1d874d7 commit aa720c6

File tree

5 files changed

+106
-5
lines changed

5 files changed

+106
-5
lines changed

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ extern void _CFURLInitWithFileSystemPathRelativeToBase(CFURLRef url, CFStringRef
210210
extern Boolean _CFURLInitWithURLString(CFURLRef url, CFStringRef string, Boolean checkForLegalCharacters, _Nullable CFURLRef baseURL);
211211
extern Boolean _CFURLInitAbsoluteURLWithBytes(CFURLRef url, const UInt8 *relativeURLBytes, CFIndex length, CFStringEncoding encoding, _Nullable CFURLRef baseURL);
212212

213+
extern CFHashCode CFHashBytes(uint8_t *bytes, CFIndex length);
213214
extern CFIndex __CFProcessorCount();
214215
extern uint64_t __CFMemorySize();
215216
extern CFIndex __CFActiveProcessorCount();

Foundation.xcodeproj/project.pbxproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
88D28DE71C13AE9000494606 /* TestNSGeometry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88D28DE61C13AE9000494606 /* TestNSGeometry.swift */; };
201201
C93559291C12C49F009FD6A9 /* TestNSAffineTransform.swift in Sources */ = {isa = PBXBuildFile; fileRef = C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */; };
202202
DCDBB8331C1768AC00313299 /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCDBB8321C1768AC00313299 /* TestNSData.swift */; };
203+
C2A9D75C1C15C08B00993803 /* TestNSUUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */; };
203204
E876A73E1C1180E000F279EC /* TestNSRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = E876A73D1C1180E000F279EC /* TestNSRange.swift */; };
204205
848A30581C137B3500C83206 /* TestNSHTTPCookie.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */; };
205206
EA66F6361BEED03E00136161 /* TargetConditionals.h in Headers */ = {isa = PBXBuildFile; fileRef = EA66F6351BEED03E00136161 /* TargetConditionals.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -528,6 +529,7 @@
528529
88D28DE61C13AE9000494606 /* TestNSGeometry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSGeometry.swift; sourceTree = "<group>"; };
529530
C93559281C12C49F009FD6A9 /* TestNSAffineTransform.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSAffineTransform.swift; sourceTree = "<group>"; };
530531
DCDBB8321C1768AC00313299 /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
532+
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSUUID.swift; sourceTree = "<group>"; };
531533
E876A73D1C1180E000F279EC /* TestNSRange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRange.swift; sourceTree = "<group>"; };
532534
848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestNSHTTPCookie.swift; path = TestFoundation/TestNSHTTPCookie.swift; sourceTree = SOURCE_ROOT; };
533535
EA313DFC1BE7F2E90060A403 /* CFURLComponents_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFURLComponents_Internal.h; sourceTree = "<group>"; };
@@ -667,7 +669,10 @@
667669
5B5D89AB1BBDCD0B00234F36 /* Frameworks */,
668670
5B5D885E1BBC938800234F36 /* Products */,
669671
);
672+
indentWidth = 4;
670673
sourceTree = "<group>";
674+
tabWidth = 4;
675+
usesTabs = 0;
671676
};
672677
5B5D885E1BBC938800234F36 /* Products */ = {
673678
isa = PBXGroup;
@@ -1031,6 +1036,7 @@
10311036
E876A73D1C1180E000F279EC /* TestNSRange.swift */,
10321037
EA66F6411BF1619600136161 /* TestNSSet.swift */,
10331038
EA66F6421BF1619600136161 /* TestNSString.swift */,
1039+
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */,
10341040
EA66F6431BF1619600136161 /* TestNSURL.swift */,
10351041
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
10361042
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */,
@@ -1723,6 +1729,7 @@
17231729
EA66F6501BF1619600136161 /* TestNSNumber.swift in Sources */,
17241730
844DC3331C17584F005611F9 /* TestNSScanner.swift in Sources */,
17251731
E876A73E1C1180E000F279EC /* TestNSRange.swift in Sources */,
1732+
C2A9D75C1C15C08B00993803 /* TestNSUUID.swift in Sources */,
17261733
EA66F6521BF1619600136161 /* TestNSPropertyList.swift in Sources */,
17271734
4DC1D0801C12EEEF00B5948A /* TestNSPipe.swift in Sources */,
17281735
84BA558E1C16F90900F48C54 /* TestNSTimeZone.swift in Sources */,

Foundation/NSUUID.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ public class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
3232
}
3333

3434
public init(UUIDBytes bytes: UnsafePointer<UInt8>) {
35-
memcpy(unsafeBitCast(buffer, UnsafeMutablePointer<Void>.self), UnsafePointer<Void>(bytes), 16)
35+
if (bytes != nil) {
36+
memcpy(unsafeBitCast(buffer, UnsafeMutablePointer<Void>.self), UnsafePointer<Void>(bytes), 16)
37+
} else {
38+
memset(unsafeBitCast(buffer, UnsafeMutablePointer<Void>.self), 0, 16)
39+
}
3640
}
3741

3842
public func getUUIDBytes(uuid: UnsafeMutablePointer<UInt8>) {
@@ -42,8 +46,8 @@ public class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
4246
public var UUIDString: String {
4347
get {
4448
let strPtr = UnsafeMutablePointer<Int8>.alloc(37)
45-
_cf_uuid_unparse_upper(buffer, strPtr)
46-
return String(strPtr)
49+
_cf_uuid_unparse_lower(buffer, strPtr)
50+
return String.fromCString(strPtr)!
4751
}
4852
}
4953

@@ -56,10 +60,28 @@ public class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
5660
}
5761

5862
public required init?(coder: NSCoder) {
59-
63+
NSUnimplemented()
6064
}
6165

6266
public func encodeWithCoder(aCoder: NSCoder) {
63-
67+
NSUnimplemented()
68+
}
69+
70+
public override func isEqual(object: AnyObject?) -> Bool {
71+
if object === self {
72+
return true
73+
} else if let other = object as? NSUUID {
74+
return _cf_uuid_compare(buffer, other.buffer) == 0
75+
} else {
76+
return false
77+
}
78+
}
79+
80+
public override var hash: Int {
81+
return Int(CFHashBytes(buffer, 16))
82+
}
83+
84+
public override var description: String {
85+
return UUIDString
6486
}
6587
}

TestFoundation/TestNSUUID.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
12+
import Foundation
13+
import XCTest
14+
#else
15+
import SwiftFoundation
16+
import SwiftXCTest
17+
#endif
18+
19+
20+
class TestNSUUID : XCTestCase {
21+
22+
var allTests : [(String, () -> ())] {
23+
return [
24+
("test_Equality", test_Equality),
25+
("test_InvalidUUID", test_InvalidUUID),
26+
("test_InitializationWithNil", test_InitializationWithNil),
27+
("test_UUIDString", test_UUIDString),
28+
("test_description", test_description),
29+
// Disabled until NSKeyedArchiver and NSKeyedUnarchiver are implemented
30+
// ("test_NSCoding", test_NSCoding),
31+
]
32+
}
33+
34+
func test_Equality() {
35+
let uuidA = NSUUID(UUIDString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")
36+
let uuidB = NSUUID(UUIDString: "e621e1f8-c36c-495a-93fc-0c247a3e6e5f")
37+
let uuidC = NSUUID(UUIDBytes: [0xe6,0x21,0xe1,0xf8,0xc3,0x6c,0x49,0x5a,0x93,0xfc,0x0c,0x24,0x7a,0x3e,0x6e,0x5f])
38+
let uuidD = NSUUID()
39+
40+
XCTAssertEqual(uuidA, uuidB, "String case must not matter.")
41+
XCTAssertEqual(uuidA, uuidC, "A UUID initialized with a string must be equal to the same UUID initialized with its UnsafePointer<UInt8> equivalent representation.")
42+
XCTAssertNotEqual(uuidC, uuidD, "Two different UUIDs must not be equal.")
43+
}
44+
45+
func test_InvalidUUID() {
46+
let uuid = NSUUID(UUIDString: "Invalid UUID")
47+
XCTAssertNil(uuid, "The convenience initializer `init?(UUIDString string:)` must return nil for an invalid UUID string.")
48+
}
49+
50+
func test_InitializationWithNil() {
51+
let uuid = NSUUID(UUIDBytes: nil)
52+
XCTAssertEqual(uuid, NSUUID(UUIDBytes: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]), "The convenience initializer `init(UUIDBytes bytes:)` must return the Nil UUID when UUIDBytes is nil.")
53+
}
54+
55+
func test_UUIDString() {
56+
let uuid = NSUUID(UUIDBytes: [0xe6,0x21,0xe1,0xf8,0xc3,0x6c,0x49,0x5a,0x93,0xfc,0x0c,0x24,0x7a,0x3e,0x6e,0x5f])
57+
XCTAssertEqual(uuid.UUIDString, "e621e1f8-c36c-495a-93fc-0c247a3e6e5f", "The UUIDString representation must be lowercase as defined by RFC 4122.")
58+
}
59+
60+
func test_description() {
61+
let uuid = NSUUID()
62+
XCTAssertEqual(uuid.description, uuid.UUIDString, "The description must be the same as the UUIDString.")
63+
}
64+
65+
func test_NSCoding() {
66+
let uuidA = NSUUID()
67+
let uuidB = NSKeyedUnarchiver.unarchiveObjectWithData(NSKeyedArchiver.archivedDataWithRootObject(uuidA)) as! NSUUID
68+
XCTAssertEqual(uuidA, uuidB, "Archived then unarchived uuid must be equal.")
69+
}
70+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ XCTMain([
4242
TestNSScanner(),
4343
TestNSHTTPCookie(),
4444
TestNSGeometry(),
45+
TestNSUUID(),
4546
])
4647

0 commit comments

Comments
 (0)