I have the following code to parse PPM file.
(Specification can be found here https://netpbm.sourceforge.net/doc/ppm.html)
My question is how can I can get preferably get rid of the first for loop without increasing complexity at lot?
And without using alloc or the std lib.
#![no_std]
use core::slice;
#[derive(Debug)]
pub struct PPM<'a>{
pub magic: [u8;2],
pub width: u16,
pub heigth: u16,
pub max_col: u16,
pub data: &'a[u8]
}
impl<'a> PPM<'a> {
pub fn new(img: *const u8,size: usize) -> Option<Self> {
let img_sl = unsafe { slice::from_raw_parts(img, size) };
let mut data_start = 0;
let mut fields_seen = 0;
for (i,x) in img_sl.iter().enumerate() {
if x.is_ascii_whitespace() {
fields_seen += 1;
if fields_seen == 4 {
data_start = i;
break;
}
} else if i > 20 { return None }
}
let hdr = unsafe { core::str::from_utf8_unchecked(&img_sl[0..data_start]) };
let mut hdr_it = hdr.split_whitespace();
let magic = hdr_it.next().unwrap();
if magic != "P6" { return None }
let ppm = PPM {
magic: *b"P6",
width: u16::from_str_radix(hdr_it.next().unwrap(), 10).ok()?,
heigth: u16::from_str_radix(hdr_it.next().unwrap(), 10).ok()?,
max_col: u16::from_str_radix(hdr_it.next().unwrap(), 10).ok()?,
data: &img_sl[hdr_it.count()..]
};
Some(ppm)
}
}```

