mirror of
https://github.com/apple/swift-system
synced 2026-11-23 04:34:26 +00:00
Add socket APIs to swift-system (#281) Port the long-standing sockets implementation from #30 to the new staging branch. This adds comprehensive low-level Swift APIs for POSIX socket programming. Original Co-authored-by: Karoy Lorentey <karoly@apple.com>
30 lines
847 B
Swift
30 lines
847 B
Swift
/*
|
|
This source file is part of the Swift System open source project
|
|
|
|
Copyright (c) 2021 - 2025 Apple Inc. and the Swift System project authors
|
|
Licensed under Apache License v2.0 with Runtime Library Exception
|
|
|
|
See https://swift.org/LICENSE.txt for license information
|
|
*/
|
|
|
|
extension String {
|
|
internal init(
|
|
_unsafeUninitializedCapacity capacity: Int,
|
|
initializingUTF8With body: (UnsafeMutableBufferPointer<UInt8>) throws -> Int
|
|
) rethrows {
|
|
if #available(macOS 11, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
|
|
self = try String(
|
|
unsafeUninitializedCapacity: capacity,
|
|
initializingUTF8With: body)
|
|
return
|
|
}
|
|
|
|
let array = try Array<UInt8>(
|
|
unsafeUninitializedCapacity: capacity
|
|
) { buffer, count in
|
|
count = try body(buffer)
|
|
}
|
|
self = String(decoding: array, as: UTF8.self)
|
|
}
|
|
}
|