#ascii #escaping #byte-sequences #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

#1189 in Text processing

Download history 2714/week @ 2024-07-22 4064/week @ 2024-07-29 3824/week @ 2024-08-05 2038/week @ 2024-08-12 3552/week @ 2024-08-19 4153/week @ 2024-08-26 2586/week @ 2024-09-02 3724/week @ 2024-09-09 2719/week @ 2024-09-16 3122/week @ 2024-09-23 2563/week @ 2024-09-30 2435/week @ 2024-10-07 2366/week @ 2024-10-14 2240/week @ 2024-10-21 2554/week @ 2024-10-28 3282/week @ 2024-11-04

10,544 downloads per month
Used in 82 crates (3 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