#escaping #string #unicode

no-std descape

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

2 stable releases

1.1.2 Feb 27, 2024
1.0.1 Feb 26, 2024

#74 in Parser tooling

Download history 383/week @ 2024-02-25 21/week @ 2024-03-03 1/week @ 2024-03-10 47/week @ 2024-03-31 43/week @ 2024-04-07

90 downloads per month

Apache-2.0 OR MIT

10KB
125 lines

GitHub Actions Workflow Status Coverage Documentation MSRV Repository Latest version License unsafe forbidden

Adds a single extension trait for &str to unescape any backslashes. Supports no-std.

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}

UTF-8 escapes specified by multiple consecutive \xNN escapes will not work as intended, producing mojibake. It's assumed that the escaped data is already UTF-8 encoded.


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 7
let invalid_escape = "Uh oh! \\xJJ".to_unescaped();
assert_eq!(
    invalid_escape,
    Err(7)
);

No runtime deps