@@ -34,11 +34,12 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
3434 lhsName: String ? ,
3535 rhsName: String ? ,
3636 separator: String ,
37- indent: Int
37+ indent: Int ,
38+ isRoot: Bool
3839 ) -> String {
3940 let rhsName = rhsName ?? lhsName
4041 guard !isMirrorEqual( lhs, rhs) else {
41- return _customDump ( lhs, name: rhsName, indent: indent, maxDepth: 0 )
42+ return _customDump ( lhs, name: rhsName, indent: indent, isRoot : isRoot , maxDepth: 0 )
4243 . appending ( separator)
4344 . indenting ( with: format. both + " " )
4445 }
@@ -48,8 +49,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
4849 var out = " "
4950
5051 func diffEverything( ) {
51- var lhs = _customDump ( lhs, name: lhsName, indent: indent, maxDepth: . max)
52- var rhs = _customDump ( rhs, name: rhsName, indent: indent, maxDepth: . max)
52+ var lhs = _customDump ( lhs, name: lhsName, indent: indent, isRoot : isRoot , maxDepth: . max)
53+ var rhs = _customDump ( rhs, name: rhsName, indent: indent, isRoot : isRoot , maxDepth: . max)
5354 if lhs == rhs {
5455 if lhsMirror. subjectType != rhsMirror. subjectType {
5556 lhs. append ( " as \( typeName ( lhsMirror. subjectType) ) " )
@@ -104,6 +105,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
104105 lhs,
105106 name: lhsName,
106107 indent: indent,
108+ isRoot: false ,
107109 maxDepth: 0
108110 )
109111 . indenting ( with: format. first + " " ) ,
@@ -114,6 +116,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
114116 rhs,
115117 name: rhsName,
116118 indent: indent,
119+ isRoot: false ,
117120 maxDepth: 0
118121 )
119122 . indenting ( with: format. second + " " ) ,
@@ -130,6 +133,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
130133 lhs,
131134 name: lhsName,
132135 indent: indent,
136+ isRoot: isRoot,
133137 maxDepth: . max
134138 )
135139 . indenting ( with: format. first + " " ) ,
@@ -140,6 +144,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
140144 rhs,
141145 name: rhsName,
142146 indent: indent,
147+ isRoot: isRoot,
143148 maxDepth: . max
144149 )
145150 . indenting ( with: format. second + " " ) ,
@@ -178,6 +183,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
178183 child. value,
179184 name: child. label,
180185 indent: indent + elementIndent,
186+ isRoot: false ,
181187 maxDepth: 0
182188 )
183189 . indenting ( with: format. both + " " ) ,
@@ -234,7 +240,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
234240 separator: lhsOffset == lhsChildren. count - 1 && rhsOffset == rhsChildren. count - 1
235241 ? " "
236242 : elementSeparator,
237- indent: indent + elementIndent
243+ indent: indent + elementIndent,
244+ isRoot: false
238245 ) ,
239246 to: & out
240247 )
@@ -250,6 +257,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
250257 lhsChild. value,
251258 name: lhsChild. label,
252259 indent: indent + elementIndent,
260+ isRoot: false ,
253261 maxDepth: . max
254262 )
255263 . indenting ( with: format. first + " " ) ,
@@ -266,6 +274,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
266274 rhsChild. value,
267275 name: rhsChild. label,
268276 indent: indent + elementIndent,
277+ isRoot: false ,
269278 maxDepth: . max
270279 )
271280 . indenting ( with: format. second + " " ) ,
@@ -301,7 +310,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
301310 lhsName: lhsName,
302311 rhsName: rhsName,
303312 separator: separator,
304- indent: indent
313+ indent: indent,
314+ isRoot: isRoot
305315 )
306316 )
307317
@@ -385,16 +395,16 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
385395 let lhs = $0. value as? ( key: AnyHashable , value: Any ) ,
386396 let rhs = $1. value as? ( key: AnyHashable , value: Any )
387397 else {
388- return _customDump ( $0. value, name: nil , indent: 0 , maxDepth: 1 )
389- < _customDump( $1. value, name: nil , indent: 0 , maxDepth: 1 )
398+ return _customDump ( $0. value, name: nil , indent: 0 , isRoot : false , maxDepth: 1 )
399+ < _customDump( $1. value, name: nil , indent: 0 , isRoot : false , maxDepth: 1 )
390400 }
391- return _customDump ( lhs. key. base, name: nil , indent: 0 , maxDepth: 1 )
392- < _customDump( rhs. key. base, name: nil , indent: 0 , maxDepth: 1 )
401+ return _customDump ( lhs. key. base, name: nil , indent: 0 , isRoot : false , maxDepth: 1 )
402+ < _customDump( rhs. key. base, name: nil , indent: 0 , isRoot : false , maxDepth: 1 )
393403 }
394404 ) { child, _ in
395405 guard let pair = child. value as? ( key: AnyHashable , value: Any ) else { return }
396406 child = (
397- _customDump ( pair. key. base, name: nil , indent: 0 , maxDepth: 1 ) ,
407+ _customDump ( pair. key. base, name: nil , indent: 0 , isRoot : false , maxDepth: 1 ) ,
398408 pair. value
399409 )
400410 }
@@ -420,7 +430,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
420430 ? rhsChildMirror
421431 : Mirror ( rhs, unlabeledChildren: [ rhsChild. value] , displayStyle: . tuple)
422432
423- let subjectType = typeName ( lhsMirror. subjectType)
433+ let subjectType = isRoot ? typeName ( lhsMirror. subjectType) : " "
424434 diffChildren (
425435 lhsAssociatedValuesMirror,
426436 rhsAssociatedValuesMirror,
@@ -452,7 +462,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
452462 lhsName: lhsName,
453463 rhsName: rhsName,
454464 separator: separator,
455- indent: indent
465+ indent: indent,
466+ isRoot: isRoot
456467 )
457468 )
458469
@@ -469,8 +480,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
469480 isIdentityEqual ( $0. value, $1. value) || isMirrorEqual ( $0. value, $1. value)
470481 } ,
471482 areInIncreasingOrder: {
472- _customDump ( $0. value, name: nil , indent: 0 , maxDepth: 1 )
473- < _customDump( $1. value, name: nil , indent: 0 , maxDepth: 1 )
483+ _customDump ( $0. value, name: nil , indent: 0 , isRoot : false , maxDepth: 1 )
484+ < _customDump( $1. value, name: nil , indent: 0 , isRoot : false , maxDepth: 1 )
474485 }
475486 )
476487
@@ -522,7 +533,10 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
522533 . split ( separator: " \n " , omittingEmptySubsequences: false )
523534 . map ( Line . init ( rawValue: ) )
524535 )
525- let hashes = String ( repeating: " # " , count: max ( lhs. hashCount, rhs. hashCount) )
536+ let hashes = String (
537+ repeating: " # " ,
538+ count: max ( lhs. hashCount ( isMultiline: true ) , rhs. hashCount ( isMultiline: true ) )
539+ )
526540 diffChildren (
527541 lhsMirror,
528542 rhsMirror,
@@ -545,7 +559,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
545559
546560 guard !isMirrorEqual( lhs, rhs) else { return nil }
547561
548- var diff = diffHelp ( lhs, rhs, lhsName: nil , rhsName: nil , separator: " " , indent: 0 )
562+ var diff = diffHelp ( lhs, rhs, lhsName: nil , rhsName: nil , separator: " " , indent: 0 , isRoot : true )
549563 if diff. last == " \n " { diff. removeLast ( ) }
550564 return diff
551565}
0 commit comments