Struct pct_str::PctStr

source ·
pub struct PctStr(_);
Expand description

Percent-Encoded string slice.

This is the equivalent of str for percent-encoded strings. This is an unsized type, meaning that it must always be used behind a pointer like & or Box. For an owned version of this type, see PctString.

Examples

use pct_str::PctStr;

let buffer = "Hello%20World%21";
let pct_str = PctStr::new(buffer).unwrap();

// You can compare percent-encoded strings with a regular string.
assert!(pct_str == "Hello World!");

// The underlying string is unchanged.
assert!(pct_str.as_str() == "Hello%20World%21");

// Just as a regular string, you can iterate over the
// encoded characters of `pct_str` with [`PctStr::chars`].
for c in pct_str.chars() {
  print!("{}", c);
}

// You can decode the string and every remove percent-encoded characters
// with the [`PctStr::decode`] method.
let decoded_string: String = pct_str.decode();
println!("{}", decoded_string);

Implementations§

source§

impl PctStr

source

pub fn new<S: AsRef<[u8]> + ?Sized>( input: &S ) -> Result<&PctStr, InvalidPctString<&S>>

Create a new percent-encoded string slice.

The input slice is checked for correct percent-encoding. If the test fails, a [InvalidEncoding] error is returned.

Examples found in repository?
examples/str.rs (line 10)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() -> Result<(), InvalidPctString<&'static str>> {
	// [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
	let buffer = "Hello%20World%21";
	// It is just a reference to `buffer`.
	// It can fail if `buffer` is not a valid percent-encoded string.
	let pct_str = PctStr::new(buffer)?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_str == "Hello World!"); // => true

	// The underlying string is unchanged.
	assert!(pct_str.as_str() == "Hello%20World%21"); // => true

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctStr::chars`].
	for c in pct_str.chars() {
		print!("{}", c);
	}
	// => Hello World!

	println!("");

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_str.decode();
	println!("{}", decoded_string);
	// => Hello World!

	Ok(())
}
More examples
Hide additional examples
examples/string.rs (line 20)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main() -> Result<(), InvalidPctString<String>> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_string == "Hello World!");

	// The underlying string is percent-encoded.
	assert!(pct_string.as_str() == "Hello%20World%21");

	// You can get a reference to the string as a [`PctStr`].
	assert!(
		pct_string.as_pct_str()
			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
	);

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctString::chars`].
	for c in pct_string.chars() {
		println!("{}", c);
	}

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_string.decode();
	println!("{}", decoded_string);

	Ok(())
}
source

pub unsafe fn new_unchecked<S: AsRef<[u8]> + ?Sized>(input: &S) -> &PctStr

Create a new percent-encoded string slice without checking for correct encoding.

This is an unsafe function. The resulting string slice will have an undefined behaviour if the input slice is not percent-encoded.

Safety

The input str must be a valid percent-encoded string.

source

pub fn validate(input: impl Iterator<Item = u8>) -> bool

Checks that the given iterator produces a valid percent-encoded string.

source

pub fn len(&self) -> usize

Length of the decoded string (character count).

Computed in linear time. This is different from the byte length, which can be retrieved using value.as_bytes().len().

source

pub fn is_empty(&self) -> bool

Checks if the string is empty.

source

pub fn as_bytes(&self) -> &[u8]

Returns the underlying percent-encoding bytes.

source

pub fn as_str(&self) -> &str

Get the underlying percent-encoded string slice.

Examples found in repository?
examples/encode.rs (line 20)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
	// You can encode any string into a percent-encoded string
	// using the [`PctString::encode`] function.
	// It takes a `char` iterator and a [`Encoder`] instance deciding which
	// characters to encode.
	let pct_string = PctString::encode("Hello World!".chars(), URIReserved);
	// [`URIReserved`] is a predefined encoder for URI-reserved characters.
	assert_eq!(pct_string.as_str(), "Hello World%21");

	// You can create your own encoder by implementing the [`Encoder`] trait.
	let pct_string = PctString::encode("Hello World!".chars(), CustomEncoder);
	println!("{}", pct_string.as_str());
	assert_eq!(pct_string.as_str(), "%48ello %57orld%21");

	// You can also use any function implementing `Fn(char) -> bool`.
	let pct_string = PctString::encode("Hello World!".chars(), char::is_uppercase);
	assert_eq!(pct_string.as_str(), "%48ello %57orld!");
}
More examples
Hide additional examples
examples/str.rs (line 16)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() -> Result<(), InvalidPctString<&'static str>> {
	// [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
	let buffer = "Hello%20World%21";
	// It is just a reference to `buffer`.
	// It can fail if `buffer` is not a valid percent-encoded string.
	let pct_str = PctStr::new(buffer)?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_str == "Hello World!"); // => true

	// The underlying string is unchanged.
	assert!(pct_str.as_str() == "Hello%20World%21"); // => true

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctStr::chars`].
	for c in pct_str.chars() {
		print!("{}", c);
	}
	// => Hello World!

	println!("");

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_str.decode();
	println!("{}", decoded_string);
	// => Hello World!

	Ok(())
}
examples/string.rs (line 15)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main() -> Result<(), InvalidPctString<String>> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_string == "Hello World!");

	// The underlying string is percent-encoded.
	assert!(pct_string.as_str() == "Hello%20World%21");

	// You can get a reference to the string as a [`PctStr`].
	assert!(
		pct_string.as_pct_str()
			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
	);

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctString::chars`].
	for c in pct_string.chars() {
		println!("{}", c);
	}

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_string.decode();
	println!("{}", decoded_string);

	Ok(())
}
source

pub fn chars(&self) -> Chars<'_>

Iterate over the encoded characters of the string.

Examples found in repository?
examples/str.rs (line 20)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() -> Result<(), InvalidPctString<&'static str>> {
	// [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
	let buffer = "Hello%20World%21";
	// It is just a reference to `buffer`.
	// It can fail if `buffer` is not a valid percent-encoded string.
	let pct_str = PctStr::new(buffer)?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_str == "Hello World!"); // => true

	// The underlying string is unchanged.
	assert!(pct_str.as_str() == "Hello%20World%21"); // => true

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctStr::chars`].
	for c in pct_str.chars() {
		print!("{}", c);
	}
	// => Hello World!

	println!("");

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_str.decode();
	println!("{}", decoded_string);
	// => Hello World!

	Ok(())
}
More examples
Hide additional examples
examples/string.rs (line 25)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main() -> Result<(), InvalidPctString<String>> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_string == "Hello World!");

	// The underlying string is percent-encoded.
	assert!(pct_string.as_str() == "Hello%20World%21");

	// You can get a reference to the string as a [`PctStr`].
	assert!(
		pct_string.as_pct_str()
			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
	);

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctString::chars`].
	for c in pct_string.chars() {
		println!("{}", c);
	}

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_string.decode();
	println!("{}", decoded_string);

	Ok(())
}
source

pub fn bytes(&self) -> Bytes<'_>

Iterate over the encoded bytes of the string.

source

pub fn decode(&self) -> String

Decoding.

Return the string with the percent-encoded characters decoded.

Examples found in repository?
examples/str.rs (line 29)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() -> Result<(), InvalidPctString<&'static str>> {
	// [`PctStr`] is the equivalent of [`str`] for percent-encoded strings.
	let buffer = "Hello%20World%21";
	// It is just a reference to `buffer`.
	// It can fail if `buffer` is not a valid percent-encoded string.
	let pct_str = PctStr::new(buffer)?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_str == "Hello World!"); // => true

	// The underlying string is unchanged.
	assert!(pct_str.as_str() == "Hello%20World%21"); // => true

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctStr::chars`].
	for c in pct_str.chars() {
		print!("{}", c);
	}
	// => Hello World!

	println!("");

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_str.decode();
	println!("{}", decoded_string);
	// => Hello World!

	Ok(())
}
More examples
Hide additional examples
examples/string.rs (line 31)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
fn main() -> Result<(), InvalidPctString<String>> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::from_string("Hello%20World%21".to_string())?;

	// You can compare percent-encoded strings with a regular string.
	assert!(pct_string == "Hello World!");

	// The underlying string is percent-encoded.
	assert!(pct_string.as_str() == "Hello%20World%21");

	// You can get a reference to the string as a [`PctStr`].
	assert!(
		pct_string.as_pct_str()
			== PctStr::new("Hello%20World%21").map_err(InvalidPctString::into_owned)?
	);

	// Just as a regular string, you can iterate over the
	// encoded characters of `pct_str` with [`PctString::chars`].
	for c in pct_string.chars() {
		println!("{}", c);
	}

	// You can decode the string and every remove percent-encoded characters
	// with the [`PctStr::decode`] method.
	let decoded_string: String = pct_string.decode();
	println!("{}", decoded_string);

	Ok(())
}

Trait Implementations§

source§

impl AsRef<[u8]> for PctStr

source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<PctStr> for PctString

source§

fn as_ref(&self) -> &PctStr

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl AsRef<str> for PctStr

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Borrow<[u8]> for PctStr

source§

fn borrow(&self) -> &[u8]

Immutably borrows from an owned value. Read more
source§

impl Borrow<PctStr> for PctString

source§

fn borrow(&self) -> &PctStr

Immutably borrows from an owned value. Read more
source§

impl Borrow<str> for PctStr

source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
source§

impl Debug for PctStr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for PctStr

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for PctStr

source§

fn hash<H: Hasher>(&self, hasher: &mut H)

Feeds this value into the given Hasher. Read more
source§

impl Ord for PctStr

source§

fn cmp(&self, other: &PctStr) -> Ordering

This method returns an Ordering between self and other. Read more
source§

impl PartialEq<PctStr> for PctStr

source§

fn eq(&self, other: &PctStr) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<PctStr> for PctString

source§

fn eq(&self, other: &PctStr) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<PctString> for PctStr

source§

fn eq(&self, other: &PctString) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<str> for PctStr

source§

fn eq(&self, other: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<PctStr> for PctStr

source§

fn partial_cmp(&self, other: &PctStr) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<PctStr> for PctString

source§

fn partial_cmp(&self, other: &PctStr) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<PctString> for PctStr

source§

fn partial_cmp(&self, other: &PctString) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl ToOwned for PctStr

§

type Owned = PctString

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> Self::Owned

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · source§

fn clone_into(&self, target: &mut Self::Owned)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<'a> TryFrom<&'a str> for &'a PctStr

§

type Error = InvalidPctString<&'a str>

The type returned in the event of a conversion error.
source§

fn try_from(value: &'a str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for PctStr

Auto Trait Implementations§

§

impl RefUnwindSafe for PctStr

§

impl Send for PctStr

§

impl !Sized for PctStr

§

impl Sync for PctStr

§

impl Unpin for PctStr

§

impl UnwindSafe for PctStr

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more