Skip to content

Commit

Permalink
#154 Add String.init(utf8:)
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 5, 2024
1 parent b967167 commit ea0dd13
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Sources/Bluetooth/Extensions/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// String.swift
// Bluetooth
//
// Created by Alsey Coleman Miller on 11/4/24.
//

internal extension String {

/// Initialize from UTF8 data.
init?<Data: DataContainer>(utf8 data: Data) {
#if hasFeature(Embedded)
self.init(validating: data, as: UTF8.self)
#else
if #available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *) {
self.init(validating: data, as: UTF8.self)
} else {
self.init(bytes: data, encoding: .utf8)
}
#endif
}
}
22 changes: 22 additions & 0 deletions Sources/BluetoothGAP/Extensions/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// String.swift
// Bluetooth
//
// Created by Alsey Coleman Miller on 11/4/24.
//

internal extension String {

/// Initialize from UTF8 data.
init?<Data: DataContainer>(utf8 data: Data) {
#if hasFeature(Embedded)
self.init(validating: data, as: UTF8.self)
#else
if #available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *) {
self.init(validating: data, as: UTF8.self)
} else {
self.init(bytes: data, encoding: .utf8)
}
#endif
}
}

0 comments on commit ea0dd13

Please sign in to comment.