#[repr(C)]pub struct TokenString { /* private fields */ }
Expand description
A string which can hold at most MAX_LENGTH
bytes (not Unicode scalar
values).
This holds valid UTF-8 encoded strings only.
Strings that are short enough, which need at most MAX_LENGTH_SMALL
bytes, are stored in the struct itself, bigger ones use the heap.
§Invariant
TokenString
must be a UTF-8 string (like &str
andalloc::string::String
).- The length of a
TokenString
is at mostMAX_LENGTH
and at least 0 - the empty string.
Implementations§
Source§impl TokenString
impl TokenString
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Return the length of the string in bytes.
This is the length of the string in bytes, not Unicode scalar values and not grapheme clusters.
Sourcepub const fn is_small(&self) -> bool
pub const fn is_small(&self) -> bool
Return true
if the string is a “small string”, that is, it is saved in
the TokenString
struct itself.
If this returns false
, the string is allocated on the heap.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Return true
, if this is the empty string.
Returns false
else.
Sourcepub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
Convert to a TokenString
.
bytes
must be valid UTF-8, use TokenString::try_from
if you are
not sure that it is valid. If the given byte slice is bigger than
MAX_LENGTH
, this panics.
Memory:
Allocates if and only if the length of bytes
is bigger than
MAX_LENGTH_SMALL
.
§Panics
Panics if bytes
is bigger than MAX_LENGTH
.
§Safety
bytes
must be valid UTF-8, if not, all bets are off - UB!
Sourcepub fn from_str_unchecked(s: &str) -> Self
pub fn from_str_unchecked(s: &str) -> Self
Convert to a TokenString
.
If the given string s
is bigger than MAX_LENGTH
, this panics. Use
TokenString::try_from
for a function that does not panic. The string
s
must be valid UTF-8 too, but it has already been UB if it isn’t.
Memory:
Allocates if and only if the length of s
is bigger than
MAX_LENGTH_SMALL
.
§Panics
Panics if s
is bigger than MAX_LENGTH
.
Sourcepub fn from_string_unchecked(s: &String) -> Self
pub fn from_string_unchecked(s: &String) -> Self
Convert to a TokenString
.
If the given string s
is bigger than MAX_LENGTH
, this panics. Use
TokenString::try_from
for a function that does not panic. The string
s
must be valid UTF-8 too, but it has already been UB if it isn’t.
Memory:
Allocates if and only if the length of s
is bigger than
MAX_LENGTH_SMALL
.
§Panics
Panics if s
is bigger than MAX_LENGTH
.
Sourcepub fn get(&self, idx: u16) -> Result<u8, TkStrError>
pub fn get(&self, idx: u16) -> Result<u8, TkStrError>
Return the byte at index idx
, check bounds.
Returns TkStrError::OutOfBounds
if the index is bigger than the
string’s length.
§Errors
TkStrError::OutOfBounds
if idx
is bigger than the string’s length.
Sourcepub fn get_unchecked(&self, idx: u16) -> u8
pub fn get_unchecked(&self, idx: u16) -> u8
Return the byte at index idx
, don’t check bounds.
Panics if the index is bigger than the string’s length.
§Panics
if idx
is bigger than the string’s length.
Sourcepub fn chars(&self) -> Chars<'_>
pub fn chars(&self) -> Chars<'_>
Return an iterator over the [char]
s of a string.
That is, an iterator over the Unicode scalar values of the
TokenString
.
Sourcepub fn iter(&self) -> TokenStringIter<'_> ⓘ
pub fn iter(&self) -> TokenStringIter<'_> ⓘ
Get a reference iterator.
Sourcepub const fn starts_ascii_uppercase(&self) -> bool
pub const fn starts_ascii_uppercase(&self) -> bool
Return true
, if the first byte is an uppercase ASCII character.
Sourcepub const fn starts_ascii_lowercase(&self) -> bool
pub const fn starts_ascii_lowercase(&self) -> bool
Return true
, if the first byte is an lowercase ASCII character.
Sourcepub fn starts_with(&self, needle: &Self) -> bool
pub fn starts_with(&self, needle: &Self) -> bool
Return true
, if the string starts with needle
.
Returns true
too if the string is needle
.
Sourcepub fn starts_with_bytes(&self, needle: &[u8]) -> bool
pub fn starts_with_bytes(&self, needle: &[u8]) -> bool
Return true
, if the string starts with needle
.
Returns true
too if the string is needle
.
Sourcepub fn starts_with_str(&self, needle: &str) -> bool
pub fn starts_with_str(&self, needle: &str) -> bool
Return true
, if the string starts with needle
.
Returns true
too if the string is needle
.
Sourcepub fn ends_with(&self, needle: &Self) -> bool
pub fn ends_with(&self, needle: &Self) -> bool
Return true
, if the string ends with needle
.
Returns true
too if the string is needle
.
Sourcepub fn ends_with_bytes(&self, needle: &[u8]) -> bool
pub fn ends_with_bytes(&self, needle: &[u8]) -> bool
Return true
, if the string ends with needle
.
Returns true
too if the string is needle
.
Sourcepub fn ends_with_str(&self, needle: &str) -> bool
pub fn ends_with_str(&self, needle: &str) -> bool
Return true
, if the string ends with needle
.
Returns true
too if the string is needle
.
Sourcepub fn to_ascii_lowercase(&self) -> Self
pub fn to_ascii_lowercase(&self) -> Self
Return a new string with all uppercase ASCII characters changed to lowercase.
Sourcepub fn to_ascii_uppercase(&self) -> Self
pub fn to_ascii_uppercase(&self) -> Self
Return a new string with all lowercase ASCII characters changed to uppercase.
Sourcepub fn trim_ascii(&self) -> Self
pub fn trim_ascii(&self) -> Self
Return a new string with all ASCII whitespace removed from the start and end.
Sourcepub fn trim_ascii_start(&self) -> Self
pub fn trim_ascii_start(&self) -> Self
Return a new string with all ASCII whitespace removed from the start.
Sourcepub fn trim_ascii_end(&self) -> Self
pub fn trim_ascii_end(&self) -> Self
Return a new string with all ASCII whitespace removed from the end.
Sourcepub fn strip_prefix<P: Pattern>(&self, prefix: P) -> Option<Self>
Available on pattern
only.
pub fn strip_prefix<P: Pattern>(&self, prefix: P) -> Option<Self>
pattern
only.Return a new string with prefix
removed from the start.
Sourcepub fn strip_suffix<P>(&self, suffix: P) -> Option<Self>
Available on pattern
only.
pub fn strip_suffix<P>(&self, suffix: P) -> Option<Self>
pattern
only.Return a new string with suffix
removed from the end.
Trait Implementations§
Source§impl AsRef<str> for TokenString
impl AsRef<str> for TokenString
Source§impl Borrow<str> for TokenString
impl Borrow<str> for TokenString
Source§impl Clone for TokenString
impl Clone for TokenString
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Return a clone of the TokenString
.
Memory:
Allocates if and only if the length of value
is bigger than
MAX_LENGTH_SMALL
.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a, const N: usize> Concat<&'a TokenString> for Result<&'a mut Builder<'a, N>, TkStrError>
impl<'a, const N: usize> Concat<&'a TokenString> for Result<&'a mut Builder<'a, N>, TkStrError>
Source§impl Debug for TokenString
impl Debug for TokenString
Source§impl Display for TokenString
impl Display for TokenString
Source§impl Drop for TokenString
impl Drop for TokenString
Source§impl Hash for TokenString
impl Hash for TokenString
Source§impl<Idx> Index<Idx> for TokenStringwhere
Idx: SliceIndex<str>,
impl<Idx> Index<Idx> for TokenStringwhere
Idx: SliceIndex<str>,
Source§impl<'a> IntoIterator for &'a TokenString
impl<'a> IntoIterator for &'a TokenString
Source§impl IntoIterator for TokenString
impl IntoIterator for TokenString
Source§impl Ord for TokenString
impl Ord for TokenString
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Compare two TokenString
s byte-wise.
This is not a sensible alphabetical comparison for anything that isn’t ASCII.
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq<[u8]> for TokenString
impl PartialEq<[u8]> for TokenString
Source§impl PartialEq<String> for TokenString
impl PartialEq<String> for TokenString
Source§impl PartialEq<str> for TokenString
impl PartialEq<str> for TokenString
Source§impl PartialEq for TokenString
impl PartialEq for TokenString
Source§impl PartialOrd for TokenString
impl PartialOrd for TokenString
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Compare two TokenString
s byte-wise.
This is not a sensible alphabetical comparison for anything that isn’t ASCII.
Source§impl TryFrom<&[char]> for TokenString
impl TryFrom<&[char]> for TokenString
Source§fn try_from(value: &[char]) -> Result<Self, Self::Error>
fn try_from(value: &[char]) -> Result<Self, Self::Error>
Try to create a TokenString
from the given slice.
Return TkStrError::TooBig
if the given slice is too big, greater
than MAX_LENGTH
.
Memory
Allocates and deallocates a temporary alloc::string::String
collecting the converted bytes.
Source§type Error = TkStrError
type Error = TkStrError
Source§impl TryFrom<&[u8]> for TokenString
impl TryFrom<&[u8]> for TokenString
Source§fn try_from(value: &[u8]) -> Result<Self, Self::Error>
fn try_from(value: &[u8]) -> Result<Self, Self::Error>
Try to create a TokenString
from the given slice.
Return TkStrError::TooBig
if the given slice is too big, greater
than MAX_LENGTH
.
Return TkStrError::UnicodeError
Memory:
Allocates if and only if the length of value
is bigger than
MAX_LENGTH_SMALL
.
Source§type Error = TkStrError
type Error = TkStrError
Source§impl TryFrom<&String> for TokenString
impl TryFrom<&String> for TokenString
Source§fn try_from(value: &String) -> Result<Self, Self::Error>
fn try_from(value: &String) -> Result<Self, Self::Error>
Create a TokenString
from a &alloc::string::String
.
Return TkStrError::TooBig
if the argument is greater than
MAX_LENGTH
.
Memory:
Allocates if and only if the length of value
is bigger than
MAX_LENGTH_SMALL
.
Source§type Error = TkStrError
type Error = TkStrError
Source§impl TryFrom<&str> for TokenString
impl TryFrom<&str> for TokenString
Source§fn try_from(value: &str) -> Result<Self, Self::Error>
fn try_from(value: &str) -> Result<Self, Self::Error>
Create a TokenString
from a &str
.
Return TkStrError::TooBig
if the argument is greater than
MAX_LENGTH
.
Memory:
Allocates if and only if the length of value
is bigger than
MAX_LENGTH_SMALL
.
Source§type Error = TkStrError
type Error = TkStrError
Source§impl TryFrom<String> for TokenString
impl TryFrom<String> for TokenString
Source§fn try_from(value: String) -> Result<Self, Self::Error>
fn try_from(value: String) -> Result<Self, Self::Error>
Create a TokenString
from a alloc::string::String
.
Return TkStrError::TooBig
if the argument is greater than
MAX_LENGTH
.
Memory:
Allocates if and only if the length of value
is bigger than
MAX_LENGTH_SMALL
.