//===----------------------------------------------------------------------===// // // This source file is part of the Swift Collections open source project // // Copyright (c) 2021 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // //===----------------------------------------------------------------------===// // MARK: CustomDebugStringConvertible extension _Node.UnsafeHandle: CustomDebugStringConvertible { #if DEBUG private enum PrintPosition { case start, end, middle } private func indentDescription(_ node: _Node.UnsafeHandle, position: PrintPosition) -> String { let label = "(\(node.elementCount)/\(node.subtreeCount))" let spaces = String(repeating: " ", count: label.count) let lines = describeNode(node).split(separator: "\n") return lines.enumerated().map({ index, line in var lineToInsert = line let middle = (lines.count - 1) / 2 if index < middle { if position == .start { return " " + spaces + lineToInsert } else { return "┃ " + spaces + lineToInsert } } else if index > middle { if position == .end { return " " + spaces + lineToInsert } else { return "┃ " + spaces + lineToInsert } } else { switch line[line.startIndex] { case "╺": lineToInsert.replaceSubrange(...line.startIndex, with: "━") case "┗": lineToInsert.replaceSubrange(...line.startIndex, with: "┻") case "┏": lineToInsert.replaceSubrange(...line.startIndex, with: "┳") case "┣": lineToInsert.replaceSubrange(...line.startIndex, with: "╋") default: break } switch position { case .start: return "┏━\(label)━" + lineToInsert case .middle: return "┣━\(label)━" + lineToInsert case .end: return "┗━\(label)━" + lineToInsert } } }).joined(separator: "\n") } /// A textual representation of this instance, suitable for debugging. private func describeNode(_ node: _Node.UnsafeHandle) -> String { if node.elementCount == 0 { var result = "" if !node.isLeaf { node[childAt: 0].read { handle in result += indentDescription(handle, position: .start) + "\n" } result += "┗━ << EMPTY >>" } else { result = "╺━ << EMPTY >>" } return result } var result = "" for slot in 0..