use core::{slice, str};
use crate::io_macros::*;
#[derive(Clone, Copy)]
pub struct EnvironmentIter(*mut *mut u8);
impl EnvironmentIter {
pub fn new(environment_pointer: *mut *mut u8) -> Self {
Self(environment_pointer)
}
pub fn into_inner(self) -> *mut *mut u8 {
self.0
}
}
impl Iterator for EnvironmentIter {
type Item = (&'static str, &'static str);
fn next(&mut self) -> Option<Self::Item> {
unsafe {
if self.0.is_null() {
return None;
}
let string_pointer = *self.0;
if string_pointer.is_null() {
return None;
}
let mut split_at = None;
let end_index = (0..).find(|&index| {
let character_byte = *string_pointer.add(index);
if split_at.is_none() && character_byte == b'=' {
split_at = Some(index);
}
character_byte == 0
})?;
syscall_debug_assert!(split_at.is_some());
let split_at = split_at.unwrap();
let name_slice = slice::from_raw_parts(string_pointer, split_at);
let value_slice =
slice::from_raw_parts(string_pointer.add(split_at + 1), end_index - split_at - 1);
let name = str::from_utf8_unchecked(name_slice);
let value = str::from_utf8_unchecked(value_slice);
self.0 = self.0.add(1);
Some((name, value))
}
}
}