use core::ffi::CStr;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Represents an iterator over a concatenated array of strings in the FDT.
///
/// Panics if any of the strings that are parsed are not valid UTF-8.
pub struct StrIter<'a> {
    slice: &'a [u8],
}

impl<'a> StrIter<'a> {
    pub const fn as_bytes(&self) -> &'a [u8] {
        self.slice
    }

    pub const fn new(slice: &'a [u8]) -> Self {
        Self { slice }
    }
}

impl<'a> Iterator for StrIter<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        let cstr = CStr::from_bytes_until_nul(self.slice).ok()?;
        let str = cstr.to_str().expect("Invalid FDT String");
        self.slice = &self.slice[str.len() + 1..];
        Some(str)
    }
}
