Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. use percent_encoding; // 1.0.1
  2. use percent_encoding::{percent_decode, percent_encode, DEFAULT_ENCODE_SET};
  3. use std::borrow::Cow;
  4.  
  5. fn main() {
  6. // has invalid sequence of bytes (non-utf8)
  7. let trash_file = b"tras \xff\xee\xef\xced d h.txt";
  8. let percent_encoded = percent_encode(trash_file, DEFAULT_ENCODE_SET).to_string();
  9. println!("encoding: {}", &percent_encoded);
  10. println!("expected: {}", "tras%20%FF%EE%EF%CEd%20d%20h.txt");
  11. // encoding is right
  12.  
  13. // How to get back the original bytes though?
  14. let percent_decoded = percent_decode(percent_encoded.as_bytes()).if_any()
  15. .map_or(Cow::Borrowed(percent_encoded.as_bytes()), Cow::Owned);
  16. assert_eq!(trash_file, &*percent_decoded);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement