Skip to content

Commit ee4d4a3

Browse files
authored
Merge pull request #75433 from lorentey/string-index-printing2
2 parents cee810a + 0cb5767 commit ee4d4a3

File tree

5 files changed

+128
-44
lines changed

5 files changed

+128
-44
lines changed

stdlib/public/core/StringIndex.swift

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -524,42 +524,43 @@ extension String.Index {
524524
}
525525
}
526526

527-
// The definitions below are placeholders for potential future `String.Index`
528-
// conformances to `CustomStringConvertible` and
529-
// `CustomDebugStringConvertible`. They are supplied here to make working with
530-
// string indices somewhat bearable while we're working on adding the actual
531-
// conformances.
532-
533-
/// A textual representation of this instance.
534-
@_alwaysEmitIntoClient
527+
/// A textual representation of this instance, intended for debugging.
528+
///
529+
/// - Important: The contents of the returned string are not guaranteed to
530+
/// remain stable: they may arbitrarily change in any Swift release.
531+
@_alwaysEmitIntoClient // FIXME: Use @backDeployed
535532
@inline(never)
536-
public var _description: String {
533+
public var debugDescription: String {
537534
// 23[utf8]+1
538535
var d = "\(_encodedOffset)[\(_encodingDescription)]"
539536
if transcodedOffset != 0 {
540537
d += "+\(transcodedOffset)"
541538
}
542539
return d
543540
}
541+
}
544542

545-
/// A textual representation of this instance, suitable for debugging.
543+
@available(SwiftStdlib 6.1, *)
544+
extension String.Index: CustomDebugStringConvertible {}
545+
546+
extension String.Index {
547+
/// A textual representation of this instance, intended for debugging.
548+
///
549+
/// - Important: The contents of the returned string are not guaranteed to
550+
/// remain stable: they may arbitrarily change in any Swift release.
546551
@_alwaysEmitIntoClient
547-
@inline(never)
552+
@available(*, deprecated, renamed: "debugDescription")
553+
public var _description: String {
554+
debugDescription
555+
}
556+
557+
/// A textual representation of this instance, intended for debugging.
558+
///
559+
/// - Important: The contents of the returned string are not guaranteed to
560+
/// remain stable: they may arbitrarily change in any Swift release.
561+
@_alwaysEmitIntoClient
562+
@available(*, deprecated, renamed: "debugDescription")
548563
public var _debugDescription: String {
549-
var d = "String.Index("
550-
d += "offset: \(_encodedOffset)[\(_encodingDescription)]"
551-
if transcodedOffset != 0 {
552-
d += "+\(transcodedOffset)"
553-
}
554-
if _isCharacterAligned {
555-
d += ", aligned: character"
556-
} else if _isScalarAligned {
557-
d += ", aligned: scalar"
558-
}
559-
if let stride = characterStride {
560-
d += ", stride: \(stride)"
561-
}
562-
d += ")"
563-
return d
564+
debugDescription
564565
}
565566
}

test/abi/macOS/arm64/stdlib.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,8 @@ Added: _$ss7UnicodeO5ASCIIO27encodedReplacementCharacters15CollectionOfOneVys5UI
767767
Added: _$ss7UnicodeO5UTF16O27encodedReplacementCharacters11_UIntBufferVys6UInt16VGvpZMV
768768
Added: _$ss7UnicodeO5UTF32O27encodedReplacementCharacters15CollectionOfOneVys6UInt32VGvpZMV
769769
Added: _$sSo19_SwiftStdlibVersionasE6v6_1_0ABvpZMV
770+
771+
// SE-0445 Improving printed descriptions of String.Index
772+
Added: _$sSS5IndexV16debugDescriptionSSvpMV
773+
Added: _$sSS5IndexVs28CustomDebugStringConvertiblesMc
774+
Added: _$sSS5IndexVs28CustomDebugStringConvertiblesWP

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,3 +768,8 @@ Added: _$ss7UnicodeO5ASCIIO27encodedReplacementCharacters15CollectionOfOneVys5UI
768768
Added: _$ss7UnicodeO5UTF16O20_replacementCodeUnits6UInt16VvpZMV
769769
Added: _$ss7UnicodeO5UTF16O27encodedReplacementCharacters11_UIntBufferVys6UInt16VGvpZMV
770770
Added: _$ss7UnicodeO5UTF32O27encodedReplacementCharacters15CollectionOfOneVys6UInt32VGvpZMV
771+
772+
// SE-0445 Improving printed descriptions of String.Index
773+
Added: _$sSS5IndexV16debugDescriptionSSvpMV
774+
Added: _$sSS5IndexVs28CustomDebugStringConvertiblesMc
775+
Added: _$sSS5IndexVs28CustomDebugStringConvertiblesWP

test/api-digester/stability-stdlib-abi-without-asserts.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,4 +817,6 @@ Func _SliceBuffer.withUnsafeBufferPointer(_:) has mangled name changing from 'Sw
817817
Func _SliceBuffer.withUnsafeMutableBufferPointer(_:) has been renamed to Func __abi_withUnsafeMutableBufferPointer(_:)
818818
Func _SliceBuffer.withUnsafeMutableBufferPointer(_:) has mangled name changing from 'Swift._SliceBuffer.withUnsafeMutableBufferPointer<A>((Swift.UnsafeMutableBufferPointer<A>) throws -> A1) throws -> A1' to 'Swift._SliceBuffer.__abi_withUnsafeMutableBufferPointer<A>((Swift.UnsafeMutableBufferPointer<A>) throws -> A1) throws -> A1'
819819

820+
Struct String.Index has added a conformance to an existing protocol CustomDebugStringConvertible
821+
820822
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)

test/stdlib/StringIndex.swift

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,23 +1013,22 @@ suite.test("UTF-16 breadcrumbs") {
10131013
c̵̛̘̥̮̙̥̟̘̝͙̤̮͉͔̭̺̺̅̀̽̒̽̏̊̆͒͌̂͌̌̓̈́̐̔̿̂͑͠͝͝ͅ\#
10141014
"""#
10151015

1016-
print(string.utf16.count)
10171016
let indices = Array(string.utf16.indices) + [string.utf16.endIndex]
10181017
for i in 0 ..< indices.count {
10191018
for j in 0 ..< indices.count {
10201019
let distance = string.utf16.distance(from: indices[i], to: indices[j])
10211020
expectEqual(distance, j - i,
10221021
"""
1023-
i: \(i), indices[i]: \(indices[i]._description)
1024-
j: \(j), indices[j]: \(indices[j]._description)
1022+
i: \(i), indices[i]: \(indices[i])
1023+
j: \(j), indices[j]: \(indices[j])
10251024
""")
10261025

10271026
let target = string.utf16.index(indices[i], offsetBy: j - i)
10281027
expectEqual(target, indices[j],
10291028
"""
1030-
i: \(i), indices[i]: \(indices[i]._description)
1031-
j: \(j), indices[j]: \(indices[j]._description)
1032-
target: \(target._description)
1029+
i: \(i), indices[i]: \(indices[i])
1030+
j: \(j), indices[j]: \(indices[j])
1031+
target: \(target)
10331032
""")
10341033
}
10351034
}
@@ -1136,9 +1135,9 @@ if #available(SwiftStdlib 5.8, *) {
11361135
let actual = string._index(roundingDown: index)
11371136
expectEqual(actual, expected,
11381137
"""
1139-
index: \(index._description)
1140-
actual: \(actual._description)
1141-
expected: \(expected._description)
1138+
index: \(index)
1139+
actual: \(actual)
1140+
expected: \(expected)
11421141
""")
11431142
}
11441143
}
@@ -1154,9 +1153,9 @@ suite.test("String index rounding/Scalars")
11541153
let actual = string.unicodeScalars._index(roundingDown: index)
11551154
expectEqual(actual, expected,
11561155
"""
1157-
index: \(index._description)
1158-
actual: \(actual._description)
1159-
expected: \(expected._description)
1156+
index: \(index)
1157+
actual: \(actual)
1158+
expected: \(expected)
11601159
""")
11611160
}
11621161
}
@@ -1180,9 +1179,9 @@ suite.test("String index rounding/UTF-16")
11801179
let actual = string.utf16._index(roundingDown: index)
11811180
expectEqual(actual, expected,
11821181
"""
1183-
index: \(index._description)
1184-
actual: \(actual._description)
1185-
expected: \(expected._description)
1182+
index: \(index)
1183+
actual: \(actual)
1184+
expected: \(expected)
11861185
""")
11871186
}
11881187
}
@@ -1205,9 +1204,81 @@ suite.test("String index rounding/UTF-8")
12051204
let actual = string.utf8._index(roundingDown: index)
12061205
expectEqual(actual, expected,
12071206
"""
1208-
index: \(index._description)
1209-
actual: \(actual._description)
1210-
expected: \(expected._description)
1207+
index: \(index)
1208+
actual: \(actual)
1209+
expected: \(expected)
12111210
""")
12121211
}
12131212
}
1213+
1214+
if #available(SwiftStdlib 6.1, *) {
1215+
suite.test("String index printing (native)") {
1216+
let str = "nai\u{308}ve 🪻"
1217+
1218+
let utf8Indices = [
1219+
"0[any]", "1[utf8]", "2[utf8]", "3[utf8]", "4[utf8]", "5[utf8]",
1220+
"6[utf8]", "7[utf8]", "8[utf8]", "9[utf8]", "10[utf8]", "11[utf8]"
1221+
]
1222+
expectEqual(str.utf8.indices.map { "\($0)" }, utf8Indices)
1223+
1224+
let utf16Indices = [
1225+
"0[any]", "1[utf8]", "2[utf8]", "3[utf8]", "5[utf8]", "6[utf8]",
1226+
"7[utf8]", "8[utf8]", "8[utf8]+1"
1227+
]
1228+
expectEqual(str.utf16.indices.map { "\($0)" }, utf16Indices)
1229+
1230+
let scalarIndices = [
1231+
"0[any]", "1[utf8]", "2[utf8]", "3[utf8]", "5[utf8]", "6[utf8]",
1232+
"7[utf8]", "8[utf8]"
1233+
]
1234+
expectEqual(str.unicodeScalars.indices.map { "\($0)" }, scalarIndices)
1235+
1236+
let characterIndices = [
1237+
"0[any]", "1[utf8]", "2[utf8]", "5[utf8]", "6[utf8]", "7[utf8]", "8[utf8]"
1238+
]
1239+
expectEqual(str.indices.map { "\($0)" }, characterIndices)
1240+
}
1241+
}
1242+
1243+
suite.test("String index debugDescription backdeployment") {
1244+
// Note: no availability check
1245+
let str = "i\u{308}"
1246+
expectEqual(str.startIndex.debugDescription, "0[any]")
1247+
expectEqual(str.endIndex.debugDescription, "3[utf8]")
1248+
}
1249+
1250+
1251+
#if _runtime(_ObjC)
1252+
if #available(SwiftStdlib 6.1, *) {
1253+
suite.test("String index printing (bridged Cocoa)") {
1254+
let utf16 = Array("nai\u{308}ve 🪻".utf16)
1255+
let nsstr = NSString(characters: utf16, length: utf16.count)
1256+
let str = nsstr as String
1257+
1258+
let utf8Indices = [
1259+
"0[any]", "1[utf16]", "2[utf16]", "3[utf16]", "3[utf16]+1", "4[utf16]",
1260+
"5[utf16]", "6[utf16]", "7[utf16]", "7[utf16]+1", "7[utf16]+2",
1261+
"7[utf16]+3"
1262+
]
1263+
expectEqual(str.utf8.indices.map { "\($0)" }, utf8Indices)
1264+
1265+
let utf16Indices = [
1266+
"0[any]", "1[utf16]", "2[utf16]", "3[utf16]", "4[utf16]", "5[utf16]",
1267+
"6[utf16]", "7[utf16]", "8[utf16]"
1268+
]
1269+
expectEqual(str.utf16.indices.map { "\($0)" }, utf16Indices)
1270+
1271+
let scalarIndices = [
1272+
"0[any]", "1[utf16]", "2[utf16]", "3[utf16]", "4[utf16]", "5[utf16]",
1273+
"6[utf16]", "7[utf16]"
1274+
]
1275+
expectEqual(str.unicodeScalars.indices.map { "\($0)" }, scalarIndices)
1276+
1277+
let characterIndices = [
1278+
"0[any]", "1[utf16]", "2[utf16]", "4[utf16]", "5[utf16]", "6[utf16]",
1279+
"7[utf16]"
1280+
]
1281+
expectEqual(str.indices.map { "\($0)" }, characterIndices)
1282+
}
1283+
}
1284+
#endif

0 commit comments

Comments
 (0)