Skip to content

Commit

Permalink
Add LosslessStringConvertible conformance to BluetoothUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 18, 2024
1 parent e855eb1 commit 9e389d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
60 changes: 48 additions & 12 deletions Sources/Bluetooth/BluetoothUUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ public extension BluetoothUUID {
}
}

// MARK: - Equatable

extension BluetoothUUID: Equatable {

public static func == (lhs: BluetoothUUID, rhs: BluetoothUUID) -> Bool {
switch (lhs, rhs) {
case let (.bit16(lhsValue), .bit16(rhsValue)):
return lhsValue == rhsValue
case let (.bit32(lhsValue), .bit32(rhsValue)):
return lhsValue == rhsValue
case let (.bit128(lhsValue), .bit128(rhsValue)):
return lhsValue == rhsValue
default:
return false
}
}
}

// MARK: - CustomStringConvertible

extension BluetoothUUID: CustomStringConvertible {
Expand All @@ -44,21 +62,39 @@ extension BluetoothUUID: CustomStringConvertible {
}
}

// MARK: - Equatable
// MARK: - LosslessStringConvertible

extension BluetoothUUID: Equatable {
extension BluetoothUUID: LosslessStringConvertible {

public static func == (lhs: BluetoothUUID, rhs: BluetoothUUID) -> Bool {
switch (lhs, rhs) {
case let (.bit16(lhsValue), .bit16(rhsValue)):
return lhsValue == rhsValue
case let (.bit32(lhsValue), .bit32(rhsValue)):
return lhsValue == rhsValue
case let (.bit128(lhsValue), .bit128(rhsValue)):
return lhsValue == rhsValue
default:
return false
public init?(_ string: String) {
#if !os(WASI) && !hasFeature(Embedded)
var rawValue = string
var name: String?
// Find UUID name
let components = string.split(
maxSplits: 1,
omittingEmptySubsequences: true,
whereSeparator: { $0 == " " }
)
if components.count == 2 {
rawValue = String(components[0])
name = String(components[1])
// remove parenthesis
if name?.first == "(", name?.last == ")" {
name?.removeFirst()
name?.removeLast()
}
}
self.init(rawValue: rawValue)
// validate name
if let name {
guard name == self.name else {
return nil
}
}
#else
self.init(rawValue: string)
#endif
}
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/BluetoothTests/BluetoothUUIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,25 @@ final class BluetoothUUIDTests: XCTestCase {
}
}

func testLosslessStringConvertible() {

let data: [(BluetoothUUID, String)] = [
(.bit16(0x1800), "1800"),
(.bit16(0x1800), "1800 (Generic Access Profile)"),
(.bit16(0xFEA9), "FEA9"),
(.bit16(0xFEA9), "FEA9 (Savant Systems LLC)"),
(.bit32(0x12345678).bit128, "12345678-0000-1000-8000-00805F9B34FB")
]

for (uuid, string) in data {
guard let parsed = BluetoothUUID(string) else {
XCTFail("Unable to parse: \(string)")
continue
}
XCTAssertEqual(parsed, uuid)
}
}

func testPerformanceStringParse() {

let uuids = randomUUIDs.map { $0.uuidString }
Expand Down

0 comments on commit 9e389d1

Please sign in to comment.