pub(crate) fn scan_start_code(buf: &[u8], from: usize, floor: usize) -> Option<(usize, usize)> {
let mut search = from;
while let Some(rel) = memchr::memchr(0x01, buf.get(search..)?) {
let one = search + rel;
if one >= floor + 2 && buf[one - 1] == 0 && buf[one - 2] == 0 {
if one >= floor + 3 && buf[one - 3] == 0 {
return Some((one - 3, 4));
}
return Some((one - 2, 3));
}
search = one + 1;
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn floor_from_requires_code_to_begin_at_or_after_origin() {
let buf = [0, 0, 1, 9];
assert_eq!(scan_start_code(&buf, 1, 1), None);
assert_eq!(scan_start_code(&buf, 1, 0), Some((0, 3)));
}
#[test]
fn classifies_three_and_four_byte_codes() {
let buf = [0, 0, 0, 1, 9, 0, 0, 1, 7];
assert_eq!(scan_start_code(&buf, 0, 0), Some((0, 4)));
assert_eq!(scan_start_code(&buf, 4, 4), Some((5, 3)));
}
#[test]
fn out_of_range_origin_is_none_not_panic() {
let buf = [0, 0, 1];
assert_eq!(scan_start_code(&buf, 99, 0), None);
}
}