Skip to content

Commit 4c32315

Browse files
authored
Merge pull request #58462 from glessard/rdar91343475-5.7
[5.7][stdlib] remove most uses of _asCChar and _asUInt8
2 parents 3e8d153 + ff6dbdb commit 4c32315

File tree

3 files changed

+52
-40
lines changed

3 files changed

+52
-40
lines changed

stdlib/public/core/CString.swift

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ extension String {
4545
/// - Parameter nullTerminatedUTF8: A pointer to a null-terminated UTF-8 code sequence.
4646
public init(cString nullTerminatedUTF8: UnsafePointer<CChar>) {
4747
let len = UTF8._nullCodeUnitOffset(in: nullTerminatedUTF8)
48-
self = String._fromUTF8Repairing(
49-
UnsafeBufferPointer(start: nullTerminatedUTF8._asUInt8, count: len)).0
48+
let buffer = UnsafeBufferPointer(start: nullTerminatedUTF8, count: len)
49+
self = buffer.withMemoryRebound(to: UInt8.self) {
50+
String._fromUTF8Repairing($0).0
51+
}
5052
}
5153

5254
@inlinable
5355
@_alwaysEmitIntoClient
5456
public init(cString nullTerminatedUTF8: [CChar]) {
55-
self = nullTerminatedUTF8.withUnsafeBytes {
56-
String(_checkingCString: $0.assumingMemoryBound(to: UInt8.self))
57+
self = nullTerminatedUTF8.withUnsafeBufferPointer {
58+
$0.withMemoryRebound(to: UInt8.self, String.init(_checkingCString:))
5759
}
5860
}
5961

@@ -150,8 +152,9 @@ extension String {
150152
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
151153
public init?(validatingUTF8 cString: UnsafePointer<CChar>) {
152154
let len = UTF8._nullCodeUnitOffset(in: cString)
153-
guard let str = String._tryFromUTF8(
154-
UnsafeBufferPointer(start: cString._asUInt8, count: len))
155+
guard let str = cString.withMemoryRebound(to: UInt8.self, capacity: len, {
156+
String._tryFromUTF8(UnsafeBufferPointer(start: $0, count: len))
157+
})
155158
else { return nil }
156159

157160
self = str
@@ -165,9 +168,10 @@ extension String {
165168
"input of String.init(validatingUTF8:) must be null-terminated"
166169
)
167170
}
168-
guard let string = cString.prefix(length).withUnsafeBytes({
169-
String._tryFromUTF8($0.assumingMemoryBound(to: UInt8.self))
170-
}) else { return nil }
171+
guard let string = cString.prefix(length).withUnsafeBufferPointer({
172+
$0.withMemoryRebound(to: UInt8.self, String._tryFromUTF8(_:))
173+
})
174+
else { return nil }
171175

172176
self = string
173177
}
@@ -244,14 +248,18 @@ extension String {
244248
guard let cPtr = cString else { return nil }
245249

246250
if _fastPath(encoding == Unicode.UTF8.self) {
247-
let ptr = UnsafeRawPointer(cPtr).assumingMemoryBound(to: UInt8.self)
248-
let len = UTF8._nullCodeUnitOffset(in: ptr)
249-
let codeUnits = UnsafeBufferPointer(start: ptr, count: len)
250-
if isRepairing {
251-
return String._fromUTF8Repairing(codeUnits)
252-
} else {
253-
guard let str = String._tryFromUTF8(codeUnits) else { return nil }
254-
return (str, false)
251+
let len = UTF8._nullCodeUnitOffset(
252+
in: UnsafeRawPointer(cPtr).assumingMemoryBound(to: UInt8.self)
253+
)
254+
let bytes = UnsafeBufferPointer(start: cPtr, count: len)
255+
return bytes.withMemoryRebound(to: UInt8.self) { codeUnits in
256+
if isRepairing {
257+
return String._fromUTF8Repairing(codeUnits)
258+
}
259+
else if let str = String._tryFromUTF8(codeUnits) {
260+
return (str, false)
261+
}
262+
return nil
255263
}
256264
}
257265

@@ -279,16 +287,17 @@ extension String {
279287
}
280288

281289
if _fastPath(encoding == Unicode.UTF8.self) {
282-
return cString.prefix(length).withUnsafeBytes {
283-
buf -> (result: String, repairsMade: Bool)? in
284-
let codeUnits = buf.assumingMemoryBound(to: UInt8.self)
285-
if isRepairing {
286-
return String._fromUTF8Repairing(codeUnits)
290+
return cString.prefix(length).withUnsafeBufferPointer {
291+
buffer -> (result: String, repairsMade: Bool)? in
292+
return buffer.withMemoryRebound(to: UInt8.self) { codeUnits in
293+
if isRepairing {
294+
return String._fromUTF8Repairing(codeUnits)
295+
}
296+
else if let str = String._tryFromUTF8(codeUnits) {
297+
return (str, false)
298+
}
299+
return nil
287300
}
288-
else if let str = String._tryFromUTF8(codeUnits) {
289-
return (str, false)
290-
}
291-
return nil
292301
}
293302
}
294303

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,7 @@ extension _StringGuts {
167167
_ f: (UnsafeBufferPointer<CChar>) throws -> R
168168
) rethrows -> R {
169169
return try self.withFastUTF8 { utf8 in
170-
let ptr = utf8.baseAddress._unsafelyUnwrappedUnchecked._asCChar
171-
return try f(UnsafeBufferPointer(start: ptr, count: utf8.count))
170+
return try utf8.withMemoryRebound(to: CChar.self, f)
172171
}
173172
}
174173
}

test/stdlib/StringAPICString.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,10 @@ CStringTests.test("String.cString.with.Array.CChar.input") {
253253
do {
254254
let (u8p, dealloc) = getASCIIUTF8()
255255
defer { dealloc() }
256-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: CChar.self)
257-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
258-
let str = String(cString: Array(buffer))
256+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
257+
let str = buffer.withMemoryRebound(to: CChar.self) {
258+
String(cString: Array($0))
259+
}
259260
str.withCString {
260261
$0.withMemoryRebound(to: UInt8.self, capacity: buffer.count) {
261262
expectEqualCString(u8p, $0)
@@ -318,9 +319,10 @@ CStringTests.test("String.validatingUTF8.with.Array.input") {
318319
do {
319320
let (u8p, dealloc) = getASCIIUTF8()
320321
defer { dealloc() }
321-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: CChar.self)
322-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
323-
let str = String(validatingUTF8: Array(buffer))
322+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
323+
let str = buffer.withMemoryRebound(to: CChar.self) {
324+
String(validatingUTF8: Array($0))
325+
}
324326
expectNotNil(str)
325327
str?.withCString {
326328
$0.withMemoryRebound(to: UInt8.self, capacity: buffer.count) {
@@ -373,9 +375,10 @@ CStringTests.test("String.decodeCString.with.Array.input") {
373375
do {
374376
let (u8p, dealloc) = getASCIIUTF8()
375377
defer { dealloc() }
376-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: Unicode.UTF8.CodeUnit.self)
377-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
378-
let result = String.decodeCString(Array(buffer), as: Unicode.UTF8.self)
378+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
379+
let result = buffer.withMemoryRebound(to: Unicode.UTF8.CodeUnit.self) {
380+
String.decodeCString(Array($0), as: Unicode.UTF8.self)
381+
}
379382
expectNotNil(result)
380383
expectEqual(result?.repairsMade, false)
381384
result?.result.withCString {
@@ -436,9 +439,10 @@ CStringTests.test("String.init.decodingCString.with.Array.input") {
436439
do {
437440
let (u8p, dealloc) = getASCIIUTF8()
438441
defer { dealloc() }
439-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: Unicode.UTF8.CodeUnit.self)
440-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
441-
let str = String(decodingCString: Array(buffer), as: Unicode.UTF8.self)
442+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
443+
let str = buffer.withMemoryRebound(to: Unicode.UTF8.CodeUnit.self) {
444+
String(decodingCString: Array($0), as: Unicode.UTF8.self)
445+
}
442446
str.withCString {
443447
$0.withMemoryRebound(to: UInt8.self, capacity: buffer.count) {
444448
expectEqualCString(u8p, $0)

0 commit comments

Comments
 (0)