Skip to content

Commit 1523531

Browse files
committed
[stdlib] less assumingMemoryBound, rebind instead
1 parent 8379b24 commit 1523531

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

stdlib/public/core/CString.swift

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +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 = nullTerminatedUTF8.withMemoryRebound(to: UInt8.self, capacity: len) {
49-
String._fromUTF8Repairing(UnsafeBufferPointer(start: $0, count: len)).0
48+
let buffer = UnsafeBufferPointer(start: nullTerminatedUTF8, count: len)
49+
self = buffer.withMemoryRebound(to: UInt8.self) {
50+
String._fromUTF8Repairing($0).0
5051
}
5152
}
5253

5354
@inlinable
5455
@_alwaysEmitIntoClient
5556
public init(cString nullTerminatedUTF8: [CChar]) {
56-
self = nullTerminatedUTF8.withUnsafeBytes {
57-
String(_checkingCString: $0.assumingMemoryBound(to: UInt8.self))
57+
self = nullTerminatedUTF8.withUnsafeBufferPointer {
58+
$0.withMemoryRebound(to: UInt8.self, String.init(_checkingCString:))
5859
}
5960
}
6061

@@ -247,14 +248,18 @@ extension String {
247248
guard let cPtr = cString else { return nil }
248249

249250
if _fastPath(encoding == Unicode.UTF8.self) {
250-
let ptr = UnsafeRawPointer(cPtr).assumingMemoryBound(to: UInt8.self)
251-
let len = UTF8._nullCodeUnitOffset(in: ptr)
252-
let codeUnits = UnsafeBufferPointer(start: ptr, count: len)
253-
if isRepairing {
254-
return String._fromUTF8Repairing(codeUnits)
255-
} else {
256-
guard let str = String._tryFromUTF8(codeUnits) else { return nil }
257-
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
258263
}
259264
}
260265

@@ -282,16 +287,17 @@ extension String {
282287
}
283288

284289
if _fastPath(encoding == Unicode.UTF8.self) {
285-
return cString.prefix(length).withUnsafeBytes {
286-
buf -> (result: String, repairsMade: Bool)? in
287-
let codeUnits = buf.assumingMemoryBound(to: UInt8.self)
288-
if isRepairing {
289-
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
290300
}
291-
else if let str = String._tryFromUTF8(codeUnits) {
292-
return (str, false)
293-
}
294-
return nil
295301
}
296302
}
297303

test/stdlib/StringAPICString.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ CStringTests.test("String.cString.with.Array.CChar.input") {
254254
do {
255255
let (u8p, dealloc) = getASCIIUTF8()
256256
defer { dealloc() }
257-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: CChar.self)
258-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
259-
let str = String(cString: Array(buffer))
257+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
258+
let str = buffer.withMemoryRebound(to: CChar.self) {
259+
String(cString: Array($0))
260+
}
260261
str.withCString {
261262
$0.withMemoryRebound(to: UInt8.self, capacity: buffer.count) {
262263
expectEqualCString(u8p, $0)
@@ -324,9 +325,10 @@ CStringTests.test("String.validatingUTF8.with.Array.input") {
324325
do {
325326
let (u8p, dealloc) = getASCIIUTF8()
326327
defer { dealloc() }
327-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: CChar.self)
328-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
329-
let str = String(validatingUTF8: Array(buffer))
328+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
329+
let str = buffer.withMemoryRebound(to: CChar.self) {
330+
String(validatingUTF8: Array($0))
331+
}
330332
expectNotNil(str)
331333
str?.withCString {
332334
$0.withMemoryRebound(to: UInt8.self, capacity: buffer.count) {
@@ -382,9 +384,10 @@ CStringTests.test("String.decodeCString.with.Array.input") {
382384
do {
383385
let (u8p, dealloc) = getASCIIUTF8()
384386
defer { dealloc() }
385-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: Unicode.UTF8.CodeUnit.self)
386-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
387-
let result = String.decodeCString(Array(buffer), as: Unicode.UTF8.self)
387+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
388+
let result = buffer.withMemoryRebound(to: Unicode.UTF8.CodeUnit.self) {
389+
String.decodeCString(Array($0), as: Unicode.UTF8.self)
390+
}
388391
expectNotNil(result)
389392
expectEqual(result?.repairsMade, false)
390393
result?.result.withCString {
@@ -448,9 +451,10 @@ CStringTests.test("String.init.decodingCString.with.Array.input") {
448451
do {
449452
let (u8p, dealloc) = getASCIIUTF8()
450453
defer { dealloc() }
451-
let cstr = UnsafeRawPointer(u8p).assumingMemoryBound(to: Unicode.UTF8.CodeUnit.self)
452-
let buffer = UnsafeBufferPointer(start: cstr, count: getUTF8Length(u8p)+1)
453-
let str = String(decodingCString: Array(buffer), as: Unicode.UTF8.self)
454+
let buffer = UnsafeBufferPointer(start: u8p, count: getUTF8Length(u8p)+1)
455+
let str = buffer.withMemoryRebound(to: Unicode.UTF8.CodeUnit.self) {
456+
String(decodingCString: Array($0), as: Unicode.UTF8.self)
457+
}
454458
str.withCString {
455459
$0.withMemoryRebound(to: UInt8.self, capacity: buffer.count) {
456460
expectEqualCString(u8p, $0)

0 commit comments

Comments
 (0)