Rewrite WUtils string.c in Rust #3
Reference in New Issue
Block a user
Delete Branch "trurl/wmaker:refactor/wutil-rs-string"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This should be pretty straightforward.
There's a case to be made that the functions which see very little use (wtokenjoin, wtokensplit) should belong elsewhere, but it all needs to be rewritten in Rust or factored away regardless.
wtokenjoin. 091c86634e@@ -0,0 +28,4 @@pub unsafe extern "C" fn wstrndup(s: *const c_char, len: usize) -> *mut c_char {assert!(!s.is_null());let s = unsafe { CStr::from_ptr(s) };let len = usize::min(len, s.count_bytes()) + 1;I don't think this is what you want, necessarily. You want to find a zero byte, sure, but looking beyond the first
lenbytes is not useful and may not even be correct, if the source string is not e.g. 0 terminated. I would argue that's a bug, but I could be wrong.So I'd do something like this:
Thanks, this is an improvement. (The extant
wstrndupcalledstrlenons, so I didn't feel too badly about scanning ahead for a null byte ... but we can do better.)I'd like to migrate away from supporting C-style strings, so I'm putting the length computation inline.
@@ -0,0 +104,4 @@// TODO: complain.return ptr::null_mut();};let s: Vec<_> = s.trim().bytes().chain(iter::once(b'\0')).collect();So this will allocate a
Vec<u8>to hold the trimmed bytes, and then invokealloc_stringto do another allocation. I don't know if there's a good way around that; perhaps usestrndup? Something like,This is an improvement. Thanks!
b574880350toc298b5f96f