Skip to content

Commit

Permalink
refactor: minor changes for fixing build in 32-bits targets.
Browse files Browse the repository at this point in the history
Don't use bit arrays with `u64` as the underlying type, as this is not supported in 32-bits platforms, use `usize` instead.
  • Loading branch information
plusvic committed Feb 21, 2025
1 parent b4b7247 commit f5509f7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/src/compiler/atoms/quality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
best_quality: i32,
best_range: Option<Range<usize>>,
queue: VecDeque<(usize, u8, u8, i32)>,
bytes_present: BitArray<[u64; 4]>,
bytes_present: BitArray<[usize; 256 / usize::BITS as usize]>,
byte_mask_iter: I,
}

Expand Down Expand Up @@ -483,7 +483,7 @@ mod test {
[0x01, 0x02, 0x03, 0x04].iter(),
[0xff, 0xff, 0x00, 0xff].iter(),
);

assert!(q_00000001 > q_00000000);
assert!(q_00000001 > q_000001);
assert!(q_000001 > q_0001);
Expand Down
6 changes: 5 additions & 1 deletion lib/src/re/thompson/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ impl Display for SplitId {
}

impl SplitId {
/// Number of bits in an SplitId.
pub const BITS: usize = 13;

/// Maximum SplitId.
pub const MAX: usize = (1 << SplitId::BITS) - 1;

#[inline]
pub fn to_le_bytes(self) -> [u8; size_of::<Self>()] {
self.0.to_le_bytes()
Expand All @@ -103,7 +107,7 @@ impl SplitId {
#[inline]
pub fn add(self, amount: u16) -> Option<Self> {
let sum = self.0.checked_add(amount)?;
if sum >= 1 << Self::BITS {
if sum as usize > Self::MAX {
return None;
}
Some(Self(sum))
Expand Down
3 changes: 2 additions & 1 deletion lib/src/re/thompson/pikevm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ pub struct EpsilonClosureState {
/// This bit array has one bit per possible value of SplitId. If the
/// split instruction with SplitId = N is executed, the N-th bit in the
/// array is set to 1.
executed_splits: BitArray<[u64; (1 << SplitId::BITS) / 64]>,
executed_splits:
BitArray<[usize; SplitId::MAX.div_ceil(usize::BITS as usize)]>,
/// Indicates whether the `executed_splits` bit array needs to be
/// cleared during the next call to [`EpsilonClosureState::executed`].
dirty: bool,
Expand Down
11 changes: 11 additions & 0 deletions lib/src/re/thompson/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use pretty_assertions::assert_eq;
use crate::compiler::Atom;
use crate::re;
use crate::re::bitmapset::BitmapSet;
use crate::re::thompson::instr::SplitId;
use crate::re::{BckCodeLoc, FwdCodeLoc};
use crate::types::Regexp;

Expand Down Expand Up @@ -1455,3 +1456,13 @@ fn re_atoms() {
50
);
}

#[test]
fn split_id() {
assert_eq!(SplitId::BITS, 13);
assert_eq!(SplitId::MAX, 8191);
assert!(SplitId::default()
.add(SplitId::MAX as u16)
.and_then(|id| id.add(1))
.is_none())
}

0 comments on commit f5509f7

Please sign in to comment.