mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
[BitCollections] Don't trap in BitArray shifts when the amount is Int.min
`maskingShiftLeft(by:)`, `maskingShiftRight(by:)` and `resizingShiftLeft(by:)` handle a negative amount by negating it and shifting the other way, so `Int.min` trapped with an arithmetic overflow. All three have a defined result for it: an amount of at least `count` clears or removes every value, which is what `Int.min + 1` already did. Clamp the magnitude of a negative amount to `Int.max` in these three. `resizingShiftRight(by:)` is left alone: a negative amount inserts values there, and no array can hold as many as `Int.min` asks for. The masking shifts had no tests. Add tests for both of them over word-crossing counts and amounts on either side of `count`, for their doc examples, and for the extreme amounts. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c409d4e06
commit
09d8daf0b5
2 files changed
+102
-3
No files matched your search
@@ -11,6 +11,19 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitArray {
|
||||
/// Returns the magnitude of a negative shift amount.
|
||||
///
|
||||
/// `Int.min` has no positive counterpart, so it is clamped to `Int.max`.
|
||||
/// This only suits shifts that discard values: those treat every amount
|
||||
/// that is at least `count` the same way.
|
||||
@inline(__always)
|
||||
internal static func _magnitude(ofNegativeShift amount: Int) -> Int {
|
||||
assert(amount < 0)
|
||||
return Int(clamping: amount.magnitude)
|
||||
}
|
||||
}
|
||||
|
||||
extension BitArray {
|
||||
/// Shift the bits in this array by the specified number of places to the
|
||||
/// left (towards the end of the array), by inserting `amount` false values
|
||||
@@ -31,7 +44,7 @@ extension BitArray {
|
||||
if amount > 0 {
|
||||
_resizingShiftLeft(by: amount)
|
||||
} else {
|
||||
_resizingShiftRight(by: -amount)
|
||||
_resizingShiftRight(by: Self._magnitude(ofNegativeShift: amount))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +71,8 @@ extension BitArray {
|
||||
if amount > 0 {
|
||||
_resizingShiftRight(by: amount)
|
||||
} else {
|
||||
// This direction inserts values, so `Int.min` cannot be clamped: no
|
||||
// array can hold as many values as it asks for, and the negation traps.
|
||||
_resizingShiftLeft(by: -amount)
|
||||
}
|
||||
}
|
||||
@@ -106,7 +121,8 @@ extension BitArray {
|
||||
if amount > 0 {
|
||||
unsafe $0._maskingShiftLeft(by: amount)
|
||||
} else {
|
||||
unsafe $0._maskingShiftRight(by: -amount)
|
||||
unsafe $0._maskingShiftRight(
|
||||
by: BitArray._magnitude(ofNegativeShift: amount))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,7 +151,8 @@ extension BitArray {
|
||||
if amount > 0 {
|
||||
unsafe $0._maskingShiftRight(by: amount)
|
||||
} else {
|
||||
unsafe $0._maskingShiftLeft(by: -amount)
|
||||
unsafe $0._maskingShiftLeft(
|
||||
by: BitArray._magnitude(ofNegativeShift: amount))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user