@@ -45,8 +45,9 @@ extension String {
45
45
/// - Parameter nullTerminatedUTF8: A pointer to a null-terminated UTF-8 code sequence.
46
46
public init ( cString nullTerminatedUTF8: UnsafePointer < CChar > ) {
47
47
let len = UTF8 . _nullCodeUnitOffset ( in: nullTerminatedUTF8)
48
- self = String . _fromUTF8Repairing (
49
- UnsafeBufferPointer ( start: nullTerminatedUTF8. _asUInt8, count: len) ) . 0
48
+ self = nullTerminatedUTF8. withMemoryRebound ( to: UInt8 . self, capacity: len) {
49
+ String . _fromUTF8Repairing ( UnsafeBufferPointer ( start: $0, count: len) ) . 0
50
+ }
50
51
}
51
52
52
53
@inlinable
@@ -150,8 +151,9 @@ extension String {
150
151
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
151
152
public init ? ( validatingUTF8 cString: UnsafePointer < CChar > ) {
152
153
let len = UTF8 . _nullCodeUnitOffset ( in: cString)
153
- guard let str = String . _tryFromUTF8 (
154
- UnsafeBufferPointer ( start: cString. _asUInt8, count: len) )
154
+ guard let str = cString. withMemoryRebound ( to: UInt8 . self, capacity: len, {
155
+ String . _tryFromUTF8 ( UnsafeBufferPointer ( start: $0, count: len) )
156
+ } )
155
157
else { return nil }
156
158
157
159
self = str
@@ -165,9 +167,10 @@ extension String {
165
167
" input of String.init(validatingUTF8:) must be null-terminated "
166
168
)
167
169
}
168
- guard let string = cString. prefix ( length) . withUnsafeBytes ( {
169
- String . _tryFromUTF8 ( $0. assumingMemoryBound ( to: UInt8 . self) )
170
- } ) else { return nil }
170
+ guard let string = cString. prefix ( length) . withUnsafeBufferPointer ( {
171
+ $0. withMemoryRebound ( to: UInt8 . self, String . _tryFromUTF8 ( _: ) )
172
+ } )
173
+ else { return nil }
171
174
172
175
self = string
173
176
}
0 commit comments