use alloc::vec::Vec;
use crate::compressor::core::codecs::FnEncoder;
use crate::compressor::core::data::Stream;
use crate::compressor::core::error::NodeError;
use crate::compressor::core::exec::NodeCx;
pub(crate) static PREFIX: FnEncoder = FnEncoder(prefix);
fn prefix<'a>(cx: &mut NodeCx<'_, 'a>, inputs: &[Stream<'a>]) -> Result<(), NodeError> {
let input = &inputs[0];
let mut suffixes = Vec::with_capacity(input.content_size());
let mut suffix_lens = Vec::with_capacity(input.n_elts());
let mut match_bytes = Vec::with_capacity(input.n_elts() * 4);
let mut previous: &[u8] = &[];
let mut rest = input.bytes();
for &len in input.lens() {
let (current, tail) = rest.split_at(len as usize);
let matched = previous
.iter()
.zip(current)
.take_while(|(a, b)| a == b)
.count();
suffixes.extend_from_slice(¤t[matched..]);
suffix_lens.push((current.len() - matched) as u32);
match_bytes.extend_from_slice(&(matched as u32).to_le_bytes());
previous = current;
rest = tail;
}
cx.emit_strings(0, suffixes, suffix_lens)?;
cx.emit(1, 4, match_bytes)?;
Ok(())
}