Struct fluent_uri::encoding::EString
source · pub struct EString<E: Encoder> { /* private fields */ }Expand description
A percent-encoded, growable string.
The borrowed counterpart of EString is EStr.
See its documentation for the meaning of the type parameter E.
§Comparison
EStrings are compared lexicographically
by their byte values. Normalization is not performed prior to comparison.
§Examples
Encode key-value pairs to a query string and use it to build a UriRef.
use fluent_uri::{
encoding::{
encoder::{Data, Query},
EStr, EString, Encoder, Table,
},
UriRef,
};
let pairs = [("name", "张三"), ("speech", "¡Olé!")];
let mut buf = EString::<Query>::new();
for (k, v) in pairs {
if !buf.is_empty() {
buf.push_byte(b'&');
}
// WARNING: Absolutely do not confuse data with delimiters! Use `Data`
// to encode data contained in a URI unless you know what you're doing!
//
// `Data` preserves only unreserved characters and encodes the others,
// which is always safe to use but may be wasteful of memory because
// usually not all reserved characters are used as delimiters and you can
// choose to preserve some of them. See below for an example of creating
// a custom encoder based on an existing one.
buf.encode::<Data>(k);
buf.push_byte(b'=');
buf.encode::<Data>(v);
}
assert_eq!(buf, "name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9%21");
let uri_ref = UriRef::builder()
.path(EStr::EMPTY)
.query(&buf)
.build()
.unwrap();
assert_eq!(uri_ref.as_str(), "?name=%E5%BC%A0%E4%B8%89&speech=%C2%A1Ol%C3%A9%21");Encode a path whose segments may contain the slash ('/') character
by using a custom encoder:
use fluent_uri::encoding::{encoder::Path, EString, Encoder, Table};
struct PathSegment;
impl Encoder for PathSegment {
const TABLE: &'static Table = &Path::TABLE.sub(&Table::gen(b"/"));
}
let mut path = EString::<Path>::new();
path.push_byte(b'/');
path.encode::<PathSegment>("foo/bar");
assert_eq!(path, "/foo%2Fbar");Implementations§
source§impl<E: Encoder> EString<E>
impl<E: Encoder> EString<E>
sourcepub fn encode<SubE: Encoder>(&mut self, s: &(impl AsRef<[u8]> + ?Sized))
pub fn encode<SubE: Encoder>(&mut self, s: &(impl AsRef<[u8]> + ?Sized))
Encodes a byte sequence with a sub-encoder and appends the result onto the end of this EString.
A byte will be percent-encoded if and only if SubE::TABLE does not allow it.
When encoding data, make sure that SubE::TABLE does not allow the component delimiters
that delimit the data.
Note that this method will not encode 0x20 (space) as U+002B (+).
§Panics
Panics at compile time if SubE is not a sub-encoder of E,
or if SubE::TABLE does not allow percent-encoded octets.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Consumes this EString and yields the underlying String.
Methods from Deref<Target = EStr<E>>§
pub const EMPTY: &'static Self = _
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);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(&self) -> Option<Split<'_, Path>>
pub fn segments(&self) -> Option<Split<'_, Path>>
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::UriRef;
// 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 = UriRef::parse("file:///path/to//dir/")?.path();
assert_eq!(path, "/path/to//dir/");
assert!(path.segments().into_iter().flatten().eq(["path", "to", "", "dir", ""]));
let path = UriRef::parse("foo:bar/baz")?.path();
assert_eq!(path, "bar/baz");
assert!(path.segments().is_none());
let path = UriRef::parse("http://example.com")?.path();
assert!(path.is_empty());
assert!(path.segments().is_none());Trait Implementations§
source§impl<E: Encoder> Ord for EString<E>
impl<E: Encoder> Ord for EString<E>
source§impl<E: Encoder> PartialOrd for EString<E>
impl<E: Encoder> PartialOrd for EString<E>
impl<E: Encoder> Eq for EString<E>
Auto Trait Implementations§
impl<E> Freeze for EString<E>
impl<E> RefUnwindSafe for EString<E>where
E: RefUnwindSafe,
impl<E> Send for EString<E>where
E: Send,
impl<E> Sync for EString<E>where
E: Sync,
impl<E> Unpin for EString<E>where
E: Unpin,
impl<E> UnwindSafe for EString<E>where
E: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)