Unescapes a testdrive byte string. The escape character is `\` and the only interesting escape sequence is `\xNN`, where each `N` is a valid hexadecimal digit. All other characters following a backslash are taken literally.
(s: &[u8])
| 15 | /// `\xNN`, where each `N` is a valid hexadecimal digit. All other characters |
| 16 | /// following a backslash are taken literally. |
| 17 | pub fn unescape(s: &[u8]) -> Result<Vec<u8>, anyhow::Error> { |
| 18 | let mut out = vec![]; |
| 19 | let mut s = s.iter().copied().fuse(); |
| 20 | while let Some(b) = s.next() { |
| 21 | match b { |
| 22 | b'\\' if s.next() == Some(b'x') => match (next_hex(&mut s), next_hex(&mut s)) { |
| 23 | (Some(c1), Some(c0)) => out.push((c1 << 4) + c0), |
| 24 | _ => bail!("invalid hexadecimal escape"), |
| 25 | }, |
| 26 | b'\\' => continue, |
| 27 | _ => out.push(b), |
| 28 | } |
| 29 | } |
| 30 | Ok(out) |
| 31 | } |
| 32 | |
| 33 | /// Retrieves the value of the next hexadecimal digit in `iter`, if the next |
| 34 | /// byte is a valid hexadecimal digit. |