Skip to content

Commit db9232f

Browse files
authored
Merge pull request #73550 from Azoy/string-debugger-changes
[debug] Print strings using their description in the debugger
2 parents e5a1d61 + 528160d commit db9232f

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

β€Žstdlib/public/core/DebuggerSupport.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,13 @@ public enum _DebuggerSupport {
238238
print("\(name) : ", terminator: "", to: &target)
239239
}
240240

241-
if let str = asStringRepresentation(value: value, mirror: mirror, count: count) {
241+
if isRoot, let value = value as? String {
242+
// We don't want to use string's debug desciprtion for 'po' because it
243+
// escapes the string and prints it raw (e.g. prints "\n" instead of
244+
// actually printing a newline), but only if its the root value. Otherwise,
245+
// continue using the debug description.
246+
print(value, terminator: "", to: &target)
247+
} else if let str = asStringRepresentation(value: value, mirror: mirror, count: count) {
242248
print(str, terminator: "", to: &target)
243249
}
244250

β€Žtest/stdlib/BridgedObjectDebuggerSupport.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ StringForPrintObjectTests.test("NSStringUTF8") {
7777
let debug = debugVal(&newNSUTF16)
7878
expectEqual("πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ", String(reflecting: nsUTF16))
7979
expectEqual("\"πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ\"", String(reflecting: newNSUTF16))
80-
expectEqual("\"πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ\"\n", printed)
80+
81+
if #available(SwiftStdlib 6.0, *) {
82+
expectEqual("πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ\n", printed)
83+
} else {
84+
expectEqual("\"πŸ‚β˜ƒβ…β†β„οΈŽβ›„οΈβ„οΈ\"\n", printed)
85+
}
8186
expectEqual(printed, debug)
8287
}
8388

β€Žtest/stdlib/DebuggerSupport.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ StringForPrintObjectTests.test("DontBridgeThisStruct") {
101101
}
102102
#endif
103103

104+
if #available(SwiftStdlib 6.0, *) {
105+
StringForPrintObjectTests.test("String") {
106+
let printed = _stringForPrintObject("hello\nworld")
107+
expectEqual(printed, "hello\nworld\n")
108+
}
109+
}
110+
104111
class RefCountedObj {
105112
var patatino : Int
106113
init(_ p : Int) {

0 commit comments

Comments
Β (0)