bit-string 0.4.2

A compact owned bit string type with editing, matching, and bitwise operations.
Documentation
use alloc::vec::Vec;

use crate::WORD_BITS;

/// Returns `u64::MAX` when `bits >= WORD_BITS`, otherwise the low `bits` ones.
#[inline]
pub(crate) fn low_mask(bits: usize) -> u64 {
    if bits >= WORD_BITS {
        u64::MAX
    } else {
        (1u64 << bits) - 1
    }
}

/// Returns the mask for the last word of a bit string of total length `len`.
///
/// The number of valid bits in the last word is `len % WORD_BITS`. When that
/// remainder is zero the last word is full and `u64::MAX` is returned;
/// otherwise only the low `len % WORD_BITS` bits are set.
#[inline]
pub(crate) fn last_word_mask(len: usize) -> u64 {
    let rem = len % WORD_BITS;
    if rem == 0 {
        u64::MAX
    } else {
        (1u64 << rem) - 1
    }
}

/// Returns the number of `u64` words needed to store `bit_len` bits.
#[inline]
pub(crate) fn word_len(bit_len: usize) -> usize {
    bit_len / WORD_BITS + usize::from(bit_len % WORD_BITS != 0)
}

/// Allocates a zero-initialized `Vec<u64>` of `words` capacity and length.
#[inline]
pub(crate) fn zero_words(words: usize) -> Vec<u64> {
    let mut bits = Vec::with_capacity(words);
    bits.resize(words, 0);
    bits
}

#[cfg(test)]
mod tests_for_low_mask;

#[cfg(test)]
mod tests_for_last_word_mask;

#[cfg(test)]
mod tests_for_word_len;

#[cfg(test)]
mod tests_for_zero_words;