descape
Provides utilities for easily parsing escape sequences in a string, using alloc::borrow::Cow to only borrow when needed.
This library supports many escape sequences:
\\a->\x07\\b->\x08\\t->\x09\\n->\x0A\\v->\x0B\\f->\x0C\\r->\x0D\\e->\x1B\\'->'\\"->"- \\` -> `
\\\\->\\\\xNN->\xNN\\o->\o, for all octal digitso\\oo->\oo, for all octal digitso\\ooo->\ooo, for all octal digitso\\uXXXX->\u{XXXX}\\u{HEX}->\u{HEX}
Along with this, you can define your own custom escape handlers!
Custom escape handlers support multi-character escape results, skipping past escape sequences, and even custom escape prefixes!
See descape::EscapeHandler.
This crate supports no-std.
Optionally, this crate has the std and core_error features,
to allow the error type of an invalid escape to implement the Error trait.
std uses std::error::Error, and core_error depends on core::error::Error, which is stable on Rust 1.82.0 or greater.
Examples
Parsing an escaped string
let escaped = "Hello,\\nworld!".to_unescaped;
assert_eq!;
Not allocating for a string without escapes
let no_escapes = "No escapes here!".to_unescaped;
assert_eq!;
Erroring for invalid escapes
// v invalid at index 7
let invalid_escape = r"Uh oh! \xJJ".to_unescaped;
assert_eq!;
Permitting any escape, handing it back raw
let escaped = r"\H\e\l\l\o \n \W\o\r\l\d";
let unescaped = escaped.to_unescaped_with.expect;
assert_eq!;
Removing escape sequences entirely
let escaped = r"What if I want a \nnewline?";
let unescaped = escaped.to_unescaped_with.expect;
assert_eq!;
Not allowing escape sequences unsupported by Rust
r"This is \nfine".to_unescaped_with.expect;
r"This is not \fine".to_unescaped_with.expect_err;
Custom escape prefixes
;
assert_eq!