@@ -58,14 +58,15 @@ public func customDump<T, TargetStream>(
5858) -> T where TargetStream: TextOutputStream {
5959
6060 var idPerItem : [ ObjectIdentifier : UInt ] = [ : ]
61- var occurencePerType : [ String : UInt ] = [ : ]
61+ var occurrencePerType : [ String : UInt ] = [ : ]
6262 var visitedItems : Set < ObjectIdentifier > = [ ]
6363
6464 func customDumpHelp< T, TargetStream> (
6565 _ value: T ,
6666 to target: inout TargetStream ,
6767 name: String ? ,
6868 indent: Int ,
69+ isRoot: Bool ,
6970 maxDepth: Int
7071 ) where TargetStream: TextOutputStream {
7172 if T . self is AnyObject . Type , withUnsafeBytes ( of: value, { $0. allSatisfy { $0 == 0 } } ) {
@@ -89,7 +90,12 @@ public func customDump<T, TargetStream>(
8990 var childOut = " "
9091 let child = mirror. children. first!
9192 customDumpHelp (
92- child. value, to: & childOut, name: child. label, indent: 0 , maxDepth: maxDepth - 1
93+ child. value,
94+ to: & childOut,
95+ name: child. label,
96+ indent: 0 ,
97+ isRoot: false ,
98+ maxDepth: maxDepth - 1
9399 )
94100 if childOut. contains ( " \n " ) {
95101 if maxDepth == 0 {
@@ -113,7 +119,13 @@ public func customDump<T, TargetStream>(
113119 for (offset, var child) in children. enumerated ( ) {
114120 transform ( & child, offset)
115121 customDumpHelp (
116- child. value, to: & out, name: child. label, indent: 2 , maxDepth: maxDepth - 1 )
122+ child. value,
123+ to: & out,
124+ name: child. label,
125+ indent: 2 ,
126+ isRoot: false ,
127+ maxDepth: maxDepth - 1
128+ )
117129 if offset != children. count - 1 {
118130 out. write ( " , " )
119131 }
@@ -132,16 +144,18 @@ public func customDump<T, TargetStream>(
132144 out. write ( value. customDumpDescription)
133145
134146 case let ( value as CustomDumpRepresentable , _) :
135- customDumpHelp ( value. customDumpValue, to: & out, name: nil , indent: 0 , maxDepth: maxDepth - 1 )
147+ customDumpHelp (
148+ value. customDumpValue, to: & out, name: nil , indent: 0 , isRoot: false , maxDepth: maxDepth - 1
149+ )
136150
137151 case let ( value as AnyObject , . class? ) :
138152 let item = ObjectIdentifier ( value)
139- var occurence = occurencePerType [ typeName ( mirror. subjectType) , default: 0 ] {
140- didSet { occurencePerType [ typeName ( mirror. subjectType) ] = occurence }
153+ var occurrence = occurrencePerType [ typeName ( mirror. subjectType) , default: 0 ] {
154+ didSet { occurrencePerType [ typeName ( mirror. subjectType) ] = occurrence }
141155 }
142156
143157 var id : String {
144- let id = idPerItem [ item, default: occurence ]
158+ let id = idPerItem [ item, default: occurrence ]
145159 idPerItem [ item] = id
146160
147161 return id > 1 ? " # \( id) " : " "
@@ -150,7 +164,7 @@ public func customDump<T, TargetStream>(
150164 out. write ( " \( typeName ( mirror. subjectType) ) \( id) (↩︎) " )
151165 } else {
152166 visitedItems. insert ( item)
153- occurence += 1
167+ occurrence += 1
154168 var children = Array ( mirror. children)
155169
156170 var superclassMirror = mirror. superclassMirror
@@ -192,7 +206,7 @@ public func customDump<T, TargetStream>(
192206 }
193207
194208 case ( _, . enum? ) :
195- out. write ( " \( typeName ( mirror. subjectType) ) . " )
209+ out. write ( isRoot ? " \( typeName ( mirror. subjectType) ) . " : " . " )
196210 if let child = mirror. children. first {
197211 let childMirror = Mirror ( customDumpReflecting: child. value)
198212 let associatedValuesMirror =
@@ -215,7 +229,7 @@ public func customDump<T, TargetStream>(
215229
216230 case ( _, . optional? ) :
217231 if let value = mirror. children. first? . value {
218- customDumpHelp ( value, to: & out, name: nil , indent: 0 , maxDepth: maxDepth)
232+ customDumpHelp ( value, to: & out, name: nil , indent: 0 , isRoot : false , maxDepth: maxDepth)
219233 } else {
220234 out. write ( " nil " )
221235 }
@@ -266,7 +280,7 @@ public func customDump<T, TargetStream>(
266280 target. write ( ( name. map { " \( $0) : " } ?? " " ) . appending ( out) . indenting ( by: indent) )
267281 }
268282
269- customDumpHelp ( value, to: & target, name: name, indent: indent, maxDepth: maxDepth)
283+ customDumpHelp ( value, to: & target, name: name, indent: indent, isRoot : true , maxDepth: maxDepth)
270284 return value
271285}
272286
0 commit comments