Skip to content

[cxx-interop] Add conversions between std::u16string and Swift.String #61695

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

Merged
merged 1 commit into from
Oct 24, 2022
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
29 changes: 29 additions & 0 deletions stdlib/public/Cxx/std/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

// MARK: Initializing C++ string from a Swift String

extension std.string {
public init(_ string: String) {
self.init()
Expand All @@ -19,12 +21,31 @@ extension std.string {
}
}

extension std.u16string {
public init(_ string: String) {
self.init()
for char in string.utf16 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will optimize this in a follow-up patch.

self.push_back(char)
}
}
}

// MARK: Initializing C++ string from a Swift String literal

extension std.string: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}

extension std.u16string: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}

// MARK: Initializing Swift String from a C++ string

extension String {
public init(cxxString: std.string) {
let buffer = UnsafeBufferPointer<CChar>(
Expand All @@ -35,4 +56,12 @@ extension String {
}
withExtendedLifetime(cxxString) {}
}

public init(cxxU16String: std.u16string) {
let buffer = UnsafeBufferPointer<UInt16>(
start: cxxU16String.__dataUnsafe(),
count: cxxU16String.size())
self = String(decoding: buffer, as: UTF16.self)
withExtendedLifetime(cxxU16String) {}
}
}
30 changes: 30 additions & 0 deletions test/Interop/Cxx/stdlib/overlay/std-string-overlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@ StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
expectEqual(swift6, "xyz\0abc")
}

StdStringOverlayTestSuite.test("std::u16string <=> Swift.String") {
let cxx1 = std.u16string()
let swift1 = String(cxxU16String: cxx1)
expectEqual(swift1, "")

let cxx2 = std.u16string("something123")
expectEqual(cxx2.size(), 12)
let swift2 = String(cxxU16String: cxx2)
expectEqual(swift2, "something123")

let cxx3: std.u16string = "literal"
expectEqual(cxx3.size(), 7)

let cxx4: std.u16string = "тест"
expectEqual(cxx4.size(), 4)
let swift4 = String(cxxU16String: cxx4)
expectEqual(swift4, "тест")

// Emojis are represented by more than one CWideChar.
let cxx5: std.u16string = "emoji_🤖"
expectEqual(cxx5.size(), 8)
let swift5 = String(cxxU16String: cxx5)
expectEqual(swift5, "emoji_🤖")

let cxx6 = std.u16string("xyz\0abc")
expectEqual(cxx6.size(), 7)
let swift6 = String(cxxU16String: cxx6)
expectEqual(swift6, "xyz\0abc")
}

extension std.string.const_iterator: UnsafeCxxInputIterator {
// This func should not be required.
public static func ==(lhs: std.string.const_iterator,
Expand Down