pub fn unescape(s: &str) -> Vec<u8> ⓘ
Expand description
Unescapes a string.
It supports a limited set of escape sequences:
\t
,\r
and\n
are mapped to their corresponding ASCII bytes.\xZZ
hexadecimal escapes are mapped to their byte.
Everything else is left as is, including non-hexadecimal escapes like
\xGG
.
This is useful when it is desirable for a command line argument to be capable of specifying arbitrary bytes or otherwise make it easier to specify non-printable characters.
The dual of this routine is escape
.
Example
This example shows how to convert an escaped string (which is valid UTF-8) into a corresponding sequence of bytes. Each escape sequence is mapped to its bytes, which may include invalid UTF-8.
Pay special attention to the use of raw strings. That is, r"\n"
is
equivalent to "\\n"
.
use grep_cli::unescape;
assert_eq!(&b"foo\nbar\xFFbaz"[..], &*unescape(r"foo\nbar\xFFbaz"));