use alloc::vec::Vec;
pub trait ToNullTerminatedString {
fn to_null_terminated_utf8(&self) -> Vec<u8>;
}
impl ToNullTerminatedString for &str {
fn to_null_terminated_utf8(&self) -> Vec<u8> {
let bytes_without_nul_count = self.as_bytes().len();
let mut v = Vec::with_capacity(bytes_without_nul_count + 1);
unsafe {
core::ptr::copy_nonoverlapping(self.as_ptr(), v.as_mut_ptr(), bytes_without_nul_count);
*v.as_mut_ptr().add(bytes_without_nul_count) = 0;
v.set_len(bytes_without_nul_count + 1);
}
v
}
}
impl ToNullTerminatedString for alloc::string::String {
fn to_null_terminated_utf8(&self) -> Vec<u8> {
(&**self).to_null_terminated_utf8()
}
}
#[inline]
unsafe fn strlen(s: *const u8) -> usize {
let mut isize = 0;
while *s.offset(isize) != 0 {
isize += 1;
}
return isize as usize;
}
pub unsafe fn parse_null_terminated_utf8<'a>(
p: *const u8,
) -> Result<&'a str, core::str::Utf8Error> {
let slice = {
let bytes_without_nul_count = strlen(p);
core::slice::from_raw_parts::<'a>(p, bytes_without_nul_count)
};
core::str::from_utf8(slice)
}