| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices#; |
| 3 | use descaperust-analyzer cargo descape 3.0.0 crate/::*; |
| 4 | |
| 5 | macro_rules! ensure_errrust-analyzer cargo descape 3.0.0 ensure_err! { |
| 6 | ($($name: ident),+) => {$( |
| 7 | assert_eq!($name.to_unescaped().map_err(|err| err.index), Err(0), "{} parsed successfully when invalid", stringify!($name)); |
| 8 | )+}; |
| 9 | } |
| 10 | |
| 11 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 12 | fn test_escapesrust-analyzer cargo descape 3.0.0 test_escapes().() { |
| 13 | static ESCAPEDrust-analyzer cargo descape 3.0.0 ESCAPED.: &str = |
| 14 | r#"\a \b \t \n \v❤️\f \r \e \' \" \` \\ \u{0} \u{21}❤️\u{433} \u{FFFD} \u0000 \u0021 \uFFFD \x7E \xFF \0 \11 \100"#; |
| 15 | static UNESCAPEDrust-analyzer cargo descape 3.0.0 UNESCAPED.: &str = |
| 16 | "\x07 \x08 \t \n \x0B❤️\x0C \x0D \x1B \' \" ` \\ \u{0} \u{21}❤️\u{433} \u{FFFD} \u{0000} \u{0021} \u{FFFD} \x7E \u{FF} \0 \t @"; |
| 17 | static NO_ESCAPESrust-analyzer cargo descape 3.0.0 NO_ESCAPES.: &str = "No escapes here!"; |
| 18 | static BAD_ESCAPErust-analyzer cargo descape 3.0.0 BAD_ESCAPE.: &str = r"\Z"; |
| 19 | static CUT_ESCAPErust-analyzer cargo descape 3.0.0 CUT_ESCAPE.: &str = r"\"; |
| 20 | static BAD_UNICODErust-analyzer cargo descape 3.0.0 BAD_UNICODE.: &str = r"\u{This is definitely not hexadecimal}"; |
| 21 | static EMPTY_UNICODErust-analyzer cargo descape 3.0.0 EMPTY_UNICODE.: &str = r"\u{}"; |
| 22 | static CUT_UNICODErust-analyzer cargo descape 3.0.0 CUT_UNICODE.: &str = r"\u{03"; |
| 23 | static BAD_HEXrust-analyzer cargo descape 3.0.0 BAD_HEX.: &str = r"\xGG"; |
| 24 | static CUT_HEXrust-analyzer cargo descape 3.0.0 CUT_HEX.: &str = r"\xA"; |
| 25 | static EMPTY_HEXrust-analyzer cargo descape 3.0.0 EMPTY_HEX.: &str = r"\x"; |
| 26 | static NON_UNICODErust-analyzer cargo descape 3.0.0 NON_UNICODE.: &str = r"\u{D800}"; |
| 27 | |
| 28 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 29 | ESCAPEDrust-analyzer cargo descape 3.0.0 ESCAPED..to_unescapedrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped().() |
| 30 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| &ESCAPEDrust-analyzer cargo descape 3.0.0 ESCAPED.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...err.indexrust-analyzer cargo descape 3.0.0 InvalidEscape#index.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) |
| 31 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("should not reject legal escaped string"), |
| 32 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#::<'_, str>(UNESCAPEDrust-analyzer cargo descape 3.0.0 UNESCAPED..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 33 | ); |
| 34 | |
| 35 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 36 | NO_ESCAPESrust-analyzer cargo descape 3.0.0 NO_ESCAPES..to_unescapedrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped().() |
| 37 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| &ESCAPEDrust-analyzer cargo descape 3.0.0 ESCAPED.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...err.indexrust-analyzer cargo descape 3.0.0 InvalidEscape#index.]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) |
| 38 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("should not reject legal escaped string"), |
| 39 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::Borrowedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Borrowed#(NO_ESCAPESrust-analyzer cargo descape 3.0.0 NO_ESCAPES.) |
| 40 | ); |
| 41 | |
| 42 | ensure_errrust-analyzer cargo descape 3.0.0 ensure_err!!( |
| 43 | BAD_ESCAPErust-analyzer cargo descape 3.0.0 BAD_ESCAPE., |
| 44 | CUT_ESCAPErust-analyzer cargo descape 3.0.0 CUT_ESCAPE., |
| 45 | BAD_UNICODErust-analyzer cargo descape 3.0.0 BAD_UNICODE., |
| 46 | EMPTY_UNICODErust-analyzer cargo descape 3.0.0 EMPTY_UNICODE., |
| 47 | CUT_UNICODErust-analyzer cargo descape 3.0.0 CUT_UNICODE., |
| 48 | BAD_HEXrust-analyzer cargo descape 3.0.0 BAD_HEX., |
| 49 | CUT_HEXrust-analyzer cargo descape 3.0.0 CUT_HEX., |
| 50 | EMPTY_HEXrust-analyzer cargo descape 3.0.0 EMPTY_HEX., |
| 51 | NON_UNICODErust-analyzer cargo descape 3.0.0 NON_UNICODE. |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | fn custom_escrust-analyzer cargo descape 3.0.0 custom_esc().<'a>(_: usize, chr: char, iter: &mut CharIndicesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/CharIndices#<'a>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#<'a>, ()> { |
| 56 | if chr rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialEq<Self>`]eq().'T' { |
| 57 | let (_, next) = iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`CharIndices<'a>`][Iterator]next().().ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 58 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((match next { |
| 59 | 'a' => 'g', |
| 60 | 'o' => 'p', |
| 61 | _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 62 | }).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()); |
| 63 | } |
| 64 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(EscapeValuerust-analyzer cargo descape 3.0.0 EscapeValue#::Removerust-analyzer cargo descape 3.0.0 EscapeValue#Remove#) |
| 65 | } |
| 66 | |
| 67 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 68 | fn test_customsrust-analyzer cargo descape 3.0.0 test_customs().() { |
| 69 | r"Hello \T world".to_unescaped_withrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped_with().(custom_escrust-analyzer cargo descape 3.0.0 custom_esc().) |
| 70 | .expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().(r"custom escape should fail for \T"); |
| 71 | r"Foo \Tg bar".to_unescaped_withrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped_with().(custom_escrust-analyzer cargo descape 3.0.0 custom_esc().) |
| 72 | .expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().(r"custom escape should fail for \Tg"); |
| 73 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 74 | r"Spam E\Tags".to_unescaped_withrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped_with().(custom_escrust-analyzer cargo descape 3.0.0 custom_esc().).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().(r"custom escape should succeed for \Ta"), |
| 75 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::<'static, str>::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("Spam Eggs")), |
| 76 | "custom escape gave incorrect result" |
| 77 | ); |
| 78 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 79 | r"Bee\To \n Boop".to_unescaped_withrust-analyzer cargo descape 3.0.0 impl#[str][UnescapeExt]to_unescaped_with().(custom_escrust-analyzer cargo descape 3.0.0 custom_esc().).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().(r"custom escape should succeed for \To"), |
| 80 | Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#::<'static, str>::Ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#Owned#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("Beep Boop")), |
| 81 | "custom escape gave incorrect result" |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 |