37
37
//! assert_eq!(utf8_percent_encode("foo <bar>", FRAGMENT).to_string(), "foo%20%3Cbar%3E");
38
38
//! ```
39
39
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
+ } ;
43
50
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} ;
48
51
49
52
/// Represents a set of characters or bytes in the ASCII range.
50
53
///
@@ -293,7 +296,7 @@ impl<'a> fmt::Display for PercentEncode<'a> {
293
296
}
294
297
}
295
298
296
- #[ cfg( feature = "std " ) ]
299
+ #[ cfg( feature = "alloc " ) ]
297
300
impl < ' a > From < PercentEncode < ' a > > for Cow < ' a , str > {
298
301
fn from ( mut iter : PercentEncode < ' a > ) -> Self {
299
302
match iter. next ( ) {
@@ -332,18 +335,13 @@ pub fn percent_decode_str(input: &str) -> PercentDecode<'_> {
332
335
/// * Implements `Iterator<Item = u8>` and therefore has a `.collect::<Vec<u8>>()` method,
333
336
/// * Has `decode_utf8()` and `decode_utf8_lossy()` methods.
334
337
///
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
+ /// ```
347
345
#[ inline]
348
346
pub fn percent_decode ( input : & [ u8 ] ) -> PercentDecode < ' _ > {
349
347
PercentDecode {
@@ -384,7 +382,7 @@ impl<'a> Iterator for PercentDecode<'a> {
384
382
}
385
383
}
386
384
387
- #[ cfg( feature = "std " ) ]
385
+ #[ cfg( feature = "alloc " ) ]
388
386
impl < ' a > From < PercentDecode < ' a > > for Cow < ' a , [ u8 ] > {
389
387
fn from ( iter : PercentDecode < ' a > ) -> Self {
390
388
match iter. if_any ( ) {
@@ -396,7 +394,7 @@ impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]> {
396
394
397
395
impl < ' a > PercentDecode < ' a > {
398
396
/// If the percent-decoding is different from the input, return it as a new bytes vector.
399
- #[ cfg( feature = "std " ) ]
397
+ #[ cfg( feature = "alloc " ) ]
400
398
fn if_any ( & self ) -> Option < Vec < u8 > > {
401
399
let mut bytes_iter = self . bytes . clone ( ) ;
402
400
while bytes_iter. any ( |& b| b == b'%' ) {
@@ -416,7 +414,7 @@ impl<'a> PercentDecode<'a> {
416
414
/// Decode the result of percent-decoding as UTF-8.
417
415
///
418
416
/// This is return `Err` when the percent-decoded bytes are not well-formed in UTF-8.
419
- #[ cfg( feature = "std " ) ]
417
+ #[ cfg( feature = "alloc " ) ]
420
418
pub fn decode_utf8 ( self ) -> Result < Cow < ' a , str > , str:: Utf8Error > {
421
419
match self . clone ( ) . into ( ) {
422
420
Cow :: Borrowed ( bytes) => match str:: from_utf8 ( bytes) {
@@ -434,13 +432,13 @@ impl<'a> PercentDecode<'a> {
434
432
///
435
433
/// Invalid UTF-8 percent-encoded byte sequences will be replaced � U+FFFD,
436
434
/// the replacement character.
437
- #[ cfg( feature = "std " ) ]
435
+ #[ cfg( feature = "alloc " ) ]
438
436
pub fn decode_utf8_lossy ( self ) -> Cow < ' a , str > {
439
437
decode_utf8_lossy ( self . clone ( ) . into ( ) )
440
438
}
441
439
}
442
440
443
- #[ cfg( feature = "std " ) ]
441
+ #[ cfg( feature = "alloc " ) ]
444
442
fn decode_utf8_lossy ( input : Cow < ' _ , [ u8 ] > ) -> Cow < ' _ , str > {
445
443
// Note: This function is duplicated in `form_urlencoded/src/query_encoding.rs`.
446
444
match input {
0 commit comments