pub struct PctString(_);
Expand description
Implementations§
source§impl PctString
impl PctString
sourcepub fn new<B: Into<Vec<u8>>>(
bytes: B
) -> Result<Self, InvalidPctString<Vec<u8>>>
pub fn new<B: Into<Vec<u8>>>( bytes: B ) -> Result<Self, InvalidPctString<Vec<u8>>>
Create a new owned percent-encoded string.
The input string is checked for correct percent-encoding.
If the test fails, a InvalidPctString
error is returned.
sourcepub fn from_string(string: String) -> Result<Self, InvalidPctString<String>>
pub fn from_string(string: String) -> Result<Self, InvalidPctString<String>>
Examples found in repository?
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(())
}
sourcepub unsafe fn new_unchecked<B: Into<Vec<u8>>>(bytes: B) -> Self
pub unsafe fn new_unchecked<B: Into<Vec<u8>>>(bytes: B) -> Self
Creates a new owned percent-encoded string without validation.
Safety
The input string must be correctly percent-encoded.
sourcepub fn encode<E: Encoder>(
src: impl Iterator<Item = char>,
encoder: E
) -> PctString
pub fn encode<E: Encoder>( src: impl Iterator<Item = char>, 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?
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!");
}
sourcepub fn as_pct_str(&self) -> &PctStr
pub fn as_pct_str(&self) -> &PctStr
Return this string as a borrowed percent-encoded string slice.
Examples found in repository?
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(())
}
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Return the internal string of the PctString
, consuming it
pub fn into_bytes(self) -> Vec<u8>
Methods from Deref<Target = PctStr>§
sourcepub fn len(&self) -> usize
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()
.
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the underlying percent-encoded string slice.
Examples found in repository?
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
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(())
}
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(())
}
sourcepub fn chars(&self) -> Chars<'_> ⓘ
pub fn chars(&self) -> Chars<'_> ⓘ
Iterate over the encoded characters of the string.
Examples found in repository?
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
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(())
}
sourcepub fn decode(&self) -> String
pub fn decode(&self) -> String
Decoding.
Return the string with the percent-encoded characters decoded.
Examples found in repository?
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
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 PartialEq<&str> for PctString
impl PartialEq<&str> for PctString
source§impl PartialEq<PctStr> for PctString
impl PartialEq<PctStr> for PctString
source§impl PartialEq<PctString> for PctStr
impl PartialEq<PctString> for PctStr
source§impl PartialEq<PctString> for PctString
impl PartialEq<PctString> for PctString
source§impl PartialOrd<PctStr> for PctString
impl PartialOrd<PctStr> for PctString
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<PctString> for PctStr
impl PartialOrd<PctString> for PctStr
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<PctString> for PctString
impl PartialOrd<PctString> for PctString
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more