Struct ascii::AsciiStr [−][src]
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
impl AsciiStr
[src][−]
pub fn new<S: AsRef<AsciiStr> + ?Sized>(s: &S) -> &AsciiStr
[src][−]
Coerces into an AsciiStr
slice.
pub fn as_str(&self) -> &str
[src][−]
Converts &self
to a &str
slice.
pub fn as_bytes(&self) -> &[u8]
[src][−]
Converts &self
into a byte slice.
pub fn as_slice(&self) -> &[AsciiChar]
[src][−]
Returns the entire string as slice of AsciiChar
s.
pub fn as_mut_slice(&mut self) -> &mut [AsciiChar]
[src][−]
Returns the entire string as mutable slice of AsciiChar
s.
pub fn as_ptr(&self) -> *const AsciiChar
[src][−]
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.
pub fn as_mut_ptr(&mut self) -> *mut AsciiChar
[src][−]
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.
pub fn to_ascii_string(&self) -> AsciiString
[src][−]
Copies the content of this AsciiStr
into an owned AsciiString
.
pub fn from_ascii<B: ?Sized>(bytes: &B) -> Result<&AsciiStr, AsAsciiStrError> where
B: AsRef<[u8]>,
[src][−]
B: AsRef<[u8]>,
Converts anything that can represent a byte slice into an AsciiStr
.
Examples
let foo = AsciiStr::from_ascii("foo"); let err = AsciiStr::from_ascii("Ŋ"); assert_eq!(foo.unwrap().as_str(), "foo"); assert_eq!(err.unwrap_err().valid_up_to(), 0);
pub unsafe fn from_ascii_unchecked<B: ?Sized>(bytes: &B) -> &AsciiStr where
B: AsRef<[u8]>,
[src][−]
B: AsRef<[u8]>,
Converts anything that can be represented as a byte slice to an AsciiStr
without checking
for non-ASCII characters..
Examples
let foo = unsafe{ AsciiStr::from_ascii_unchecked("foo") }; assert_eq!(foo.as_str(), "foo");
pub fn len(&self) -> usize
[src][−]
Returns the number of characters / bytes in this ASCII sequence.
Examples
let s = AsciiStr::from_ascii("foo").unwrap(); assert_eq!(s.len(), 3);
pub fn is_empty(&self) -> bool
[src][−]
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());
pub fn chars(&self) -> Chars<'_>
[src][−]
Returns an iterator over the characters of the AsciiStr
.
pub fn chars_mut(&mut self) -> CharsMut<'_>
[src][−]
Returns an iterator over the characters of the AsciiStr
which allows you to modify the
value of each AsciiChar
.
pub fn split(&self, on: AsciiChar) -> Split<'_>
[src][−]
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"]);
pub fn lines(&self) -> Lines<'_>ⓘ
[src][−]
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.
pub fn trim(&self) -> &Self
[src][−]
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());
pub fn trim_left(&self) -> &Self
[src][−]
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_left());
pub fn trim_right(&self) -> &Self
[src][−]
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_right());
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
[src][−]
Compares two strings case-insensitively.
pub fn make_ascii_uppercase(&mut self)
[src][−]
Replaces lowercase letters with their uppercase equivalent.
pub fn make_ascii_lowercase(&mut self)
[src][−]
Replaces uppercase letters with their lowercase equivalent.
pub fn to_ascii_uppercase(&self) -> AsciiString
[src][−]
Returns a copy of this string where letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’.
pub fn to_ascii_lowercase(&self) -> AsciiString
[src][−]
Returns a copy of this string where letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’.
pub fn first(&self) -> Option<AsciiChar>
[src][−]
Returns the first character if the string is not empty.
pub fn last(&self) -> Option<AsciiChar>
[src][−]
Returns the last character if the string is not empty.