query_encoding.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2019 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. use std::borrow::Cow;
  9. pub type EncodingOverride<'a> = Option<&'a dyn Fn(&str) -> Cow<'_, [u8]>>;
  10. pub(crate) fn encode<'a>(encoding_override: EncodingOverride<'_>, input: &'a str) -> Cow<'a, [u8]> {
  11. if let Some(o) = encoding_override {
  12. return o(input);
  13. }
  14. input.as_bytes().into()
  15. }
  16. pub(crate) fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
  17. // Note: This function is duplicated in `percent_encoding/lib.rs`.
  18. match input {
  19. Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
  20. Cow::Owned(bytes) => {
  21. match String::from_utf8_lossy(&bytes) {
  22. Cow::Borrowed(utf8) => {
  23. // If from_utf8_lossy returns a Cow::Borrowed, then we can
  24. // be sure our original bytes were valid UTF-8. This is because
  25. // if the bytes were invalid UTF-8 from_utf8_lossy would have
  26. // to allocate a new owned string to back the Cow so it could
  27. // replace invalid bytes with a placeholder.
  28. // First we do a debug_assert to confirm our description above.
  29. let raw_utf8: *const [u8];
  30. raw_utf8 = utf8.as_bytes();
  31. debug_assert!(raw_utf8 == &*bytes as *const [u8]);
  32. // Given we know the original input bytes are valid UTF-8,
  33. // and we have ownership of those bytes, we re-use them and
  34. // return a Cow::Owned here.
  35. Cow::Owned(unsafe { String::from_utf8_unchecked(bytes) })
  36. }
  37. Cow::Owned(s) => Cow::Owned(s),
  38. }
  39. }
  40. }
  41. }