Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit fcf7745

Browse files
committed
Merge branch 'release/1.1.7'
2 parents b04929a + 5365401 commit fcf7745

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

Sources/SwiftFoundation/Endianness.swift

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,8 @@ public extension EndianConvertible {
2727
/// Converts the number to the current endianness.
2828
var currentEndian: Self {
2929

30-
if isBigEndian {
31-
32-
return bigEndian
33-
34-
} else {
35-
36-
return littleEndian
37-
}
30+
return isBigEndian ? bigEndian : littleEndian
3831
}
3932
}
4033

4134
public let isBigEndian = 10.bigEndian == 10
42-
43-
public extension UInt16 {
44-
45-
/// Initializes value from two little endian ordered bytes.
46-
public init(littleEndian value: (UInt8, UInt8)) {
47-
48-
self = (UInt16(value.0).littleEndian + UInt16(value.1).bigEndian).currentEndian
49-
}
50-
51-
public var littleEndianBytes: (UInt8, UInt8) {
52-
53-
let value = self.littleEndian
54-
55-
let lowerByte = value & 0xff
56-
let higherByte = value >> 8
57-
58-
return (UInt8(truncatingBitPattern: lowerByte), UInt8(truncatingBitPattern: higherByte))
59-
}
60-
}

Sources/SwiftFoundation/Integer.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,18 @@ public extension Int {
2525
}
2626
}
2727

28+
public extension UInt16 {
29+
30+
/// Initializes value from two bytes.
31+
public init(bytes: (UInt8, UInt8)) {
32+
33+
self = unsafeBitCast(bytes, UInt16.self)
34+
}
35+
36+
/// Converts to two bytes.
37+
public var bytes: (UInt8, UInt8) {
38+
39+
return unsafeBitCast(self, (UInt8, UInt8).self)
40+
}
41+
}
42+

Sources/SwiftFoundation/UUID.swift

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ public extension UUID {
4949
}
5050
}
5151

52+
// MARK: - Hashable
53+
54+
extension UUID: Hashable {
55+
56+
public var hashValue: Int {
57+
58+
// more expensive than casting but that's not safe
59+
let integerArray = self.toData().byteValue.map { Int($0) }
60+
var hash = 0
61+
62+
for integer in integerArray {
63+
64+
hash ^= integer
65+
}
66+
67+
return hash
68+
}
69+
}
70+
5271
// MARK: - DataConvertible
5372

5473
extension UUID: DataConvertible {
@@ -57,7 +76,7 @@ extension UUID: DataConvertible {
5776

5877
let byteValue = data.byteValue
5978

60-
guard byteValue.count == 16 else { return nil }
79+
guard byteValue.count == UUID.ByteCount else { return nil }
6180

6281
self.init(byteValue: (byteValue[0], byteValue[1], byteValue[2], byteValue[3], byteValue[4], byteValue[5], byteValue[6], byteValue[7], byteValue[8], byteValue[9], byteValue[10], byteValue[11], byteValue[12], byteValue[13], byteValue[14], byteValue[15]))
6382
}
@@ -67,3 +86,12 @@ extension UUID: DataConvertible {
6786
return Data(byteValue: [byteValue.0, byteValue.1, byteValue.2, byteValue.3, byteValue.4, byteValue.5, byteValue.6, byteValue.7, byteValue.8, byteValue.9, byteValue.10, byteValue.11, byteValue.12, byteValue.13, byteValue.14, byteValue.15])
6887
}
6988
}
89+
90+
// MARK: - Private Constants
91+
92+
private extension UUID {
93+
94+
private static var StringLength: Int { return 36 }
95+
private static var UnformattedUUIDStringLength: Int { return 32 }
96+
private static var ByteCount: Int { return 16 }
97+
}

Sources/UnitTests/UUIDTests.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ final class UUIDTests: XCTestCase {
1515
lazy var allTests: [(String, () throws -> ())] =
1616
[("testCreateRandomUUID", self.testCreateRandomUUID),
1717
("testUUIDString", self.testUUIDString),
18-
("testCreateFromString", self.testCreateFromString)]
18+
("testCreateFromString", self.testCreateFromString),
19+
("testBytes", self.testBytes)]
1920

2021
// MARK: - Functional Tests
2122

@@ -45,4 +46,16 @@ final class UUIDTests: XCTestCase {
4546

4647
XCTAssert((UUID(rawValue: "BadInput") == nil), "UUID should not be created")
4748
}
49+
50+
func testBytes() {
51+
52+
let expectedData = Data(byteValue: [91, 254, 177, 148, 104, 196, 72, 232, 143, 67, 60, 88, 99, 100, 203, 111])
53+
54+
let stringValue = "5BFEB194-68C4-48E8-8F43-3C586364CB6F"
55+
56+
guard let uuid = UUID(rawValue: stringValue)
57+
else { XCTFail("Could not create UUID from " + stringValue); return }
58+
59+
XCTAssert(uuid.toData() == expectedData, "Unexpected UUID data \(uuid.toData().byteValue)")
60+
}
4861
}

Xcode/SwiftFoundation/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.1.5</string>
18+
<string>1.1.7</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

0 commit comments

Comments
 (0)