use alloc::vec;
use alloc::vec::Vec;
pub unsafe fn strnlen(ptr: *const core::ffi::c_char, len: usize) -> usize {
const NULL_TERMINATION: core::ffi::c_char = 0;
for i in 0..len {
if unsafe { *ptr.add(i) } == NULL_TERMINATION {
return i;
}
}
len
}
pub fn as_escaped_string(bytes: &[u8]) -> alloc::string::String {
unsafe {
alloc::string::String::from_utf8_unchecked(
bytes
.iter()
.flat_map(|c| match *c {
b'\t' => vec![b'\\', b't'].into_iter(),
b'\r' => vec![b'\\', b'r'].into_iter(),
b'\n' => vec![b'\\', b'n'].into_iter(),
b'\x20'..=b'\x7f' => vec![*c].into_iter(),
_ => {
let hex_digits: &[u8; 16] = b"0123456789abcdef";
vec![
b'\\',
b'x',
hex_digits[(c >> 4) as usize],
hex_digits[(c & 0xf) as usize],
]
.into_iter()
}
})
.collect::<Vec<u8>>(),
)
}
}