Skip to content

[cxx-interop] Make std::string::append usable from Swift #66753

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
Jun 20, 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
14 changes: 12 additions & 2 deletions stdlib/public/Cxx/std/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ extension std.string: Equatable {
}

public static func +=(lhs: inout std.string, rhs: std.string) {
lhs.__appendUnsafe(rhs) // ignore the returned pointer
lhs.append(rhs)
}

@inlinable
public mutating func append(_ other: std.string) {
__appendUnsafe(other) // ignore the returned pointer
}

public static func +(lhs: std.string, rhs: std.string) -> std.string {
Expand All @@ -93,7 +98,12 @@ extension std.u16string: Equatable {
}

public static func +=(lhs: inout std.u16string, rhs: std.u16string) {
lhs.__appendUnsafe(rhs) // ignore the returned pointer
lhs.append(rhs)
}

@inlinable
public mutating func append(_ other: std.u16string) {
__appendUnsafe(other) // ignore the returned pointer
}

public static func +(lhs: std.u16string, rhs: std.u16string) -> std.u16string {
Expand Down
14 changes: 14 additions & 0 deletions test/Interop/Cxx/stdlib/overlay/std-string-overlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ StdStringOverlayTestSuite.test("std::u16string operators") {
expectTrue(s1 == "something123literal")
}

StdStringOverlayTestSuite.test("std::string::append") {
var s1 = std.string("0123")
let s2 = std.string("abc")
s1.append(s2)
expectEqual(s1, std.string("0123abc"))
}

StdStringOverlayTestSuite.test("std::u16string::append") {
var s1 = std.u16string("0123")
let s2 = std.u16string("abc")
s1.append(s2)
expectEqual(s1, std.u16string("0123abc"))
}

StdStringOverlayTestSuite.test("std::string as Hashable") {
let s0 = std.string()
let h0 = s0.hashValue
Expand Down