pub struct AsciiString { /* private fields */ }
Expand description
A growable string stored as an ASCII encoded buffer.
Implementations§
Source§impl AsciiString
impl AsciiString
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty ASCII string buffer without allocating.
§Examples
let mut s = AsciiString::new();
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new ASCII string buffer with the given capacity.
The string will be able to hold exactly capacity
bytes without reallocating.
If capacity
is 0, the ASCII string will not allocate.
§Examples
let mut s = AsciiString::with_capacity(10);
Sourcepub unsafe fn from_raw_parts(
buf: *mut AsciiChar,
length: usize,
capacity: usize,
) -> Self
pub unsafe fn from_raw_parts( buf: *mut AsciiChar, length: usize, capacity: usize, ) -> Self
Creates a new AsciiString
from a length, capacity and pointer.
§Safety
This is highly unsafe, due to the number of invariants that aren’t checked:
- The memory at
ptr
need to have been previously allocated by the same allocator this library uses. length
needs to be less than or equal tocapacity
.capacity
needs to be the correct value.
Violating these may cause problems like corrupting the allocator’s internal datastructures.
§Examples
Basic usage:
use std::mem;
unsafe {
let s = AsciiString::from_ascii("hello").unwrap();
let ptr = s.as_ptr();
let len = s.len();
let capacity = s.capacity();
mem::forget(s);
let s = AsciiString::from_raw_parts(ptr as *mut _, len, capacity);
assert_eq!(AsciiString::from_ascii("hello").unwrap(), s);
}
Sourcepub unsafe fn from_ascii_unchecked<B>(bytes: B) -> Self
pub unsafe fn from_ascii_unchecked<B>(bytes: B) -> Self
Converts a vector of bytes to an AsciiString
without checking for non-ASCII characters.
§Safety
This function is unsafe because it does not check that the bytes passed to it are valid
ASCII characters. If this constraint is violated, it may cause memory unsafety issues with
future of the AsciiString
, as the rest of this library assumes that AsciiString
s are
ASCII encoded.
Sourcepub fn from_ascii<B>(bytes: B) -> Result<AsciiString, FromAsciiError<B>>
pub fn from_ascii<B>(bytes: B) -> Result<AsciiString, FromAsciiError<B>>
Converts anything that can represent a byte buffer into an AsciiString
.
§Failure
Returns the byte buffer if not all of the bytes are ASCII characters.
§Examples
let foo = AsciiString::from_ascii("foo".to_string()).unwrap();
let err = AsciiString::from_ascii("Ŋ".to_string()).unwrap_err();
assert_eq!(foo.as_str(), "foo");
assert_eq!(err.into_source(), "Ŋ");
Sourcepub fn push_str(&mut self, string: &AsciiStr)
pub fn push_str(&mut self, string: &AsciiStr)
Pushes the given ASCII string onto this ASCII string buffer.
§Examples
use std::str::FromStr;
let mut s = AsciiString::from_str("foo").unwrap();
s.push_str("bar".as_ascii_str().unwrap());
assert_eq!(s, "foobar".as_ascii_str().unwrap());
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of bytes that this ASCII string buffer can hold without reallocating.
§Examples
let s = String::with_capacity(10);
assert!(s.capacity() >= 10);
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more bytes to be inserted in the given
AsciiString
. The collection may reserve more space to avoid frequent reallocations.
§Panics
Panics if the new capacity overflows usize
.
§Examples
let mut s = AsciiString::new();
s.reserve(10);
assert!(s.capacity() >= 10);
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional
more bytes to be inserted in the
given AsciiString
. Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests. Therefore
capacity can not be relied upon to be precisely minimal. Prefer reserve
if future
insertions are expected.
§Panics
Panics if the new capacity overflows usize
.
§Examples
let mut s = AsciiString::new();
s.reserve_exact(10);
assert!(s.capacity() >= 10);
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of this ASCII string buffer to match it’s length.
§Examples
use std::str::FromStr;
let mut s = AsciiString::from_str("foo").unwrap();
s.reserve(100);
assert!(s.capacity() >= 100);
s.shrink_to_fit();
assert_eq!(s.capacity(), 3);
Sourcepub fn push(&mut self, ch: AsciiChar)
pub fn push(&mut self, ch: AsciiChar)
Adds the given ASCII character to the end of the ASCII string.
§Examples
let mut s = AsciiString::from_ascii("abc").unwrap();
s.push(AsciiChar::from('1').unwrap());
s.push(AsciiChar::from('2').unwrap());
s.push(AsciiChar::from('3').unwrap());
assert_eq!(s, "abc123");
Sourcepub fn pop(&mut self) -> Option<AsciiChar>
pub fn pop(&mut self) -> Option<AsciiChar>
Removes the last character from the ASCII string buffer and returns it.
Returns None
if this string buffer is empty.
§Examples
let mut s = AsciiString::from_ascii("foo").unwrap();
assert_eq!(s.pop().map(|c| c.as_char()), Some('o'));
assert_eq!(s.pop().map(|c| c.as_char()), Some('o'));
assert_eq!(s.pop().map(|c| c.as_char()), Some('f'));
assert_eq!(s.pop(), None);
Sourcepub fn remove(&mut self, idx: usize) -> AsciiChar
pub fn remove(&mut self, idx: usize) -> AsciiChar
Removes the ASCII character at position idx
from the buffer and returns it.
§Warning
This is an O(n) operation as it requires copying every element in the buffer.
§Panics
If idx
is out of bounds this function will panic.
§Examples
let mut s = AsciiString::from_ascii("foo").unwrap();
assert_eq!(s.remove(0).as_char(), 'f');
assert_eq!(s.remove(1).as_char(), 'o');
assert_eq!(s.remove(0).as_char(), 'o');
Sourcepub fn insert(&mut self, idx: usize, ch: AsciiChar)
pub fn insert(&mut self, idx: usize, ch: AsciiChar)
Inserts an ASCII character into the buffer at position idx
.
§Warning
This is an O(n) operation as it requires copying every element in the buffer.
§Panics
If idx
is out of bounds this function will panic.
§Examples
let mut s = AsciiString::from_ascii("foo").unwrap();
s.insert(2, AsciiChar::b);
assert_eq!(s, "fobo");
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of bytes in this ASCII string.
§Examples
let s = AsciiString::from_ascii("foo").unwrap();
assert_eq!(s.len(), 3);
Methods from Deref<Target = 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 fn as_ptr(&self) -> *const AsciiChar
pub 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 len(&self) -> usize
pub 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 fn is_empty(&self) -> bool
pub 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) -> Split<'_>
pub fn split(&self, on: AsciiChar) -> Split<'_>
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) -> Lines<'_> ⓘ
pub fn lines(&self) -> Lines<'_> ⓘ
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_left(&self) -> &Self
pub fn trim_left(&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_left());
Sourcepub fn trim_right(&self) -> &Self
pub fn trim_right(&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_right());
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’.
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 AsMut<[AsciiChar]> for AsciiString
impl AsMut<[AsciiChar]> for AsciiString
Source§impl AsMut<AsciiStr> for AsciiString
impl AsMut<AsciiStr> for AsciiString
Source§impl AsRef<[AsciiChar]> for AsciiString
impl AsRef<[AsciiChar]> for AsciiString
Source§impl AsRef<[u8]> for AsciiString
impl AsRef<[u8]> for AsciiString
Source§impl AsRef<AsciiStr> for AsciiString
impl AsRef<AsciiStr> for AsciiString
Source§impl AsRef<str> for AsciiString
impl AsRef<str> 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 Clone for AsciiString
impl Clone for AsciiString
Source§fn clone(&self) -> AsciiString
fn clone(&self) -> AsciiString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for AsciiString
impl Debug for AsciiString
Source§impl Default for AsciiString
impl Default for AsciiString
Source§fn default() -> AsciiString
fn default() -> AsciiString
Source§impl Deref for AsciiString
impl Deref for AsciiString
Source§impl DerefMut for AsciiString
impl DerefMut for AsciiString
Source§impl Display for AsciiString
impl Display for AsciiString
Source§impl<'a> Extend<&'a AsciiChar> for AsciiString
impl<'a> Extend<&'a AsciiChar> for AsciiString
Source§fn extend<I: IntoIterator<Item = &'a AsciiChar>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a AsciiChar>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a> Extend<&'a AsciiStr> for AsciiString
impl<'a> Extend<&'a AsciiStr> for AsciiString
Source§fn extend<I: IntoIterator<Item = &'a AsciiStr>>(&mut self, iterable: I)
fn extend<I: IntoIterator<Item = &'a AsciiStr>>(&mut self, iterable: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl Extend<AsciiChar> for AsciiString
impl Extend<AsciiChar> for AsciiString
Source§fn extend<I: IntoIterator<Item = AsciiChar>>(&mut self, iterable: I)
fn extend<I: IntoIterator<Item = AsciiChar>>(&mut self, iterable: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a> Extend<Cow<'a, AsciiStr>> for AsciiString
impl<'a> Extend<Cow<'a, AsciiStr>> for AsciiString
Source§fn extend<I: IntoIterator<Item = Cow<'a, AsciiStr>>>(&mut self, iterable: I)
fn extend<I: IntoIterator<Item = Cow<'a, AsciiStr>>>(&mut self, iterable: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<'a> From<&'a [AsciiChar]> for AsciiString
impl<'a> From<&'a [AsciiChar]> for AsciiString
Source§fn from(s: &'a [AsciiChar]) -> AsciiString
fn from(s: &'a [AsciiChar]) -> AsciiString
Source§impl<'a> From<&'a AsciiStr> for AsciiString
impl<'a> From<&'a AsciiStr> for AsciiString
Source§impl<'a> FromIterator<&'a AsciiStr> for AsciiString
impl<'a> FromIterator<&'a AsciiStr> for AsciiString
Source§fn from_iter<I: IntoIterator<Item = &'a AsciiStr>>(iter: I) -> AsciiString
fn from_iter<I: IntoIterator<Item = &'a AsciiStr>>(iter: I) -> AsciiString
Source§impl FromIterator<AsciiChar> for AsciiString
impl FromIterator<AsciiChar> for AsciiString
Source§fn from_iter<I: IntoIterator<Item = AsciiChar>>(iter: I) -> AsciiString
fn from_iter<I: IntoIterator<Item = AsciiChar>>(iter: I) -> AsciiString
Source§impl<'a> FromIterator<Cow<'a, AsciiStr>> for AsciiString
impl<'a> FromIterator<Cow<'a, AsciiStr>> for AsciiString
Source§fn from_iter<I: IntoIterator<Item = Cow<'a, AsciiStr>>>(iter: I) -> AsciiString
fn from_iter<I: IntoIterator<Item = Cow<'a, AsciiStr>>>(iter: I) -> AsciiString
Source§impl FromStr for AsciiString
impl FromStr for AsciiString
Source§type Err = AsAsciiStrError
type Err = AsAsciiStrError
Source§fn from_str(s: &str) -> Result<AsciiString, AsAsciiStrError>
fn from_str(s: &str) -> Result<AsciiString, AsAsciiStrError>
s
to return a value of this type. Read moreSource§impl Hash for AsciiString
impl Hash for AsciiString
Source§impl<T> Index<T> for AsciiString
impl<T> Index<T> for AsciiString
Source§impl<T> IndexMut<T> for AsciiString
impl<T> IndexMut<T> for AsciiString
Source§impl Into<String> for AsciiString
impl Into<String> for AsciiString
Source§impl IntoAsciiString for AsciiString
impl IntoAsciiString for AsciiString
Source§unsafe fn into_ascii_string_unchecked(self) -> AsciiString
unsafe fn into_ascii_string_unchecked(self) -> AsciiString
AsciiString
without checking for non-ASCII characters.Source§fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>
fn into_ascii_string(self) -> Result<AsciiString, FromAsciiError<Self>>
AsciiString
.Source§impl Ord for AsciiString
impl Ord for AsciiString
Source§fn cmp(&self, other: &AsciiString) -> Ordering
fn cmp(&self, other: &AsciiString) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<'a> PartialEq<&'a AsciiStr> for AsciiString
impl<'a> PartialEq<&'a AsciiStr> for AsciiString
Source§impl<'a> PartialEq<&'a str> for AsciiString
impl<'a> PartialEq<&'a str> for AsciiString
Source§impl<'a> PartialEq<AsciiString> for &'a AsciiStr
impl<'a> PartialEq<AsciiString> for &'a AsciiStr
Source§fn eq(&self, other: &AsciiString) -> bool
fn eq(&self, other: &AsciiString) -> bool
self
and other
values to be equal, and is used by ==
.Source§fn ne(&self, other: &AsciiString) -> bool
fn ne(&self, other: &AsciiString) -> bool
!=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.Source§impl<'a> PartialEq<AsciiString> for &'a str
impl<'a> PartialEq<AsciiString> for &'a str
Source§fn eq(&self, other: &AsciiString) -> bool
fn eq(&self, other: &AsciiString) -> bool
self
and other
values to be equal, and is used by ==
.Source§fn ne(&self, other: &AsciiString) -> bool
fn ne(&self, other: &AsciiString) -> bool
!=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.Source§impl<'a> PartialEq<AsciiString> for String
impl<'a> PartialEq<AsciiString> for String
Source§fn eq(&self, other: &AsciiString) -> bool
fn eq(&self, other: &AsciiString) -> bool
self
and other
values to be equal, and is used by ==
.Source§fn ne(&self, other: &AsciiString) -> bool
fn ne(&self, other: &AsciiString) -> bool
!=
. The default implementation is almost always sufficient,
and should not be overridden without very good reason.Source§impl PartialEq<AsciiString> for str
impl PartialEq<AsciiString> for str
Source§impl<'a> PartialEq<String> for AsciiString
impl<'a> PartialEq<String> for AsciiString
Source§impl PartialEq<str> for AsciiString
impl PartialEq<str> for AsciiString
Source§impl PartialEq for AsciiString
impl PartialEq for AsciiString
Source§impl PartialOrd for AsciiString
impl PartialOrd for AsciiString
Source§impl Write for AsciiString
Please note that the std::fmt::Result
returned by these methods does not support
transmission of an error other than that an error occurred.
impl Write for AsciiString
Please note that the std::fmt::Result
returned by these methods does not support
transmission of an error other than that an error occurred.