#escaping #byte #escape-bytes

no-std escape-bytes

Escapes bytes that are not printable ASCII characters

2 releases

0.1.1 Dec 1, 2023
0.1.0 Dec 1, 2023
0.0.1 Nov 30, 2023

#1005 in Text processing

Download history 506/week @ 2024-12-25 2134/week @ 2025-01-01 2760/week @ 2025-01-08 3049/week @ 2025-01-15 3299/week @ 2025-01-22 2941/week @ 2025-01-29 2794/week @ 2025-02-05 3205/week @ 2025-02-12 3255/week @ 2025-02-19 3042/week @ 2025-02-26 3983/week @ 2025-03-05 4676/week @ 2025-03-12 3590/week @ 2025-03-19 3640/week @ 2025-03-26 2961/week @ 2025-04-02 2696/week @ 2025-04-09

13,858 downloads per month
Used in 101 crates (4 directly)

Apache-2.0

50KB
987 lines

escape-bytes

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");

License: Apache-2.0

No runtime deps

Features