MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / unescape

Function unescape

src/testdrive/src/format/bytes.rs:17–31  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

15/// `\xNN`, where each `N` is a valid hexadecimal digit. All other characters
16/// following a backslash are taken literally.
17pub 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.

Callers 2

build_contentsFunction · 0.85
transcodeMethod · 0.85

Calls 5

next_hexFunction · 0.85
fuseMethod · 0.80
iterMethod · 0.45
nextMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected