Skip to content

Commit d530255

Browse files
authored
Merge pull request #691 from grego/master
Use collections from the alloc crate in percent_encoding
2 parents 89876ff + d4b4390 commit d530255

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

percent_encoding/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ license = "MIT/Apache-2.0"
88
edition = "2018"
99

1010
[features]
11-
default = ["std"]
12-
std = []
11+
default = ["alloc"]
12+
alloc = []

percent_encoding/src/lib.rs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@
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)]
41-
42-
#[cfg(not(feature = "std"))]
40+
#![no_std]
41+
#[cfg(feature = "alloc")]
42+
extern crate alloc;
43+
44+
#[cfg(feature = "alloc")]
45+
use alloc::{
46+
borrow::{Cow, ToOwned},
47+
string::String,
48+
vec::Vec,
49+
};
4350
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};
4851

4952
/// Represents a set of characters or bytes in the ASCII range.
5053
///
@@ -293,7 +296,7 @@ impl<'a> fmt::Display for PercentEncode<'a> {
293296
}
294297
}
295298

296-
#[cfg(feature = "std")]
299+
#[cfg(feature = "alloc")]
297300
impl<'a> From<PercentEncode<'a>> for Cow<'a, str> {
298301
fn from(mut iter: PercentEncode<'a>) -> Self {
299302
match iter.next() {
@@ -332,18 +335,13 @@ pub fn percent_decode_str(input: &str) -> PercentDecode<'_> {
332335
/// * Implements `Iterator<Item = u8>` and therefore has a `.collect::<Vec<u8>>()` method,
333336
/// * Has `decode_utf8()` and `decode_utf8_lossy()` methods.
334337
///
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-
)]
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+
/// ```
347345
#[inline]
348346
pub fn percent_decode(input: &[u8]) -> PercentDecode<'_> {
349347
PercentDecode {
@@ -384,7 +382,7 @@ impl<'a> Iterator for PercentDecode<'a> {
384382
}
385383
}
386384

387-
#[cfg(feature = "std")]
385+
#[cfg(feature = "alloc")]
388386
impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
389387
fn from(iter: PercentDecode<'a>) -> Self {
390388
match iter.if_any() {
@@ -396,7 +394,7 @@ impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
396394

397395
impl<'a> PercentDecode<'a> {
398396
/// If the percent-decoding is different from the input, return it as a new bytes vector.
399-
#[cfg(feature = "std")]
397+
#[cfg(feature = "alloc")]
400398
fn if_any(&self) -> Option<Vec<u8>> {
401399
let mut bytes_iter = self.bytes.clone();
402400
while bytes_iter.any(|&b| b == b'%') {
@@ -416,7 +414,7 @@ impl<'a> PercentDecode<'a> {
416414
/// Decode the result of percent-decoding as UTF-8.
417415
///
418416
/// This is return `Err` when the percent-decoded bytes are not well-formed in UTF-8.
419-
#[cfg(feature = "std")]
417+
#[cfg(feature = "alloc")]
420418
pub fn decode_utf8(self) -> Result<Cow<'a, str>, str::Utf8Error> {
421419
match self.clone().into() {
422420
Cow::Borrowed(bytes) => match str::from_utf8(bytes) {
@@ -434,13 +432,13 @@ impl<'a> PercentDecode<'a> {
434432
///
435433
/// Invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD,
436434
/// the replacement character.
437-
#[cfg(feature = "std")]
435+
#[cfg(feature = "alloc")]
438436
pub fn decode_utf8_lossy(self) -> Cow<'a, str> {
439437
decode_utf8_lossy(self.clone().into())
440438
}
441439
}
442440

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

0 commit comments

Comments
 (0)