Skip to content

Commit 35553d9

Browse files
authored
Merge pull request swiftlang#62698 from whiteio/whiteio/add-customdebugstringconvertible-conformance-to-std-string
Make std::string conform to CustomDebugStringConvertible
2 parents ba26a1f + 70a7a88 commit 35553d9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

stdlib/public/Cxx/std/String.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ extension std.string: ExpressibleByStringLiteral {
2929
}
3030
}
3131

32+
extension std.string: CustomDebugStringConvertible {
33+
public var debugDescription: String {
34+
return "std.string(\(String(cxxString: self)))"
35+
}
36+
}
37+
3238
extension String {
3339
/// Creates a String having the same content as the given C++ string.
3440
///

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
3737
expectEqual(swift6, "xyz\0abc")
3838
}
3939

40+
StdStringOverlayTestSuite.test("std::string as Swift.CustomDebugStringConvertible") {
41+
let cxx1 = std.string()
42+
expectEqual(cxx1.debugDescription, "std.string()")
43+
44+
let cxx2 = std.string("something123")
45+
expectEqual(cxx2.debugDescription, "std.string(something123)")
46+
47+
let bytes: [UInt8] = [0xE1, 0xC1, 0xAC]
48+
var cxx3 = std.string()
49+
for byte in bytes {
50+
cxx3.push_back(CChar(bitPattern: byte))
51+
}
52+
expectEqual(cxx3.debugDescription, "std.string(���)")
53+
}
54+
4055
StdStringOverlayTestSuite.test("std::string as Swift.Sequence") {
4156
let cxx1 = std.string()
4257
var iterated = false

0 commit comments

Comments
 (0)