pub struct cstr<const N: usize = 32> { /* private fields */ }
Expand description
This type is only available with the circular-str
option.
A circular string is represented underneath by a fixed-size u8
array arranged as a circular queue. The string can wrap around
either end and thus become internally non-contiguous.
This allows for efficient implementations of operations such as
push, trim in front of the string. However, Deref<str>
is not
implemented as it cannot be done efficiently. Instead, the
cstr::to_strs function returns a pair of string slices, the second
of which is non-empty if the string is not contiguous. Additionally,
only single-byte characters are currently allowed, although this might
change in the future by using a “ghost vector” at the end of the array.
An iterator cstr::chars is provided over all single-byte chars, which
also forms the foundation of other traits such as Eq, Ord, Hash, etc.
The Serialization (serde) and no-std options are both supported.
Each cstr<N>
can hold up to N bytes and the maximum N is 65535.
Values of N that are exact powers of 2 are recommended to speed up
the %
operation for computing indices in a ciruclar queue.
Examples:
let mut cb = cstr::<16>::make("abc123");
cb.push_str("xyz");
cb.push_front("9876");
assert_eq!(cb.pop_char().unwrap(), 'z');
assert_eq!(cb.pop_char_front().unwrap(), '9');
cb.push_str_front("000");
assert_eq!(cb.len(),14);
assert!(&cb == "000876abc123xy");
cb.truncate_left(10);
assert_eq!(&cb,"23xy");
cb.push_str("ijklmno ");
cb.push_char_front(' ');
assert!(!cb.is_contiguous());
cb.trim_whitespaces();
assert!("23xyijklmno" == &cb);
assert!(&cb < "4abc"); // Ord trait
let mut a = cstr8::from("abc");
let ba:cstr8 = "123" + a; // concat &str on the left efficiently
assert_eq!(ba,"123abc");
Implementations§
Source§impl<const N: usize> cstr<N>
impl<const N: usize> cstr<N>
Sourcepub fn make(src: &str) -> cstr<N>
pub fn make(src: &str) -> cstr<N>
create cstr
from &str
with silent truncation; panics if
N is greater than 65535
Sourcepub fn from_ascii(src: &str) -> cstr<N>
pub fn from_ascii(src: &str) -> cstr<N>
version of make that also panics if the input string is not ascii.
Sourcepub fn try_make(src: &str) -> Result<cstr<N>, &str>
pub fn try_make(src: &str) -> Result<cstr<N>, &str>
version of make that does not truncate: returns original str slice as error. Also checks if N is no greater than 65535 without panic.
Sourcepub fn try_make_ascii(src: &str) -> Option<cstr<N>>
pub fn try_make_ascii(src: &str) -> Option<cstr<N>>
version of try_make
that also checks if the input string is ascii.
Sourcepub fn make_remainder(src: &str) -> (cstr<N>, &str)
pub fn make_remainder(src: &str) -> (cstr<N>, &str)
version of make that returns a pair consisting of the made
cstr
and the remainder &str
that was truncated; panics if
N is greater than 65535 (but does not check for ascii strings)
Sourcepub fn from_pair(left: &str, right: &str) -> Option<cstr<N>>
pub fn from_pair(left: &str, right: &str) -> Option<cstr<N>>
make from a pair of str slices, does not truncate, and checks that N is not greater than 65535 without panic. The returned cstr will be contiguous.
Sourcepub const fn const_make(src: &str) -> cstr<N>
pub const fn const_make(src: &str) -> cstr<N>
const constructor, to be called from const contexts. However, as const constructors are restricted from using iterators, it’s slightly better to call the non-const constructors in non-const contexts. Truncates automatically.
Sourcepub const fn const_try_make(s: &str) -> Result<cstr<N>, &str>
pub const fn const_try_make(s: &str) -> Result<cstr<N>, &str>
version of const_make
that does not truncate.
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
checks if the underlying representation of the string is contiguous (without wraparound).
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
resets the internal representation of the cstr so that it is represented contiguously, without wraparound. Calling this function has O(n) cost both in terms of speed and memory as it requires a secondary buffer as well as copying.**
Sourcepub fn zero(&mut self)
pub fn zero(&mut self)
resets string to empty string and clears underlying buffer to contain all zeros.
Sourcepub fn make_contiguous(&mut self)
pub fn make_contiguous(&mut self)
guarantees a contiguous underlying representation of the string. This is a worst-case O(n) operation.
Sourcepub fn nth(&self, n: usize) -> Option<char>
pub fn nth(&self, n: usize) -> Option<char>
returns the nth char of the fstr. Since only single-byte characters are currently supported by the cstr type, this function is the same as Self::nth_bytechar except that n is checked against the length of the string.
Sourcepub const fn nth_bytechar(&self, n: usize) -> char
pub const fn nth_bytechar(&self, n: usize) -> char
returns the nth byte of the string as a char, does not check n against length of array
Sourcepub fn set(&mut self, n: usize, c: char) -> bool
pub fn set(&mut self, n: usize, c: char) -> bool
sets the nth byte of the string to the supplied character. the character must fit in a single byte. Returns true on success.
Sourcepub fn push_str<'t>(&mut self, src: &'t str) -> &'t str
pub fn push_str<'t>(&mut self, src: &'t str) -> &'t str
pushes given string to the end of the string, returns remainder
Sourcepub fn push_front<'t>(&mut self, src: &'t str) -> &'t str
pub fn push_front<'t>(&mut self, src: &'t str) -> &'t str
Pushes string to the front of the string, returns remainder. because of the circular-queue backing, this operation has the same cost as pushing to the back of the string (Self::push_str). This function does not check if the input string is ascii.
Sourcepub fn push_str_front<'t>(&mut self, src: &'t str) -> &'t str
pub fn push_str_front<'t>(&mut self, src: &'t str) -> &'t str
alias for Self::push_front
Sourcepub fn push_char(&mut self, c: char) -> bool
pub fn push_char(&mut self, c: char) -> bool
Pushes a single character to the end of the string, returning true on success. This function checks if the given character occupies a single-byte.
Sourcepub fn push_char_front(&mut self, c: char) -> bool
pub fn push_char_front(&mut self, c: char) -> bool
Pushes a single character to the front of the string, returning true on success. This function checks if the given character occupies a single-byte.
Sourcepub fn pop_char(&mut self) -> Option<char>
pub fn pop_char(&mut self) -> Option<char>
remove and return last character in string, if it exists
Sourcepub fn pop_char_front(&mut self) -> Option<char>
pub fn pop_char_front(&mut self) -> Option<char>
remove and return first character in string, if it exists
Sourcepub fn truncate_right(&mut self, n: usize)
pub fn truncate_right(&mut self, n: usize)
alias for Self::truncate
Sourcepub fn truncate(&mut self, n: usize)
pub fn truncate(&mut self, n: usize)
right-truncates string up to byte position n. Only the first n bytes will be kept. No effect if n is greater than or equal to the length of the string.
Sourcepub fn truncate_left(&mut self, n: usize)
pub fn truncate_left(&mut self, n: usize)
left-truncates string up to byte position n: that is, the first n bytes will be truncated. Because of the circular queue backing, this is an O(1) operation. No effect if n is greater than the length of the string.
Sourcepub fn truncate_front(&mut self, n: usize)
pub fn truncate_front(&mut self, n: usize)
alias for truncate_left
Sourcepub fn find<P>(&self, predicate: P) -> Option<usize>
pub fn find<P>(&self, predicate: P) -> Option<usize>
finds the position of first character that satisfies given predicate
Sourcepub fn rfind<P>(&self, predicate: P) -> Option<usize>
pub fn rfind<P>(&self, predicate: P) -> Option<usize>
finds the position of last character that satisfies given predicate
Sourcepub fn find_substr(&self, s: &str) -> Option<usize>
pub fn find_substr(&self, s: &str) -> Option<usize>
finds position of first matching substring
Sourcepub fn rfind_substr(&self, s: &str) -> Option<usize>
pub fn rfind_substr(&self, s: &str) -> Option<usize>
finds position of last matching substring
Sourcepub fn trim_right(&mut self)
pub fn trim_right(&mut self)
in-place trimming of white spaces at the end of the string
Sourcepub fn trim_whitespaces(&mut self)
pub fn trim_whitespaces(&mut self)
in-place trimming of white spaces at either end of the string
Sourcepub fn to_strs(&self) -> (&str, &str)
pub fn to_strs(&self) -> (&str, &str)
returns a pair of string slices (left,right)
which, when concatenated,
will yield an equivalent string underneath. In case of no wraparound,
the right str will be empty.
Sourcepub fn chars<'a>(&'a self) -> CircCharIter<'a> ⓘ
pub fn chars<'a>(&'a self) -> CircCharIter<'a> ⓘ
returns iterator over the characters of the string
Sourcepub fn iter<'a>(&'a self) -> CircCharIter<'a> ⓘ
pub fn iter<'a>(&'a self) -> CircCharIter<'a> ⓘ
alias for [Self.chars]
Sourcepub fn to_contiguous(&self) -> cstr<N>
pub fn to_contiguous(&self) -> cstr<N>
returns a copy of the same string that is contiguous underneath. This may call cstr::reset, which is an O(n) operation.
Sourcepub fn force_str(&self) -> &str
pub fn force_str(&self) -> &str
returns a single str slice if the cstr is contiguous underneath, otherwise panics.
Sourcepub fn substr(&self, start: usize, end: usize) -> cstr<N>
pub fn substr(&self, start: usize, end: usize) -> cstr<N>
returns a copy of the portion of the string. Will return empty string if indices are invalid. The returned string will be contiguous.
Sourcepub fn make_ascii_lowercase(&mut self)
pub fn make_ascii_lowercase(&mut self)
in-place modification of ascii characters to lower-case.
Sourcepub fn make_ascii_uppercase(&mut self)
pub fn make_ascii_uppercase(&mut self)
in-place modification of ascii characters to upper-case.
Sourcepub fn case_insensitive_eq<TA>(&self, other: TA) -> bool
pub fn case_insensitive_eq<TA>(&self, other: TA) -> bool
Tests for ascii case-insensitive equality with another string. This function does not check if the argument is ascii.
Sourcepub fn from_utf16(v: &[u16]) -> Result<Self, Self>
pub fn from_utf16(v: &[u16]) -> Result<Self, Self>
Decodes a UTF-16 encodeded slice. If a decoding error is encountered
or capacity exceeded, an Err(s)
is returned where s is the
the encoded string up to the point of the error. The returned
string will be contiguous.
Source§impl<const M: usize> cstr<M>
impl<const M: usize> cstr<M>
Sourcepub fn resize<const N: usize>(&self) -> cstr<N>
pub fn resize<const N: usize>(&self) -> cstr<N>
converts an cstr<M>
to an cstr<N>
. If the length of the string being
converted is greater than N, the extra characters are ignored.
This operation produces a new string that is contiguous underneath.
Sourcepub fn reallocate<const N: usize>(&self) -> Option<cstr<N>>
pub fn reallocate<const N: usize>(&self) -> Option<cstr<N>>
version of resize that does not allow string truncation due to length
Trait Implementations§
Source§impl<'de, const N: usize> Deserialize<'de> for cstr<N>
impl<'de, const N: usize> Deserialize<'de> for cstr<N>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<const N: usize> Hash for cstr<N>
Hashing is implemented character-by-character, starting with the
last char and ending with the first
impl<const N: usize> Hash for cstr<N>
Hashing is implemented character-by-character, starting with the last char and ending with the first
Source§impl<const N: usize> Ord for cstr<N>
impl<const N: usize> Ord for cstr<N>
Source§impl<const N: usize, const M: usize> PartialEq<cstr<M>> for cstr<N>
The implementation of this trait allows comparison between
circular strings of different capacity. This could affect the
type inference of the cstr::resize function.
impl<const N: usize, const M: usize> PartialEq<cstr<M>> for cstr<N>
The implementation of this trait allows comparison between circular strings of different capacity. This could affect the type inference of the cstr::resize function.