Line data Source code
1 : //! Small parsing helpers.
2 :
3 : use std::ffi::CStr;
4 :
5 17 : pub(crate) fn split_cstr(bytes: &[u8]) -> Option<(&CStr, &[u8])> {
6 17 : let cstr = CStr::from_bytes_until_nul(bytes).ok()?;
7 15 : let (_, other) = bytes.split_at(cstr.to_bytes_with_nul().len());
8 15 : Some((cstr, other))
9 17 : }
10 :
11 : #[cfg(test)]
12 : mod tests {
13 : use super::*;
14 :
15 : #[test]
16 1 : fn test_split_cstr() {
17 1 : assert!(split_cstr(b"").is_none());
18 1 : assert!(split_cstr(b"foo").is_none());
19 :
20 1 : let (cstr, rest) = split_cstr(b"\0").expect("uh-oh");
21 1 : assert_eq!(cstr.to_bytes(), b"");
22 1 : assert_eq!(rest, b"");
23 :
24 1 : let (cstr, rest) = split_cstr(b"foo\0bar").expect("uh-oh");
25 1 : assert_eq!(cstr.to_bytes(), b"foo");
26 1 : assert_eq!(rest, b"bar");
27 1 : }
28 : }
|