I want to be able to do something like this.
- Take a C pointer, iterator over the characters, map the charactors to json which might mean quoting them string and escaping charactors.
- Implement a printfunction that takes an Iterator that returns u8 and print the charactors, that can take (1) above.
- Be able to collect the values from iterator and collect into a CString or
let cstr = CStr::from_bytes_with_nul(b"hello world\0");
let cstrptr = cstr.as_ptr();
printcstrptrfromiterator(CStrPtr::new(cstrptr).iter().tojson());
let X = CStrPtr::new(cstrptr).iter().tojson().collect(); # returns bytearray or CString
I currently have a CStrPtr struct that implements an Iterator trait.
struct CStrPtr {
cstrptr: *const u8;
cptr: *const u8;
}
impl CStrPtr {
pub fn new(p: *const u8) -> CStrPtr {
CStrPtr { sptr: p, cptr: p}
}
}
impl Iterator for CStrPtr {
type Item = u8;
fn next(&mut self) -> Option<u8> {
let p = unsafe { p.add(1) };
if *p == 0 {
None
} else {
Some(*p as u8)
}
}
}
I think what I have above gives me an iterator of the u8 in the C string pointer.
I need to implement tojson() that takes the iterator and maps the characters and possibly insert/return additional characters that turn the C String to a quoted json string.
And then collect the characters to return a CString or buffer/array.
I can't find good examples of how to do this.
- I suspect tojson() would be a like the map() method.
- Implement collect() that returns an array/CString.