[BitCollections] Fix BitArray.resizingShiftRight leaving stale bits

_resizingShiftRight(by:) copied the surviving suffix down to index 0 and
then removed `count &- amount` trailing bits instead of `amount`, so it
truncated the result to `amount` stale bits rather than keeping
`count - amount` valid ones. resizingShiftLeft(by: -n) was affected via
the same path, and the API had no test coverage at all.

Also fix the doc examples, which demonstrated the resizing shifts with
the masking shift function names (and, in one case, a result whose bit
count contradicted the documented growth by `amount`).

Add regression tests asserting parity with the documented semantics
(dropFirst / prepending false values) across word-boundary counts,
negative amounts, boundary `amount` values, and the doc examples
verbatim.
This commit is contained in:
Aashish committed 2026-10-22 15:27:56 +05:45
1 parent c141f557b0
commit 297ffa296b
2 files changed
+89 -8

No files matched your search

@@ -20,12 +20,12 @@ extension BitArray {
/// places to the right.
///
/// var bits: BitArray = "1110110"
/// bits.maskingShiftLeft(by: 2)
/// bits.resizingShiftLeft(by: 2)
/// // bits is now 111011000
/// bits.maskingShiftLeft(by: -4)
/// bits.resizingShiftLeft(by: -4)
/// // bits is now 11101
/// bits.maskingShiftLeft(by: 8)
/// // bits is now 111010000000
/// bits.resizingShiftLeft(by: 8)
/// // bits is now 1110100000000
public mutating func resizingShiftLeft(by amount: Int) {
guard amount != 0 else { return }
if amount > 0 {
@@ -44,11 +44,11 @@ extension BitArray {
/// then the resulting bit array will be empty.
///
/// var bits: BitArray = "1110110"
/// bits.maskingShiftRight(by: 2)
/// bits.resizingShiftRight(by: 2)
/// // bits is now 11101
/// bits.maskingShiftRight(by: -4)
/// bits.resizingShiftRight(by: -4)
/// // bits is now 111010000
/// bits.maskingShiftRight(by: 10)
/// bits.resizingShiftRight(by: 10)
/// // bits is now empty
///
/// If `amount` is between 0 and `count`, then this has the same effect as
@@ -75,7 +75,7 @@ extension BitArray {
return
}
self._copy(from: unsafe Range(uncheckedBounds: (amount, count)), to: 0)
self._removeLast(count &- amount)
self._removeLast(amount)
}
}