Skip to content

Commit e770bbe

Browse files
authored
Merge pull request #63973 from apple/egorzhdan/u16string-description
[cxx-interop] Conform `std::u16string` to `CustomStringConvertible` and `CustomDebugStringConvertible`
2 parents bd35cdb + ed0cee9 commit e770bbe

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

stdlib/public/Cxx/std/String.swift

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

56+
// MARK: Getting a Swift description of a C++ string
57+
5658
extension std.string: CustomDebugStringConvertible {
5759
public var debugDescription: String {
5860
return "std.string(\(String(self)))"
5961
}
6062
}
6163

64+
extension std.u16string: CustomDebugStringConvertible {
65+
public var debugDescription: String {
66+
return "std.u16string(\(String(self)))"
67+
}
68+
}
69+
6270
extension std.string: CustomStringConvertible {
6371
public var description: String {
6472
return String(self)
6573
}
6674
}
6775

76+
extension std.u16string: CustomStringConvertible {
77+
public var description: String {
78+
return String(self)
79+
}
80+
}
81+
6882
// MARK: Initializing Swift String from a C++ string
6983

7084
extension String {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ StdStringOverlayTestSuite.test("std::string as Swift.CustomDebugStringConvertibl
9090
expectEqual(cxx3.debugDescription, "std.string(���)")
9191
}
9292

93+
StdStringOverlayTestSuite.test("std::u16string as Swift.CustomDebugStringConvertible") {
94+
let cxx1 = std.u16string()
95+
expectEqual(cxx1.debugDescription, "std.u16string()")
96+
97+
let cxx2 = std.u16string("something123")
98+
expectEqual(cxx2.debugDescription, "std.u16string(something123)")
99+
100+
let scalars: [UInt16] = [97, 55296, 99]
101+
var cxx3 = std.u16string()
102+
for scalar in scalars {
103+
cxx3.push_back(scalar)
104+
}
105+
expectEqual(cxx3.debugDescription, "std.u16string(a�c)")
106+
}
107+
93108
StdStringOverlayTestSuite.test("std::string as Swift.Sequence") {
94109
let cxx1 = std.string()
95110
var iterated = false
@@ -124,4 +139,19 @@ StdStringOverlayTestSuite.test("std::string as CustomStringConvertible") {
124139
expectEqual(cxx3.description, "���")
125140
}
126141

142+
StdStringOverlayTestSuite.test("std::u16string as Swift.CustomStringConvertible") {
143+
let cxx1 = std.u16string()
144+
expectEqual(cxx1.description, "")
145+
146+
let cxx2 = std.u16string("something123")
147+
expectEqual(cxx2.description, "something123")
148+
149+
let scalars: [UInt16] = [97, 55296, 99]
150+
var cxx3 = std.u16string()
151+
for scalar in scalars {
152+
cxx3.push_back(scalar)
153+
}
154+
expectEqual(cxx3.description, "a�c")
155+
}
156+
127157
runAllTests()

0 commit comments

Comments
 (0)