Skip to content

Commit 7dabd3e

Browse files
authoredOct 5, 2017
Merge pull request #12249 from lorentey/rdar/34672149
[stdlib] Dictionary.Keys, .Values: Implement Custom[Debug]StringConvertible
2 parents c02ffaf + 4ff5a41 commit 7dabd3e

File tree

2 files changed

+60
-48
lines changed

2 files changed

+60
-48
lines changed
 

‎stdlib/public/core/Arrays.swift.gyb

+33-27
Original file line numberDiff line numberDiff line change
@@ -1613,40 +1613,20 @@ extension ${Self} : CustomReflectable {
16131613
}
16141614

16151615
extension ${Self} : CustomStringConvertible, CustomDebugStringConvertible {
1616-
internal func _makeDescription(isDebug: Bool) -> String {
1617-
% if Self != 'Array':
1618-
var result = isDebug ? "${Self}([" : "["
1619-
% else:
1620-
// Always show sugared representation for Arrays.
1621-
var result = "["
1622-
% end
1623-
var first = true
1624-
for item in self {
1625-
if first {
1626-
first = false
1627-
} else {
1628-
result += ", "
1629-
}
1630-
debugPrint(item, terminator: "", to: &result)
1631-
}
1632-
% if Self != 'Array':
1633-
result += isDebug ? "])" : "]"
1634-
% else:
1635-
// Always show sugared representation for Arrays.
1636-
result += "]"
1637-
% end
1638-
return result
1639-
}
1640-
16411616
/// A textual representation of the array and its elements.
16421617
public var description: String {
1643-
return _makeDescription(isDebug: false)
1618+
return _makeCollectionDescription(for: self, withTypeName: nil)
16441619
}
16451620

16461621
/// A textual representation of the array and its elements, suitable for
16471622
/// debugging.
16481623
public var debugDescription: String {
1649-
return _makeDescription(isDebug: true)
1624+
% if Self != 'Array':
1625+
return _makeCollectionDescription(for: self, withTypeName: "${Self}")
1626+
% else:
1627+
// Always show sugared representation for Arrays.
1628+
return _makeCollectionDescription(for: self, withTypeName: nil)
1629+
% end
16501630
}
16511631
}
16521632

@@ -1812,6 +1792,32 @@ extension ${Self} {
18121792
}
18131793
%end
18141794

1795+
// Utility method for collections that wish to implement CustomStringConvertible
1796+
// and CustomDebugStringConvertible using a bracketed list of elements,
1797+
// like an array.
1798+
@_inlineable // FIXME(sil-serialize-all)
1799+
@_versioned // FIXME(sil-serialize-all)
1800+
internal func _makeCollectionDescription<C : Collection>
1801+
(for items: C, withTypeName type: String?) -> String {
1802+
var result = ""
1803+
if let type = type {
1804+
result += "\(type)(["
1805+
} else {
1806+
result += "["
1807+
}
1808+
var first = true
1809+
for item in items {
1810+
if first {
1811+
first = false
1812+
} else {
1813+
result += ", "
1814+
}
1815+
debugPrint(item, terminator: "", to: &result)
1816+
}
1817+
result += type != nil ? "])" : "]"
1818+
return result
1819+
}
1820+
18151821
@_versioned
18161822
@_fixed_layout
18171823
internal struct _InitializeMemoryFromCollection<

‎stdlib/public/core/HashedCollections.swift.gyb

+27-21
Original file line numberDiff line numberDiff line change
@@ -1322,33 +1322,16 @@ extension Set {
13221322
}
13231323

13241324
extension Set : CustomStringConvertible, CustomDebugStringConvertible {
1325-
@_inlineable // FIXME(sil-serialize-all)
1326-
@_versioned // FIXME(sil-serialize-all)
1327-
internal func makeDescription(isDebug: Bool) -> String {
1328-
var result = isDebug ? "Set([" : "["
1329-
var first = true
1330-
for member in self {
1331-
if first {
1332-
first = false
1333-
} else {
1334-
result += ", "
1335-
}
1336-
debugPrint(member, terminator: "", to: &result)
1337-
}
1338-
result += isDebug ? "])" : "]"
1339-
return result
1340-
}
1341-
13421325
/// A string that represents the contents of the set.
13431326
@_inlineable // FIXME(sil-serialize-all)
13441327
public var description: String {
1345-
return makeDescription(isDebug: false)
1328+
return _makeCollectionDescription(for: self, withTypeName: nil)
13461329
}
13471330

13481331
/// A string that represents the contents of the set, suitable for debugging.
13491332
@_inlineable // FIXME(sil-serialize-all)
13501333
public var debugDescription: String {
1351-
return makeDescription(isDebug: true)
1334+
return _makeCollectionDescription(for: self, withTypeName: "Set")
13521335
}
13531336
}
13541337

@@ -2554,7 +2537,9 @@ extension Dictionary {
25542537

25552538
/// A view of a dictionary's keys.
25562539
@_fixed_layout // FIXME(sil-serialize-all)
2557-
public struct Keys : Collection, Equatable {
2540+
public struct Keys
2541+
: Collection, Equatable,
2542+
CustomStringConvertible, CustomDebugStringConvertible {
25582543
public typealias Element = Key
25592544

25602545
@_versioned // FIXME(sil-serialize-all)
@@ -2638,11 +2623,22 @@ extension Dictionary {
26382623

26392624
return true
26402625
}
2626+
2627+
@_inlineable // FIXME(sil-serialize-all)
2628+
public var description: String {
2629+
return _makeCollectionDescription(for: self, withTypeName: nil)
2630+
}
2631+
2632+
@_inlineable // FIXME(sil-serialize-all)
2633+
public var debugDescription: String {
2634+
return _makeCollectionDescription(for: self, withTypeName: "Dictionary.Keys")
2635+
}
26412636
}
26422637

26432638
/// A view of a dictionary's values.
26442639
@_fixed_layout // FIXME(sil-serialize-all)
2645-
public struct Values : MutableCollection {
2640+
public struct Values
2641+
: MutableCollection, CustomStringConvertible, CustomDebugStringConvertible {
26462642
public typealias Element = Value
26472643

26482644
@_versioned // FIXME(sil-serialize-all)
@@ -2699,6 +2695,16 @@ extension Dictionary {
26992695
public var isEmpty: Bool {
27002696
return count == 0
27012697
}
2698+
2699+
@_inlineable // FIXME(sil-serialize-all)
2700+
public var description: String {
2701+
return _makeCollectionDescription(for: self, withTypeName: nil)
2702+
}
2703+
2704+
@_inlineable // FIXME(sil-serialize-all)
2705+
public var debugDescription: String {
2706+
return _makeCollectionDescription(for: self, withTypeName: "Dictionary.Values")
2707+
}
27022708
}
27032709
}
27042710

0 commit comments

Comments
 (0)
Please sign in to comment.