Skip to content

Commit 0229743

Browse files
committed
Use collections from the alloc crate in percent_encoding
1 parent 89876ff commit 0229743

File tree

2 files changed

+68
-27
lines changed

2 files changed

+68
-27
lines changed

percent_encoding/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ repository = "https://github.com/servo/rust-url/"
77
license = "MIT/Apache-2.0"
88
edition = "2018"
99

10-
[features]
11-
default = ["std"]
12-
std = []
10+
[dependencies]
11+
beef = { version = "0.5", optional = true }

percent_encoding/src/lib.rs

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,11 @@
3737
//! assert_eq!(utf8_percent_encode("foo <bar>", FRAGMENT).to_string(), "foo%20%3Cbar%3E");
3838
//! ```
3939
40-
#![cfg_attr(not(feature = "std"), no_std)]
40+
#![no_std]
41+
extern crate alloc;
4142

42-
#[cfg(not(feature = "std"))]
43+
use alloc::{borrow::{Cow, ToOwned}, vec::Vec, string::String};
4344
use core::{fmt, mem, slice, str};
44-
#[cfg(feature = "std")]
45-
use std::borrow::Cow;
46-
#[cfg(feature = "std")]
47-
use std::{fmt, mem, slice, str};
4845

4946
/// Represents a set of characters or bytes in the ASCII range.
5047
///
@@ -293,7 +290,6 @@ impl<'a> fmt::Display for PercentEncode<'a> {
293290
}
294291
}
295292

296-
#[cfg(feature = "std")]
297293
impl<'a> From<PercentEncode<'a>> for Cow<'a, str> {
298294
fn from(mut iter: PercentEncode<'a>) -> Self {
299295
match iter.next() {
@@ -311,6 +307,42 @@ impl<'a> From<PercentEncode<'a>> for Cow<'a, str> {
311307
}
312308
}
313309

310+
#[cfg(feature = "beef")]
311+
impl<'a> From<PercentEncode<'a>> for beef::Cow<'a, str> {
312+
fn from(mut iter: PercentEncode<'a>) -> Self {
313+
match iter.next() {
314+
None => "".into(),
315+
Some(first) => match iter.next() {
316+
None => first.into(),
317+
Some(second) => {
318+
let mut string = first.to_owned();
319+
string.push_str(second);
320+
string.extend(iter);
321+
string.into()
322+
}
323+
},
324+
}
325+
}
326+
}
327+
328+
#[cfg(all(feature = "beef", target_pointer_width = "64"))]
329+
impl<'a> From<PercentEncode<'a>> for beef::lean::Cow<'a, str> {
330+
fn from(mut iter: PercentEncode<'a>) -> Self {
331+
match iter.next() {
332+
None => "".into(),
333+
Some(first) => match iter.next() {
334+
None => first.into(),
335+
Some(second) => {
336+
let mut string = first.to_owned();
337+
string.push_str(second);
338+
string.extend(iter);
339+
string.into()
340+
}
341+
},
342+
}
343+
}
344+
}
345+
314346
/// Percent-decode the given string.
315347
///
316348
/// <https://url.spec.whatwg.org/#string-percent-decode>
@@ -332,18 +364,13 @@ pub fn percent_decode_str(input: &str) -> PercentDecode<'_> {
332364
/// * Implements `Iterator<Item = u8>` and therefore has a `.collect::<Vec<u8>>()` method,
333365
/// * Has `decode_utf8()` and `decode_utf8_lossy()` methods.
334366
///
335-
#[cfg_attr(
336-
feature = "std",
337-
doc = r##"
338-
# Examples
339-
340-
```
341-
use percent_encoding::percent_decode;
342-
343-
assert_eq!(percent_decode(b"foo%20bar%3f").decode_utf8().unwrap(), "foo bar?");
344-
```
345-
"##
346-
)]
367+
/// # Examples
368+
///
369+
/// ```
370+
/// use percent_encoding::percent_decode;
371+
///
372+
/// assert_eq!(percent_decode(b"foo%20bar%3f").decode_utf8().unwrap(), "foo bar?");
373+
/// ```
347374
#[inline]
348375
pub fn percent_decode(input: &[u8]) -> PercentDecode<'_> {
349376
PercentDecode {
@@ -384,7 +411,6 @@ impl<'a> Iterator for PercentDecode<'a> {
384411
}
385412
}
386413

387-
#[cfg(feature = "std")]
388414
impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
389415
fn from(iter: PercentDecode<'a>) -> Self {
390416
match iter.if_any() {
@@ -394,9 +420,28 @@ impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
394420
}
395421
}
396422

423+
#[cfg(feature = "beef")]
424+
impl<'a> From<PercentDecode<'a>> for beef::Cow<'a, [u8]> {
425+
fn from(iter: PercentDecode<'a>) -> Self {
426+
match iter.if_any() {
427+
Some(vec) => beef::Cow::owned(vec),
428+
None => beef::Cow::borrowed(iter.bytes.as_slice()),
429+
}
430+
}
431+
}
432+
433+
#[cfg(all(feature = "beef", target_pointer_width = "64"))]
434+
impl<'a> From<PercentDecode<'a>> for beef::lean::Cow<'a, [u8]> {
435+
fn from(iter: PercentDecode<'a>) -> Self {
436+
match iter.if_any() {
437+
Some(vec) => beef::lean::Cow::owned(vec),
438+
None => beef::lean::Cow::borrowed(iter.bytes.as_slice()),
439+
}
440+
}
441+
}
442+
397443
impl<'a> PercentDecode<'a> {
398444
/// If the percent-decoding is different from the input, return it as a new bytes vector.
399-
#[cfg(feature = "std")]
400445
fn if_any(&self) -> Option<Vec<u8>> {
401446
let mut bytes_iter = self.bytes.clone();
402447
while bytes_iter.any(|&b| b == b'%') {
@@ -416,7 +461,6 @@ impl<'a> PercentDecode<'a> {
416461
/// Decode the result of percent-decoding as UTF-8.
417462
///
418463
/// This is return `Err` when the percent-decoded bytes are not well-formed in UTF-8.
419-
#[cfg(feature = "std")]
420464
pub fn decode_utf8(self) -> Result<Cow<'a, str>, str::Utf8Error> {
421465
match self.clone().into() {
422466
Cow::Borrowed(bytes) => match str::from_utf8(bytes) {
@@ -434,13 +478,11 @@ impl<'a> PercentDecode<'a> {
434478
///
435479
/// Invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD,
436480
/// the replacement character.
437-
#[cfg(feature = "std")]
438481
pub fn decode_utf8_lossy(self) -> Cow<'a, str> {
439482
decode_utf8_lossy(self.clone().into())
440483
}
441484
}
442485

443-
#[cfg(feature = "std")]
444486
fn decode_utf8_lossy(input: Cow<'_, [u8]>) -> Cow<'_, str> {
445487
// Note: This function is duplicated in `form_urlencoded/src/query_encoding.rs`.
446488
match input {

0 commit comments

Comments
 (0)