Skip to content

Commit 6cd4b7c

Browse files
committed
[cxx-interop] Make std::string::append usable from Swift
This adds a new Swift overload for `append` that takes another `std::string` as a parameter. The original C++ overload for `append` is not exposed into Swift because it returns a mutable reference `string&`. rdar://107018724
1 parent de45ab1 commit 6cd4b7c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

stdlib/public/Cxx/std/String.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ extension std.string: Equatable {
7777
}
7878

7979
public static func +=(lhs: inout std.string, rhs: std.string) {
80-
lhs.__appendUnsafe(rhs) // ignore the returned pointer
80+
lhs.append(rhs)
81+
}
82+
83+
@inlinable
84+
public mutating func append(_ other: std.string) {
85+
__appendUnsafe(other) // ignore the returned pointer
8186
}
8287

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

95100
public static func +=(lhs: inout std.u16string, rhs: std.u16string) {
96-
lhs.__appendUnsafe(rhs) // ignore the returned pointer
101+
lhs.append(rhs)
102+
}
103+
104+
@inlinable
105+
public mutating func append(_ other: std.u16string) {
106+
__appendUnsafe(other) // ignore the returned pointer
97107
}
98108

99109
public static func +(lhs: std.u16string, rhs: std.u16string) -> std.u16string {

test/Interop/Cxx/stdlib/overlay/std-string-overlay.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ StdStringOverlayTestSuite.test("std::u16string operators") {
8585
expectTrue(s1 == "something123literal")
8686
}
8787

88+
StdStringOverlayTestSuite.test("std::string::append") {
89+
var s1 = std.string("0123")
90+
let s2 = std.string("abc")
91+
s1.append(s2)
92+
expectEqual(s1, std.string("0123abc"))
93+
}
94+
95+
StdStringOverlayTestSuite.test("std::u16string::append") {
96+
var s1 = std.u16string("0123")
97+
let s2 = std.u16string("abc")
98+
s1.append(s2)
99+
expectEqual(s1, std.u16string("0123abc"))
100+
}
101+
88102
StdStringOverlayTestSuite.test("std::string as Hashable") {
89103
let s0 = std.string()
90104
let h0 = s0.hashValue

0 commit comments

Comments
 (0)