Struct pct_str::PctString

source ·
pub struct PctString { /* private fields */ }
Expand description

Owned, mutable percent-encoded string.

This is the equivalent of String for percent-encoded strings. It implements Deref to PctStr meaning that all methods on PctStr slices are available on PctString values as well.

Implementations§

source§

impl PctString

source

pub fn new<S: AsRef<str> + ?Sized>(str: &S) -> Result<PctString>

Create a new owned percent-encoded string.

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

Examples found in repository?
examples/string.rs (line 9)
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
fn main() -> pct_str::Result<()> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::new("Hello%20World%21")?;

	// 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")?);

	// 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 encode<I: Iterator<Item = char>, E: Encoder>(
    src: I,
    encoder: E
) -> PctString

Encode a string into a percent-encoded string.

This function takes an Encoder instance to decide which character of the string must be encoded.

Note that the character % will always be encoded regardless of the provided Encoder.

Example
use pct_str::{PctString, URIReserved};

let pct_string = PctString::encode("Hello World!".chars(), URIReserved);
println!("{}", pct_string.as_str()); // => Hello World%21
Examples found in repository?
examples/encode.rs (line 18)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() -> pct_str::Result<()> {
	// 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.
	println!("{}", 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());
	// => %48ello %57orld%21

	Ok(())
}
source

pub fn as_pct_str(&self) -> &PctStr

Return this string as a borrowed percent-encoded string slice.

Examples found in repository?
examples/string.rs (line 18)
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
fn main() -> pct_str::Result<()> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::new("Hello%20World%21")?;

	// 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")?);

	// 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 into_string(self) -> String

Return the internal string of the PctString, consuming it

Methods from Deref<Target = PctStr>§

source

pub fn len(&self) -> usize

Length of the string slice, in bytes.

Note that two percent-encoded strings with different lengths may represent the same string.

source

pub fn is_empty(&self) -> bool

Checks if the string is empty.

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
fn main() -> pct_str::Result<()> {
	// 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.
	println!("{}", 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());
	// => %48ello %57orld%21

	Ok(())
}
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() -> pct_str::Result<()> {
	// [`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
fn main() -> pct_str::Result<()> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::new("Hello%20World%21")?;

	// 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")?);

	// 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() -> pct_str::Result<()> {
	// [`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 22)
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
fn main() -> pct_str::Result<()> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::new("Hello%20World%21")?;

	// 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")?);

	// 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() -> pct_str::Result<()> {
	// [`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 28)
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
fn main() -> pct_str::Result<()> {
	// [`PctString`] is the equivalent of [`String`] for
	// percent-encoded strings.
	// The data is owned by `pct_string`.
	let pct_string = PctString::new("Hello%20World%21")?;

	// 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")?);

	// 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 Debug for PctString

source§

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

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

impl Deref for PctString

§

type Target = PctStr

The resulting type after dereferencing.
source§

fn deref(&self) -> &PctStr

Dereferences the value.
source§

impl Display for PctString

source§

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

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

impl FromStr for PctString

§

type Err = InvalidEncoding

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for PctString

source§

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

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

fn hash_slice<H>(data: &[Self], state: &mut H)where
    H: Hasher,
    Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<&str> for PctString

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 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<PctString> for PctString

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 PctString

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 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 PartialOrd<PctString> for PctString

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 TryFrom<&str> for PctString

§

type Error = InvalidEncoding

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

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

Performs the conversion.
source§

impl TryFrom<String> for PctString

§

type Error = InvalidEncoding

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

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for PctString

Auto Trait Implementations§

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.