| 1 | [](https://github.com/balt-dev/descape/actions/) |
| 2 | [](https://coveralls.io/github/balt-dev/descape/) |
| 3 | [](https://docs.rs/descape) |
| 4 | [](https://gist.github.com/alexheretic/d1e98d8433b602e57f5d0a9637927e0c) |
| 5 | [](https://github.com/balt-dev/descape) |
| 6 | [](https://crates.io/crates/descape) |
| 7 | [](https://github.com/balt-dev/descape/blob/master/LICENSE-MIT) |
| 8 | [](https://github.com/rust-secure-code/safety-dance/) |
| 9 | |
| 10 | |
| 11 | # descape |
| 12 | |
| 13 | Provides utilities for easily parsing escape sequences in a string, using `alloc::borrow::Cow` to only borrow when needed. |
| 14 | |
| 15 | This library supports many escape sequences: |
| 16 | - `\\a` -> `\x07` |
| 17 | - `\\b` -> `\x08` |
| 18 | - `\\t` -> `\x09` |
| 19 | - `\\n` -> `\x0A` |
| 20 | - `\\v` -> `\x0B` |
| 21 | - `\\f` -> `\x0C` |
| 22 | - `\\r` -> `\x0D` |
| 23 | - `\\e` -> `\x1B` |
| 24 | - `\\'` -> `'` |
| 25 | - `\\"` -> `"` |
| 26 | - <code>\\`</code> -> <code>`</code> |
| 27 | - `\\\\` -> `\\` |
| 28 | - `\\xNN` -> `\xNN` |
| 29 | - `\\o` -> `\o`, for all octal digits `o` |
| 30 | - `\\oo` -> `\oo`, for all octal digits `o` |
| 31 | - `\\ooo` -> `\ooo`, for all octal digits `o` |
| 32 | - `\\uXXXX` -> `\u{XXXX}` |
| 33 | - `\\u{HEX}` -> `\u{HEX}` |
| 34 | |
| 35 | Along with this, you can define your own custom escape handlers! |
| 36 | |
| 37 | Custom escape handlers support multi-character escape results, |
| 38 | skipping past escape sequences, |
| 39 | and even custom escape prefixes! |
| 40 | |
| 41 | See `descape::EscapeHandler`. |
| 42 | |
| 43 | |
| 44 | |
| 45 | This crate supports `no-std`. |
| 46 | |
| 47 | Optionally, this crate has the `std` and `core_error` features, |
| 48 | to allow the error type of an invalid escape to implement the `Error` trait. |
| 49 | |
| 50 | `std` uses `std::error::Error`, and `core_error` depends on `core::error::Error`, which is stable on Rust 1.82.0 or greater. |
| 51 | |
| 52 | ## Examples |
| 53 | |
| 54 | ### Parsing an escaped string |
| 55 | ```rust |
| 56 | let escaped = "Hello,\\nworld!".to_unescaped(); |
| 57 | assert_eq!( |
| 58 | escaped.unwrap(), |
| 59 | Cow::Owned::<'_, str>("Hello,\nworld!".to_string()) |
| 60 | ); |
| 61 | ``` |
| 62 | |
| 63 | ### Not allocating for a string without escapes |
| 64 | ```rust |
| 65 | let no_escapes = "No escapes here!".to_unescaped(); |
| 66 | assert_eq!( |
| 67 | no_escapes.unwrap(), |
| 68 | Cow::Borrowed("No escapes here!") |
| 69 | ); |
| 70 | ``` |
| 71 | |
| 72 | ### Erroring for invalid escapes |
| 73 | ```rust |
| 74 | // v invalid at index 7 |
| 75 | let invalid_escape = r"Uh oh! \xJJ".to_unescaped(); |
| 76 | assert_eq!( |
| 77 | invalid_escape.unwrap_err().index, |
| 78 | 7 |
| 79 | ); |
| 80 | ``` |
| 81 | |
| 82 | ## Permitting any escape, handing it back raw |
| 83 | ```rust |
| 84 | fn raw(idx: usize, chr: char, _: &mut CharIndices) -> Result<EscapeValue<'_>, ()> { |
| 85 | Ok(chr.into()) |
| 86 | } |
| 87 | |
| 88 | let escaped = r"\H\e\l\l\o \n \W\o\r\l\d"; |
| 89 | let unescaped = escaped.to_unescaped_with(raw).expect("this is fine"); |
| 90 | assert_eq!(unescaped, "Hello n World"); |
| 91 | ``` |
| 92 | |
| 93 | ## Removing escape sequences entirely |
| 94 | ```rust |
| 95 | fn raw(idx: usize, chr: char, _: &mut CharIndices) -> Result<EscapeValue<'_>, ()> { |
| 96 | Ok(EscapeValue::Remove) |
| 97 | } |
| 98 | |
| 99 | let escaped = r"What if I want a \nnewline?"; |
| 100 | let unescaped = escaped.to_unescaped_with(raw).expect("this should work"); |
| 101 | assert_eq!(unescaped, "What if I want a newline?"); |
| 102 | ``` |
| 103 | |
| 104 | ## Not allowing escape sequences unsupported by Rust |
| 105 | ```rust |
| 106 | fn rust_only(idx: usize, chr: char, iter: &mut CharIndices) -> Result<EscapeValue<'_>, ()> { |
| 107 | match chr { |
| 108 | 'a' | 'b' | 'v' | 'f' | 'e' | '`' => Err(()), |
| 109 | _ => descape::DefaultEscapeHandler.escape(idx, chr, iter) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | r"This is \nfine".to_unescaped_with(rust_only).expect(r"\n is valid"); |
| 114 | r"This is not \fine".to_unescaped_with(rust_only).expect_err(r"\f is invalid"); |
| 115 | ``` |
| 116 | |
| 117 | ## Custom escape prefixes |
| 118 | ```rust |
| 119 | struct PercentEscape; |
| 120 | impl EscapeHandler for PercentEscape { |
| 121 | fn prefix(&self) -> char { '%' }; |
| 122 | fn escape<'iter, 'source>(&mut self, idx: usize, chr: char, iter: &'iter mut CharIndices<'source>) -> Result<EscapeValue<'source>, ()> { |
| 123 | descape::DefaultEscapeHandler.escape(idx, chr, iter) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | assert_eq!( |
| 128 | r"Hello,%tworld!".to_unescaped_with(PercentEscape).unwrap(), |
| 129 | "Hello,\tworld!" |
| 130 | ) |
| 131 | ``` |
| 132 |