#escaping #ascii #byte-sequences #characters #forms #printable #utf-8

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

#234 in Text processing

Download history 1601/week @ 2024-01-03 2286/week @ 2024-01-10 2450/week @ 2024-01-17 1813/week @ 2024-01-24 2365/week @ 2024-01-31 2145/week @ 2024-02-07 1836/week @ 2024-02-14 2620/week @ 2024-02-21 2448/week @ 2024-02-28 1628/week @ 2024-03-06 1854/week @ 2024-03-13 2650/week @ 2024-03-20 2319/week @ 2024-03-27 2986/week @ 2024-04-03 3495/week @ 2024-04-10 2480/week @ 2024-04-17

12,188 downloads per month
Used in 58 crates (2 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


lib.rs:

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

No runtime deps

Features