5 stable releases

1.2.0 Feb 2, 2023
1.1.0 Sep 10, 2020
1.0.2 Mar 16, 2020
1.0.1 Mar 14, 2020
1.0.0 Mar 10, 2020

#141 in Encoding

Download history 5572/week @ 2022-12-01 5730/week @ 2022-12-08 5906/week @ 2022-12-15 5200/week @ 2022-12-22 5045/week @ 2022-12-29 6092/week @ 2023-01-05 5099/week @ 2023-01-12 4829/week @ 2023-01-19 4941/week @ 2023-01-26 6212/week @ 2023-02-02 6706/week @ 2023-02-09 6945/week @ 2023-02-16 6823/week @ 2023-02-23 6498/week @ 2023-03-02 7987/week @ 2023-03-09 7615/week @ 2023-03-16

30,105 downloads per month
Used in 47 crates (6 directly)

MIT/Apache

26KB
593 lines

Percent-Encoded Strings

Documentation Crate informations Repository

This crate provides two types, PctStr and PctString, similar to str and String, representing percent-encoded strings used in URL, URI, IRI, etc. You can use them to encode, decode and compare percent-encoded strings.

Basic usage

You can parse/decode percent-encoded strings by building a PctStr slice over a str slice.

use pct_str::PctStr;

let pct_str = PctStr::new("Hello%20World%21")?;

assert!(pct_str == "Hello World!");

let decoded_string: String = pct_str.decode();
println!("{}", decoded_string); // => Hello World!

To create new percent-encoded strings, use the PctString to copy or encode new strings.

use pct_str::PctString;

// Copy the given percent-encoded string.
let pct_string = PctString::new("Hello%20World%21")?;

// Encode the given regular string.
let pct_string = PctString::encode("Hello World!".chars(), URIReserved);

println!("{}", pct_string.as_str()); // => Hello World%21

You can choose which character will be percent-encoded by the encode function by implementing the Encoder trait.

struct CustomEncoder;

impl pct_str::Encoder for CustomEncoder {
	fn encode(&self, c: char) -> bool {
		URIReserved.encode(c) || c.is_uppercase()
	}
}

let pct_string = PctString::encode("Hello World!".chars(), CustomEncoder);
println!("{}", pct_string.as_str()); // => %48ello %57orld%21

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~14KB