diff --git a/Sources/BitCollections/BitArray/BitArray+Descriptions.swift b/Sources/BitCollections/BitArray/BitArray+Descriptions.swift index ebc6cdff..d285b4eb 100644 --- a/Sources/BitCollections/BitArray/BitArray+Descriptions.swift +++ b/Sources/BitCollections/BitArray/BitArray+Descriptions.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Collections open source project // -// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors +// Copyright (c) 2022 - 2024 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 @@ -15,63 +15,70 @@ extension UInt8 { @inline(__always) internal static var _ascii1: Self { 49 } + + @inline(__always) + internal static var _asciiLT: Self { 49 } + + @inline(__always) + internal static var _asciiGT: Self { 49 } } extension BitArray: CustomStringConvertible { /// A textual representation of this instance. - /// Bit arrays print themselves as a string of binary bits, with the highest-indexed elements - /// appearing first, as in the binary representation of integers: + /// Bit arrays print themselves as a string of binary bits, with the + /// highest-indexed elements appearing first, as in the binary representation + /// of integers. The digits are surrounded by angle brackets, so that the + /// notation is non-ambigous even for empty bit arrays: /// /// let bits: BitArray = [false, false, false, true, true] - /// print(bits) // "11000" + /// print(bits) // "<11000>" /// - /// The description of an empty bit array is an empty string. + /// let empty = BitArray() + /// print(empty) // "<>" public var description: String { _bitString } } extension BitArray: CustomDebugStringConvertible { - /// A textual representation of this instance, suitable for debugging. - /// - /// Bit arrays print themselves as a string of binary bits, with the highest-indexed elements - /// appearing first, as in the binary representation of integers: + /// A textual representation of this instance. + /// Bit arrays print themselves as a string of binary bits, with the + /// highest-indexed elements appearing first, as in the binary representation + /// of integers: /// /// let bits: BitArray = [false, false, false, true, true] - /// print(bits) // "11000" - /// - /// As a special case, the debug description of an empty bit array is an empty pair of brackets. - /// This makes this case read better in structured printouts, like when the `BitArray` is printed - /// as part of another collection value: - /// - /// let list: [BitArray] = [[false, true], [], [false]] - /// print(list) // "[10, [], 0]" + /// print(bits) // "<11000>" /// + /// The digits are surrounded by angle brackets to serve as visual delimiters. + /// (So that empty bit arrays still have a non-empty description.) public var debugDescription: String { - guard !isEmpty else { return "[]" } - return description + description } } extension BitArray { internal var _bitString: String { - guard !isEmpty else { return "" } + guard !isEmpty else { return "<>" } var result: String if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) { - result = String(unsafeUninitializedCapacity: self.count) { target in - var i = count - 1 + result = String(unsafeUninitializedCapacity: self.count + 2) { target in + target.initializeElement(at: count - 1, to: ._asciiGT) + var i = count - 2 for v in self { target.initializeElement(at: i, to: v ? ._ascii1 : ._ascii0) i &-= 1 } + assert(i == 0) + target.initializeElement(at: 0, to: ._asciiLT) return count } } else { - result = "" - result.reserveCapacity(self.count) + result = "<" + result.reserveCapacity(self.count + 2) for v in self.reversed() { result.append(v ? "1" : "0") } + result.append(">") } return result }