descape 1.0.1

Adds a single extension trait for &str to unescape any backslashes.
Documentation
descape-1.0.1 has been yanked.

Repository Documentation Latest version MSRV License unsafe forbidden

Adds a single extension trait for &str to unescape any backslashes.

Supports no-std. Will not panic, and doesn't bring in panic handling code when compiled with release optimizations.

Unescaping is designed to support as many languages as possible.

The following escapes are valid:

  • \\n -> \n
  • \\r -> \r
  • \\t -> \t
  • \\b -> \x08
  • \\f -> \x0C
  • \\' -> '
  • \\" -> "
  • \\\\ -> \\
  • \\xNN -> \xNN
  • \\o -> \o
  • \\oo -> \oo
  • \\ooo -> \ooo
  • \\uXXXX -> \u{XXXX}
  • \\u{HEX} -> \u{HEX}

use alloc::borrow::Cow;
use descape::UnescapeExt;

let escaped = "Hello,\\nworld!".to_unescaped();
assert_eq!(
    escaped,
    Ok(Cow::Owned::<'_, str>("Hello,\nworld!".to_string()))
);

let no_escapes = "No escapes here!".to_unescaped();
assert_eq!(
    no_escapes,
    Ok(Cow::Borrowed("No escapes here!"))
);

//                              v  invalid at index 9
let invalid_escape = "Uh oh! \\xJJ".to_unescaped();
assert_eq!(
    invalid_escape,
    Err(9)
);