1use libc::c_char;
4
5pub(crate) fn cstr_to_rust(c: *const c_char) -> Option<String> {
6 cstr_to_rust_with_size(c, None)
7}
8
9pub(crate) fn cstr_to_rust_with_size(c: *const c_char, size: Option<usize>) -> Option<String> {
10 if c.is_null() {
11 return None;
12 }
13 let (mut s, max) = match size {
14 Some(len) => (Vec::with_capacity(len), len as isize),
15 None => (Vec::new(), isize::MAX),
16 };
17 let mut i = 0;
18 unsafe {
19 loop {
20 let value = *c.offset(i) as u8;
21 if value == 0 {
22 break;
23 }
24 s.push(value);
25 i += 1;
26 if i >= max {
27 break;
28 }
29 }
30 String::from_utf8(s).ok()
31 }
32}