|
|
@@ -37,14 +37,11 @@
|
|
|
//! assert_eq!(utf8_percent_encode("foo <bar>", FRAGMENT).to_string(), "foo%20%3Cbar%3E");
|
|
|
//! ```
|
|
|
|
|
|
-#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
+#![no_std]
|
|
|
+extern crate alloc;
|
|
|
|
|
|
-#[cfg(not(feature = "std"))]
|
|
|
+use alloc::{borrow::{Cow, ToOwned}, vec::Vec, string::String};
|
|
|
use core::{fmt, mem, slice, str};
|
|
|
-#[cfg(feature = "std")]
|
|
|
-use std::borrow::Cow;
|
|
|
-#[cfg(feature = "std")]
|
|
|
-use std::{fmt, mem, slice, str};
|
|
|
|
|
|
/// Represents a set of characters or bytes in the ASCII range.
|
|
|
///
|
|
|
@@ -293,7 +290,6 @@ impl<'a> fmt::Display for PercentEncode<'a> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[cfg(feature = "std")]
|
|
|
impl<'a> From<PercentEncode<'a>> for Cow<'a, str> {
|
|
|
fn from(mut iter: PercentEncode<'a>) -> Self {
|
|
|
match iter.next() {
|
|
|
@@ -311,6 +307,42 @@ impl<'a> From<PercentEncode<'a>> for Cow<'a, str> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#[cfg(feature = "beef")]
|
|
|
+impl<'a> From<PercentEncode<'a>> for beef::Cow<'a, str> {
|
|
|
+ fn from(mut iter: PercentEncode<'a>) -> Self {
|
|
|
+ match iter.next() {
|
|
|
+ None => "".into(),
|
|
|
+ Some(first) => match iter.next() {
|
|
|
+ None => first.into(),
|
|
|
+ Some(second) => {
|
|
|
+ let mut string = first.to_owned();
|
|
|
+ string.push_str(second);
|
|
|
+ string.extend(iter);
|
|
|
+ string.into()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(all(feature = "beef", target_pointer_width = "64"))]
|
|
|
+impl<'a> From<PercentEncode<'a>> for beef::lean::Cow<'a, str> {
|
|
|
+ fn from(mut iter: PercentEncode<'a>) -> Self {
|
|
|
+ match iter.next() {
|
|
|
+ None => "".into(),
|
|
|
+ Some(first) => match iter.next() {
|
|
|
+ None => first.into(),
|
|
|
+ Some(second) => {
|
|
|
+ let mut string = first.to_owned();
|
|
|
+ string.push_str(second);
|
|
|
+ string.extend(iter);
|
|
|
+ string.into()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/// Percent-decode the given string.
|
|
|
///
|
|
|
/// <https://url.spec.whatwg.org/#string-percent-decode>
|
|
|
@@ -332,18 +364,13 @@ pub fn percent_decode_str(input: &str) -> PercentDecode<'_> {
|
|
|
/// * Implements `Iterator<Item = u8>` and therefore has a `.collect::<Vec<u8>>()` method,
|
|
|
/// * Has `decode_utf8()` and `decode_utf8_lossy()` methods.
|
|
|
///
|
|
|
-#[cfg_attr(
|
|
|
- feature = "std",
|
|
|
- doc = r##"
|
|
|
-# Examples
|
|
|
-
|
|
|
-```
|
|
|
-use percent_encoding::percent_decode;
|
|
|
-
|
|
|
-assert_eq!(percent_decode(b"foo%20bar%3f").decode_utf8().unwrap(), "foo bar?");
|
|
|
-```
|
|
|
-"##
|
|
|
-)]
|
|
|
+/// # Examples
|
|
|
+///
|
|
|
+/// ```
|
|
|
+/// use percent_encoding::percent_decode;
|
|
|
+///
|
|
|
+/// assert_eq!(percent_decode(b"foo%20bar%3f").decode_utf8().unwrap(), "foo bar?");
|
|
|
+/// ```
|
|
|
#[inline]
|
|
|
pub fn percent_decode(input: &[u8]) -> PercentDecode<'_> {
|
|
|
PercentDecode {
|
|
|
@@ -384,7 +411,6 @@ impl<'a> Iterator for PercentDecode<'a> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[cfg(feature = "std")]
|
|
|
impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
|
|
|
fn from(iter: PercentDecode<'a>) -> Self {
|
|
|
match iter.if_any() {
|
|
|
@@ -394,9 +420,28 @@ impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+#[cfg(feature = "beef")]
|
|
|
+impl<'a> From<PercentDecode<'a>> for beef::Cow<'a, [u8]> {
|
|
|
+ fn from(iter: PercentDecode<'a>) -> Self {
|
|
|
+ match iter.if_any() {
|
|
|
+ Some(vec) => beef::Cow::owned(vec),
|
|
|
+ None => beef::Cow::borrowed(iter.bytes.as_slice()),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(all(feature = "beef", target_pointer_width = "64"))]
|
|
|
+impl<'a> From<PercentDecode<'a>> for beef::lean::Cow<'a, [u8]> {
|
|
|
+ fn from(iter: PercentDecode<'a>) -> Self {
|
|
|
+ match iter.if_any() {
|
|
|
+ Some(vec) => beef::lean::Cow::owned(vec),
|
|
|
+ None => beef::lean::Cow::borrowed(iter.bytes.as_slice()),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
impl<'a> PercentDecode<'a> {
|
|
|
/// If the percent-decoding is different from the input, return it as a new bytes vector.
|
|
|
- #[cfg(feature = "std")]
|
|
|
fn if_any(&self) -> Option<Vec<u8>> {
|
|
|
let mut bytes_iter = self.bytes.clone();
|
|
|
while bytes_iter.any(|&b| b == b'%') {
|
|
|
@@ -416,7 +461,6 @@ impl<'a> PercentDecode<'a> {
|
|
|
/// Decode the result of percent-decoding as UTF-8.
|
|
|
///
|
|
|
/// This is return `Err` when the percent-decoded bytes are not well-formed in UTF-8.
|
|
|
- #[cfg(feature = "std")]
|
|
|
pub fn decode_utf8(self) -> Result<Cow<'a, str>, str::Utf8Error> {
|
|
|
match self.clone().into() {
|
|
|
Cow::Borrowed(bytes) => match str::from_utf8(bytes) {
|
|
|
@@ -434,13 +478,11 @@ impl<'a> PercentDecode<'a> {
|
|
|
///
|
|
|
/// Invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD,
|
|
|
/// the replacement character.
|
|
|
- #[cfg(feature = "std")]
|
|
|
pub fn decode_utf8_lossy(self) -> Cow<'a, str> {
|
|
|
decode_utf8_lossy(self.clone().into())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-#[cfg(feature = "std")]
|
|
|
fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
|
|
|
// Note: This function is duplicated in `form_urlencoded/src/query_encoding.rs`.
|
|
|
match input {
|