Skip to content

Commit 0794b70

Browse files
authored
Merge pull request #64725 from apple/egorzhdan/5.9-std-string-concat
🍒[cxx-interop] Add operators for comparing and concatenating `std::string`s
2 parents 97c97d5 + e176b2b commit 0794b70

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

stdlib/public/Cxx/std/String.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ extension std.u16string: ExpressibleByStringLiteral {
5353
}
5454
}
5555

56+
// MARK: Concatenating and comparing C++ strings
57+
58+
extension std.string: Equatable {
59+
public static func ==(lhs: std.string, rhs: std.string) -> Bool {
60+
return lhs.compare(rhs) == 0
61+
}
62+
63+
public static func +=(lhs: inout std.string, rhs: std.string) {
64+
lhs.__appendUnsafe(rhs) // ignore the returned pointer
65+
}
66+
67+
public static func +(lhs: std.string, rhs: std.string) -> std.string {
68+
var copy = lhs
69+
copy += rhs
70+
return copy
71+
}
72+
}
73+
74+
extension std.u16string: Equatable {
75+
public static func ==(lhs: std.u16string, rhs: std.u16string) -> Bool {
76+
return lhs.compare(rhs) == 0
77+
}
78+
79+
public static func +=(lhs: inout std.u16string, rhs: std.u16string) {
80+
lhs.__appendUnsafe(rhs) // ignore the returned pointer
81+
}
82+
83+
public static func +(lhs: std.u16string, rhs: std.u16string) -> std.u16string {
84+
var copy = lhs
85+
copy += rhs
86+
return copy
87+
}
88+
}
89+
5690
// MARK: Getting a Swift description of a C++ string
5791

5892
extension std.string: CustomDebugStringConvertible {

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,46 @@ StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
4545
expectEqual(swift7, "���")
4646
}
4747

48+
StdStringOverlayTestSuite.test("std::string operators") {
49+
var s1 = std.string("something")
50+
let s2 = std.string("123")
51+
let sum = s1 + s2
52+
expectEqual(sum, std.string("something123"))
53+
54+
expectFalse(s1 == s2)
55+
let s3 = std.string("something123")
56+
expectFalse(s1 == s3)
57+
expectFalse(s2 == s3)
58+
59+
s1 += s2
60+
expectTrue(s1 == std.string("something123"))
61+
expectTrue(s1 == s3)
62+
63+
// Make sure the operators work together with ExpressibleByStringLiteral conformance.
64+
s1 += "literal"
65+
expectTrue(s1 == "something123literal")
66+
}
67+
68+
StdStringOverlayTestSuite.test("std::u16string operators") {
69+
var s1 = std.u16string("something")
70+
let s2 = std.u16string("123")
71+
let sum = s1 + s2
72+
expectEqual(sum, std.u16string("something123"))
73+
74+
expectFalse(s1 == s2)
75+
let s3 = std.u16string("something123")
76+
expectFalse(s1 == s3)
77+
expectFalse(s2 == s3)
78+
79+
s1 += s2
80+
expectTrue(s1 == std.u16string("something123"))
81+
expectTrue(s1 == s3)
82+
83+
// Make sure the operators work together with ExpressibleByStringLiteral conformance.
84+
s1 += "literal"
85+
expectTrue(s1 == "something123literal")
86+
}
87+
4888
StdStringOverlayTestSuite.test("std::u16string <=> Swift.String") {
4989
let cxx1 = std.u16string()
5090
let swift1 = String(cxx1)

0 commit comments

Comments
 (0)