use crate::entropy::calculate_entropy;
const MIN_STRING_DENSITY: f64 = 0.70;
const MAX_ENTROPY_FOR_VECTORIZED: f64 = 6.0;
const SAMPLE_SIZE: usize = 1_048_576;
#[must_use]
pub fn should_use_vectorized(data: &[u8]) -> bool {
if data.is_empty() {
return false;
}
let sample = if data.len() > SAMPLE_SIZE {
&data[..SAMPLE_SIZE]
} else {
data
};
let density = string_density(sample);
if density < MIN_STRING_DENSITY {
return false;
}
let entropy = calculate_entropy(sample);
entropy < MAX_ENTROPY_FOR_VECTORIZED
}
#[allow(clippy::cast_precision_loss)]
fn string_density(data: &[u8]) -> f64 {
if data.is_empty() {
return 0.0;
}
let printable = data
.iter()
.filter(|&&b| b == b'\t' || b == b'\n' || b == b'\r' || (0x20..=0x7E).contains(&b))
.count();
printable as f64 / data.len() as f64
}