escape_bytes/
lib.rs

1//! Escapes bytes that are not printable ASCII characters.
2//!
3//! The exact rules are:
4//! - Nul is escaped as `\0`.
5//! - Tab is escaped as `\t`.
6//! - Line feed is escaped as `\n`.
7//! - Carriage return is escaed as `\r`.
8//! - Backslach is escaped as `\\`.
9//! - Any character in the printable ASCII range `0x20`..=`0x7e` is not escaped.
10//! - Any other character is hex escaped in the form `\xNN`.
11//!
12//! Intended for use where byte sequences are not valid ASCII or UTF-8 but need
13//! to be stored in a semi-human readable form where only ASCII or UTF-8 are
14//! permitted.
15//!
16//! ## Examples
17//!
18//! ### Escape
19//!
20//! ```rust
21//! let str = b"hello\xc3world";
22//! let escaped = escape_bytes::escape(str);
23//! assert_eq!(escaped, br"hello\xc3world");
24//! ```
25//!
26//! ### Unescape
27//!
28//! ```rust
29//! # fn main() -> Result<(), escape_bytes::UnescapeError> {
30//! let escaped = br"hello\xc3world";
31//! let unescaped = escape_bytes::unescape(escaped)?;
32//! assert_eq!(unescaped, b"hello\xc3world");
33//! # Ok(())
34//! # }
35//! ```
36
37#![no_std]
38#![allow(clippy::module_name_repetitions)]
39
40#[cfg(feature = "alloc")]
41extern crate alloc;
42
43#[cfg(test)]
44mod test;
45
46mod escape;
47pub use escape::*;
48
49mod unescape;
50pub use unescape::*;