fix some string issues, introduce to_string_lossy such that to_string() continues to panic on non-utf8

This commit is contained in:
holger krekel
2019-07-20 12:30:48 +02:00
parent 38eb708db8
commit d6de420b9a
4 changed files with 22 additions and 6 deletions

View File

@@ -1561,9 +1561,20 @@ pub fn to_string(s: *const libc::c_char) -> String {
let cstr = unsafe { CStr::from_ptr(s) };
cstr.to_str().map(|s| s.to_string()).unwrap_or_else(|err| {
let lossy = cstr.to_string_lossy();
eprintln!("Non utf8 string: '{:?}' ({:?})", lossy, err);
lossy.to_string()
panic!("Non utf8 string: '{:?}' ({:?})", cstr.to_string_lossy(), err);
})
}
pub fn to_string_lossy(s: *const libc::c_char) -> String {
if s.is_null() {
return "".into();
}
let cstr = unsafe { CStr::from_ptr(s) };
cstr.to_str().map(|s| s.to_string()).unwrap_or_else(|_| {
cstr.to_string_lossy().to_string()
})
}