-
Notifications
You must be signed in to change notification settings - Fork 1
WindowsCore: remove Foundation dependency #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright © 2025 Saleem Abdulrasool <compnerd@compnerd.org> | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import WinSDK | ||
|
||
extension String { | ||
/// Calls the given closure with a pointer to the contents of the string, | ||
/// represented as a null-terminated UTF-16 encoded C string. | ||
/// | ||
/// This uses temporary stack allocation when possible, avoiding heap | ||
/// allocation for reasonably-sized strings. | ||
/// | ||
/// - Parameter body: A closure with a pointer parameter that points to a | ||
/// null-terminated UTF-16 string. If `body` has a return value, that value | ||
/// is also used as the return value for this method. | ||
/// - Returns: The return value, if any, of the `body` closure parameter. | ||
public func withUTF16CString<R>(_ body: (UnsafePointer<WCHAR>?) throws -> R) rethrows -> R { | ||
let count = self.utf16.count | ||
return try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: count + 1) { pBuffer in | ||
if self.utf16.withContiguousStorageIfAvailable({ pSourceBuffer in | ||
pSourceBuffer.baseAddress?.withMemoryRebound(to: WCHAR.self, capacity: count) { pSource in | ||
pBuffer.baseAddress?.initialize(from: pSource, count: count) | ||
} | ||
}) == nil { | ||
_ = pBuffer.initialize(from: self.utf16) | ||
} | ||
Comment on lines
+20
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The optional chaining on Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
pBuffer[count] = 0 // null terminator | ||
return try body(pBuffer.baseAddress) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
initialize(from:)
method onUnsafeMutableBufferPointer
expects a sequence, butself.utf16
returns aString.UTF16View
which may not provide the expected initialization behavior. Consider usingpBuffer.baseAddress?.initialize(from: self.utf16, count: count)
or explicitly iterate through the UTF16 view to ensure proper initialization.Copilot uses AI. Check for mistakes.