LCOV - differential code coverage report
Current view: top level - proxy/src - parse.rs (source / functions) Coverage Total Hit CBC
Current: cd44433dd675caa99df17a61b18949c8387e2242.info Lines: 100.0 % 28 28 28
Current Date: 2024-01-09 02:06:09 Functions: 100.0 % 11 11 11
Baseline: 66c52a629a0f4a503e193045e0df4c77139e344b.info
Baseline Date: 2024-01-08 15:34:46

           TLA  Line data    Source code
       1                 : //! Small parsing helpers.
       2                 : 
       3                 : use std::ffi::CStr;
       4                 : 
       5 CBC          52 : pub fn split_cstr(bytes: &[u8]) -> Option<(&CStr, &[u8])> {
       6              52 :     let cstr = CStr::from_bytes_until_nul(bytes).ok()?;
       7              50 :     let (_, other) = bytes.split_at(cstr.to_bytes_with_nul().len());
       8              50 :     Some((cstr, other))
       9              52 : }
      10                 : 
      11                 : /// See <https://doc.rust-lang.org/std/primitive.slice.html#method.split_array_ref>.
      12              51 : pub fn split_at_const<const N: usize>(bytes: &[u8]) -> Option<(&[u8; N], &[u8])> {
      13              51 :     (bytes.len() >= N).then(|| {
      14              50 :         let (head, tail) = bytes.split_at(N);
      15              50 :         (head.try_into().unwrap(), tail)
      16              51 :     })
      17              51 : }
      18                 : 
      19                 : #[cfg(test)]
      20                 : mod tests {
      21                 :     use super::*;
      22                 : 
      23               1 :     #[test]
      24               1 :     fn test_split_cstr() {
      25               1 :         assert!(split_cstr(b"").is_none());
      26               1 :         assert!(split_cstr(b"foo").is_none());
      27                 : 
      28               1 :         let (cstr, rest) = split_cstr(b"\0").expect("uh-oh");
      29               1 :         assert_eq!(cstr.to_bytes(), b"");
      30               1 :         assert_eq!(rest, b"");
      31                 : 
      32               1 :         let (cstr, rest) = split_cstr(b"foo\0bar").expect("uh-oh");
      33               1 :         assert_eq!(cstr.to_bytes(), b"foo");
      34               1 :         assert_eq!(rest, b"bar");
      35               1 :     }
      36                 : 
      37               1 :     #[test]
      38               1 :     fn test_split_at_const() {
      39               1 :         assert!(split_at_const::<0>(b"").is_some());
      40               1 :         assert!(split_at_const::<1>(b"").is_none());
      41               1 :         assert!(matches!(split_at_const::<1>(b"ok"), Some((b"o", b"k"))));
      42               1 :     }
      43                 : }
        

Generated by: LCOV version 2.1-beta