Skip to content
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

Resources: correct string encoding conversion #128

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions swiftwinrt/Resources/Support/HString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
// SPDX-License-Identifier: BSD-3

import C_BINDINGS_MODULE
import Foundation
import ucrt

@_fixed_layout
final public class HString {
internal private(set) var hString: HSTRING?

public init(_ string: String) throws {
self.hString = try string.withWideChars {
var out: HSTRING?
try CHECKED(WindowsCreateString($0, UINT32(string.count), &out))
return out
self.hString = try string.withCString(encodedAs: UTF16.self) {
var result: HSTRING?
try CHECKED(WindowsCreateString($0, UINT32(wcslen($0)), &result))
return result
}
}

Expand Down
11 changes: 0 additions & 11 deletions swiftwinrt/Resources/Support/StringHelpers.swift

This file was deleted.

7 changes: 3 additions & 4 deletions swiftwinrt/Resources/Support/WinSDK+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ extension HWND {

extension WCHAR {
public init(from val: Character) {
let value = try! String(val).withWideChars {
$0[0]
}
self.init(value);
// FIXME(compnerd) this needs to be a failable initializer as there may be a
// surrogate pair required.
self.init(val.utf16.first!)
}
}

Expand Down
1 change: 0 additions & 1 deletion swiftwinrt/resources.rc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ MakeFromAbi RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\MakeFromAbi.sw
Marshaler RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Marshaler.swift"
RawTyped RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\RawTyped.swift"
Runtime+Swift RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Runtime+Swift.swift"
StringHelpers RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\StringHelpers.swift"
Swift+Extensions RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Swift+Extensions.swift"
TrustLevel+Swift RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\TrustLevel+Swift.swift"
WinRTWrapperBase RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\WinRTWrapperBase.swift"
Expand Down