Crate escape_bytes

Source
Expand description

Escapes bytes that are not printable ASCII characters.

The exact rules are:

  • Nul is escaped as \0.
  • Tab is escaped as \t.
  • Line feed is escaped as \n.
  • Carriage return is escaed as \r.
  • Backslach is escaped as \\.
  • Any character in the printable ASCII range 0x20..=0x7e is not escaped.
  • Any other character is hex escaped in the form \xNN.

Intended for use where byte sequences are not valid ASCII or UTF-8 but need to be stored in a semi-human readable form where only ASCII or UTF-8 are permitted.

§Examples

§Escape

let str = b"hello\xc3world";
let escaped = escape_bytes::escape(str);
assert_eq!(escaped, br"hello\xc3world");

§Unescape

let escaped = br"hello\xc3world";
let unescaped = escape_bytes::unescape(escaped)?;
assert_eq!(unescaped, b"hello\xc3world");

Structs§

Escape
Iterator that escapes the input iterator.
Unescape
Iterator that unescapes the input iterator.

Enums§

EscapeIntoError
Escape into error occurs when escaping into a slice cannot continue.
UnescapeError
Unescape error occurs when encountering unexpected byte sequences.
UnescapeIntoError
Escape into error occurs when escaping into a slice cannot continue.

Functions§

escape
Escape the bytes.
escape_into
Escape the bytes into the slice.
escaped_len
Returns the escaped length of the input.
escaped_max_len
Returns the maximum escaped length of the given unescaped length.
unescape
Unescape the bytes previously escaped.
unescape_into
Unescape the bytes into the slice.
unescaped_len
Returns the unescaped length of the input.