Created
June 10, 2021 07:25
-
-
Save ctreffs/788122b5f53f6fd6758f3f89e3e263a6 to your computer and use it in GitHub Desktop.
[Swift] C-String buffer pointer from array of Strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Array where Element == String { | |
@inlinable | |
public func withUnsafeCStringBufferPointer<R>(_ body: (UnsafeBufferPointer<UnsafePointer<CChar>?>) throws -> R) rethrows -> R { | |
func translate(_ slice: inout Self.SubSequence, | |
_ offset: inout Int, | |
_ buffer: UnsafeMutableBufferPointer<UnsafePointer<CChar>?>, | |
_ body: (UnsafeBufferPointer<UnsafePointer<CChar>?>) throws -> R) rethrows -> R { | |
guard let string = slice.popFirst() else { | |
return try body(UnsafeBufferPointer(buffer)) | |
} | |
return try string.withCString { cStringPtr in | |
buffer.baseAddress! | |
.advanced(by: offset) | |
.initialize(to: cStringPtr) | |
offset += 1 | |
return try translate(&slice, &offset, buffer, body) | |
} | |
} | |
var slice = self[...] | |
var offset: Int = 0 | |
let buffer = UnsafeMutableBufferPointer<UnsafePointer<CChar>?>.allocate(capacity: count) | |
defer { buffer.deallocate() } | |
return try translate(&slice, &offset, buffer, body) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Inspired by https://forums.swift.org/t/can-an-unsafebufferpointer-to-local-scope-array-outlive-its-own-reference/47070/8