pub struct AsciiStr { /* private fields */ }
Expand description
AsciiStr
represents a byte or string slice that only contains ASCII characters.
It wraps an [AsciiChar]
and implements many of str
s methods and traits.
It can be created by a checked conversion from a str
or [u8]
, or borrowed from an
AsciiString
.
Implementations§
Source§impl AsciiStr
impl AsciiStr
Sourcepub fn as_mut_slice(&mut self) -> &mut [AsciiChar]
pub fn as_mut_slice(&mut self) -> &mut [AsciiChar]
Returns the entire string as mutable slice of AsciiChar
s.
Sourcepub const fn as_ptr(&self) -> *const AsciiChar
pub const fn as_ptr(&self) -> *const AsciiChar
Returns a raw pointer to the AsciiStr
’s buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it
will end up pointing to garbage. Modifying the AsciiStr
may cause it’s buffer to be
reallocated, which would also make any pointers to it invalid.
Sourcepub fn as_mut_ptr(&mut self) -> *mut AsciiChar
pub fn as_mut_ptr(&mut self) -> *mut AsciiChar
Returns an unsafe mutable pointer to the AsciiStr
’s buffer.
The caller must ensure that the slice outlives the pointer this function returns, or else it
will end up pointing to garbage. Modifying the AsciiStr
may cause it’s buffer to be
reallocated, which would also make any pointers to it invalid.
Sourcepub fn to_ascii_string(&self) -> AsciiString
pub fn to_ascii_string(&self) -> AsciiString
Copies the content of this AsciiStr
into an owned AsciiString
.
Sourcepub fn from_ascii<B>(bytes: &B) -> Result<&AsciiStr, AsAsciiStrError>
pub fn from_ascii<B>(bytes: &B) -> Result<&AsciiStr, AsAsciiStrError>
Converts anything that can represent a byte slice into an AsciiStr
.
§Errors
If bytes
contains a non-ascii byte, Err
will be returned
§Examples
let foo = AsciiStr::from_ascii(b"foo");
let err = AsciiStr::from_ascii("Ŋ");
assert_eq!(foo.unwrap().as_str(), "foo");
assert_eq!(err.unwrap_err().valid_up_to(), 0);
Sourcepub unsafe fn from_ascii_unchecked(bytes: &[u8]) -> &AsciiStr
pub unsafe fn from_ascii_unchecked(bytes: &[u8]) -> &AsciiStr
Converts anything that can be represented as a byte slice to an AsciiStr
without checking
for non-ASCII characters..
§Safety
If any of the bytes in bytes
do not represent valid ascii characters, calling
this function is undefined behavior.
§Examples
let foo = unsafe { AsciiStr::from_ascii_unchecked(&b"foo"[..]) };
assert_eq!(foo.as_str(), "foo");
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of characters / bytes in this ASCII sequence.
§Examples
let s = AsciiStr::from_ascii("foo").unwrap();
assert_eq!(s.len(), 3);
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the ASCII slice contains zero bytes.
§Examples
let mut empty = AsciiStr::from_ascii("").unwrap();
let mut full = AsciiStr::from_ascii("foo").unwrap();
assert!(empty.is_empty());
assert!(!full.is_empty());
Sourcepub fn chars_mut(&mut self) -> CharsMut<'_> ⓘ
pub fn chars_mut(&mut self) -> CharsMut<'_> ⓘ
Returns an iterator over the characters of the AsciiStr
which allows you to modify the
value of each AsciiChar
.
Sourcepub fn split(&self, on: AsciiChar) -> impl DoubleEndedIterator<Item = &AsciiStr>
pub fn split(&self, on: AsciiChar) -> impl DoubleEndedIterator<Item = &AsciiStr>
Returns an iterator over parts of the AsciiStr
separated by a character.
§Examples
let words = AsciiStr::from_ascii("apple banana lemon").unwrap()
.split(AsciiChar::Space)
.map(|a| a.as_str())
.collect::<Vec<_>>();
assert_eq!(words, ["apple", "banana", "lemon"]);
Sourcepub fn lines(&self) -> impl DoubleEndedIterator<Item = &AsciiStr>
pub fn lines(&self) -> impl DoubleEndedIterator<Item = &AsciiStr>
Returns an iterator over the lines of the AsciiStr
, which are themselves AsciiStr
s.
Lines are ended with either LineFeed
(\n
), or CarriageReturn
then LineFeed
(\r\n
).
The final line ending is optional.
Sourcepub fn trim(&self) -> &Self
pub fn trim(&self) -> &Self
Returns an ASCII string slice with leading and trailing whitespace removed.
§Examples
let example = AsciiStr::from_ascii(" \twhite \tspace \t").unwrap();
assert_eq!("white \tspace", example.trim());
Sourcepub fn trim_start(&self) -> &Self
pub fn trim_start(&self) -> &Self
Returns an ASCII string slice with leading whitespace removed.
§Examples
let example = AsciiStr::from_ascii(" \twhite \tspace \t").unwrap();
assert_eq!("white \tspace \t", example.trim_start());
Sourcepub fn trim_end(&self) -> &Self
pub fn trim_end(&self) -> &Self
Returns an ASCII string slice with trailing whitespace removed.
§Examples
let example = AsciiStr::from_ascii(" \twhite \tspace \t").unwrap();
assert_eq!(" \twhite \tspace", example.trim_end());
Sourcepub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
Compares two strings case-insensitively.
Sourcepub fn make_ascii_uppercase(&mut self)
pub fn make_ascii_uppercase(&mut self)
Replaces lowercase letters with their uppercase equivalent.
Sourcepub fn make_ascii_lowercase(&mut self)
pub fn make_ascii_lowercase(&mut self)
Replaces uppercase letters with their lowercase equivalent.
Sourcepub fn to_ascii_uppercase(&self) -> AsciiString
pub fn to_ascii_uppercase(&self) -> AsciiString
Returns a copy of this string where letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’.
Sourcepub fn to_ascii_lowercase(&self) -> AsciiString
pub fn to_ascii_lowercase(&self) -> AsciiString
Returns a copy of this string where letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’.
Sourcepub fn first(&self) -> Option<AsciiChar>
pub fn first(&self) -> Option<AsciiChar>
Returns the first character if the string is not empty.
Sourcepub fn into_ascii_string(self: Box<Self>) -> AsciiString
pub fn into_ascii_string(self: Box<Self>) -> AsciiString
Converts a Box<AsciiStr>
into a AsciiString
without copying or allocating.
Trait Implementations§
Source§impl<'a> Add<&'a AsciiStr> for AsciiString
impl<'a> Add<&'a AsciiStr> for AsciiString
Source§type Output = AsciiString
type Output = AsciiString
+
operator.Source§impl<'a> AddAssign<&'a AsciiStr> for AsciiString
impl<'a> AddAssign<&'a AsciiStr> for AsciiString
Source§fn add_assign(&mut self, other: &AsciiStr)
fn add_assign(&mut self, other: &AsciiStr)
+=
operation. Read moreSource§impl AsAsciiStr for AsciiStr
impl AsAsciiStr for AsciiStr
Source§fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
fn slice_ascii<R>(&self, range: R) -> Result<&AsciiStr, AsAsciiStrError>
Source§fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>
fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError>
Source§unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr
unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr
Source§impl AsMut<AsciiStr> for AsciiString
impl AsMut<AsciiStr> for AsciiString
Source§impl AsMutAsciiStr for AsciiStr
impl AsMutAsciiStr for AsciiStr
Source§fn slice_ascii_mut<R>(
&mut self,
range: R,
) -> Result<&mut AsciiStr, AsAsciiStrError>
fn slice_ascii_mut<R>( &mut self, range: R, ) -> Result<&mut AsciiStr, AsAsciiStrError>
Source§unsafe fn as_mut_ascii_str_unchecked(&mut self) -> &mut AsciiStr
unsafe fn as_mut_ascii_str_unchecked(&mut self) -> &mut AsciiStr
Source§fn as_mut_ascii_str(&mut self) -> Result<&mut AsciiStr, AsAsciiStrError>
fn as_mut_ascii_str(&mut self) -> Result<&mut AsciiStr, AsAsciiStrError>
Source§impl AsRef<AsciiStr> for AsciiString
impl AsRef<AsciiStr> for AsciiString
Source§impl Borrow<AsciiStr> for AsciiString
impl Borrow<AsciiStr> for AsciiString
Source§impl BorrowMut<AsciiStr> for AsciiString
impl BorrowMut<AsciiStr> for AsciiString
Source§fn borrow_mut(&mut self) -> &mut AsciiStr
fn borrow_mut(&mut self) -> &mut AsciiStr
Source§impl<'a> From<&'a AsciiStr> for AsciiString
impl<'a> From<&'a AsciiStr> for AsciiString
Source§impl From<AsciiString> for Box<AsciiStr>
impl From<AsciiString> for Box<AsciiStr>
Source§fn from(string: AsciiString) -> Self
fn from(string: AsciiString) -> Self
Source§impl<'a> IntoAsciiString for &'a AsciiStr
impl<'a> IntoAsciiString for &'a AsciiStr
Source§unsafe fn into_ascii_string_unchecked(self) -> AsciiString
unsafe fn into_ascii_string_unchecked(self) -> AsciiString
AsciiString
without checking for non-ASCII characters. Read moreSource§fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>
AsciiString
. Read moreSource§impl<'a> IntoIterator for &'a AsciiStr
impl<'a> IntoIterator for &'a AsciiStr
Produces references for compatibility with [u8]
.
(str
doesn’t implement IntoIterator
for its references,
so there is no compatibility to lose.)