Function grep_cli::escape

source ·
pub fn escape(bytes: &[u8]) -> String
Expand description

Escapes arbitrary bytes into a human readable string.

This converts \t, \r and \n into their escaped forms. It also converts the non-printable subset of ASCII in addition to invalid UTF-8 bytes to hexadecimal escape sequences. Everything else is left as is.

The dual of this routine is unescape.

§Example

This example shows how to convert a byte string that contains a \n and invalid UTF-8 bytes into a String.

Pay special attention to the use of raw strings. That is, r"\n" is equivalent to "\\n".

use grep_cli::escape;

assert_eq!(r"foo\nbar\xFFbaz", escape(b"foo\nbar\xFFbaz"));