#[cfg(test)]
mod tests;
pub struct FreqTable<'a>(pub &'a [u8]);
pub const ENGLISH_FREQ_TABLE: FreqTable<'static> = FreqTable(b"etaoinshrdlcumwfgypbvkjxqz");
pub fn most_frequent_char(text: &str) -> u8 {
let mut table = [0; 26];
let bytes = text.as_bytes();
for byte in bytes {
match *byte {
b'a'..=b'z' => table[(*byte - b'a') as usize] += 1,
b'A'..=b'Z' => table[(*byte - b'A') as usize] += 1,
_ => (),
}
}
let mut max = 0;
let mut max_index = 0;
for i in 0..table.len() {
if table[i] > max {
max = table[i];
max_index = i as u8;
}
}
max_index + b'a'
}
pub fn compute_shifts(char: u8, freq_table: &FreqTable) -> Vec<i8> {
freq_table
.0
.iter()
.map(|each| char as i8 - *each as i8)
.collect()
}