#[derive(Copy, Clone, Debug)]
pub struct Path<'a>(&'a [u8]);
impl<'a> Path<'a> {
pub fn new(bytes: &'a [u8]) -> Self {
Path(bytes)
}
pub fn as_bytes(&self) -> &'a [u8] {
self.0
}
pub(crate) fn components(&self) -> impl Iterator<Item = &'a [u8]> {
self.0
.split(|&b| b == b'/')
.filter(|c| !c.is_empty() && *c != b".")
}
}
#[cfg(test)]
mod tests {
extern crate alloc;
use alloc::vec::Vec;
use super::*;
#[test]
fn splits_and_filters() {
let p = Path::new(b"/boot//kernel/./vmlinuz");
let comps: Vec<&[u8]> = p.components().collect();
assert_eq!(comps, [&b"boot"[..], &b"kernel"[..], &b"vmlinuz"[..]]);
}
#[test]
fn root_has_no_components() {
assert_eq!(Path::new(b"/").components().count(), 0);
assert_eq!(Path::new(b"").components().count(), 0);
}
}