pub struct EStr<E: Encoder> { /* private fields */ }
Expand description
Percent-encoded string slices.
The owned counterpart of EStr
is EString
. See its documentation
if you want to build a percent-encoded string from scratch.
§Type parameter
The EStr<E>
type is parameterized over a type E
that implements Encoder
.
The associated constant E::TABLE
of type Table
specifies the byte patterns
allowed in a string. In short, the underlying byte sequence of an EStr<E>
slice
can be formed by joining any number of the following byte sequences:
ch.encode_utf8(&mut [0; 4])
whereE::TABLE.allows(ch)
.[b'%', hi, lo]
whereE::TABLE.allows_pct_encoded() && hi.is_ascii_hexdigit() && lo.is_ascii_hexdigit()
.
§Comparison
EStr
slices are compared lexicographically
by their byte values. Normalization is not performed prior to comparison.
§Examples
Parse key-value pairs from a query string into a hash map:
use fluent_uri::{encoding::EStr, UriRef};
use std::collections::HashMap;
let s = "?name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9%21";
let query = UriRef::parse(s)?.query().unwrap();
let map: HashMap<_, _> = query
.split('&')
.map(|s| s.split_once('=').unwrap_or((s, EStr::EMPTY)))
.map(|(k, v)| (k.decode().into_string_lossy(), v.decode().into_string_lossy()))
.collect();
assert_eq!(map["name"], "张三");
assert_eq!(map["speech"], "¡Olé!");
Implementations§
source§impl<E: Encoder> EStr<E>
impl<E: Encoder> EStr<E>
sourcepub const fn new_or_panic(s: &str) -> &Self
pub const fn new_or_panic(s: &str) -> &Self
sourcepub const fn new(s: &str) -> Option<&Self>
pub const fn new(s: &str) -> Option<&Self>
Converts a string slice to an EStr
slice, returning None
if the conversion fails.
sourcepub fn decode(&self) -> Decode<'_>
pub fn decode(&self) -> Decode<'_>
Decodes the EStr
slice.
Always split before decoding, as otherwise the data may be mistaken for component delimiters.
This method allocates only when the slice contains any percent-encoded octet.
Note that this method will not decode U+002B
(+) as 0x20
(space).
§Panics
Panics at compile time if E::TABLE
does not allow percent-encoded octets.
§Examples
use fluent_uri::encoding::{encoder::Path, EStr};
let dec = EStr::<Path>::new_or_panic("%C2%A1Hola%21").decode();
assert_eq!(dec.as_bytes(), &[0xc2, 0xa1, 0x48, 0x6f, 0x6c, 0x61, 0x21]);
assert_eq!(dec.into_string().unwrap(), "¡Hola!");
sourcepub fn split(&self, delim: char) -> Split<'_, E> ⓘ
pub fn split(&self, delim: char) -> Split<'_, E> ⓘ
Returns an iterator over subslices of the EStr
slice separated by the given delimiter.
§Panics
Panics if the delimiter is not a reserved character.
§Examples
use fluent_uri::encoding::{encoder::Path, EStr};
assert!(EStr::<Path>::new_or_panic("a,b,c").split(',').eq(["a", "b", "c"]));
assert!(EStr::<Path>::new_or_panic(",").split(',').eq(["", ""]));
assert!(EStr::<Path>::EMPTY.split(',').eq([""]));
sourcepub fn split_once(&self, delim: char) -> Option<(&Self, &Self)>
pub fn split_once(&self, delim: char) -> Option<(&Self, &Self)>
Splits the EStr
slice on the first occurrence of the given delimiter and
returns prefix before delimiter and suffix after delimiter.
Returns None
if the delimiter is not found.
§Panics
Panics if the delimiter is not a reserved character.
§Examples
use fluent_uri::encoding::{encoder::Path, EStr};
assert_eq!(
EStr::<Path>::new_or_panic("foo;bar;baz").split_once(';'),
Some((EStr::new_or_panic("foo"), EStr::new_or_panic("bar;baz")))
);
assert_eq!(EStr::<Path>::new_or_panic("foo").split_once(';'), None);
sourcepub fn rsplit_once(&self, delim: char) -> Option<(&Self, &Self)>
pub fn rsplit_once(&self, delim: char) -> Option<(&Self, &Self)>
Splits the EStr
slice on the last occurrence of the given delimiter and
returns prefix before delimiter and suffix after delimiter.
Returns None
if the delimiter is not found.
§Panics
Panics if the delimiter is not a reserved character.
§Examples
use fluent_uri::encoding::{encoder::Path, EStr};
assert_eq!(
EStr::<Path>::new_or_panic("foo;bar;baz").rsplit_once(';'),
Some((EStr::new_or_panic("foo;bar"), EStr::new_or_panic("baz")))
);
assert_eq!(EStr::<Path>::new_or_panic("foo").rsplit_once(';'), None);
source§impl<E: PathEncoder> EStr<E>
impl<E: PathEncoder> EStr<E>
Extension methods for the path component.
sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Checks whether the path is absolute, i.e., starting with '/'
.
sourcepub fn is_rootless(&self) -> bool
pub fn is_rootless(&self) -> bool
Checks whether the path is rootless, i.e., not starting with '/'
.
sourcepub fn segments_if_absolute(&self) -> Option<Split<'_, E>>
pub fn segments_if_absolute(&self) -> Option<Split<'_, E>>
Returns an iterator over the path segments, separated by '/'
.
Returns None
if the path is rootless. Use split
instead if you need to split a rootless path on occurrences of '/'
.
Note that the path can be empty when authority is present,
in which case this method will return None
.
§Examples
use fluent_uri::Uri;
// Segments are separated by '/'.
// The empty string before a leading '/' is not a segment.
// However, segments can be empty in the other cases.
let path = Uri::parse("file:///path/to//dir/")?.path();
assert_eq!(path, "/path/to//dir/");
assert!(path.segments_if_absolute().unwrap().eq(["path", "to", "", "dir", ""]));
let path = Uri::parse("foo:bar/baz")?.path();
assert_eq!(path, "bar/baz");
assert!(path.segments_if_absolute().is_none());
let path = Uri::parse("http://example.com")?.path();
assert!(path.is_empty());
assert!(path.segments_if_absolute().is_none());