Trait safe_string::IndexedStr
source · pub trait IndexedStr: Display + Debug + PartialEq<IndexedString> + for<'a> PartialEq<IndexedSlice<'a>> + Index<usize> {
// Required methods
fn as_str(&self) -> &str;
fn as_slice(&self) -> IndexedSlice<'_>;
fn len(&self) -> usize;
fn byte_len(&self) -> usize;
fn char_at(&self, index: usize) -> Option<char>;
fn slice<R: RangeBounds<usize>>(&self, range: R) -> IndexedSlice<'_>;
fn chars(&self) -> &[char];
fn to_indexed_string(&self) -> IndexedString;
// Provided methods
fn is_empty(&self) -> bool { ... }
fn to_lowercase(&self) -> IndexedString { ... }
fn to_uppercase(&self) -> IndexedString { ... }
}Expand description
A trait that facilitates safe interaction with strings that contain multi-byte characters.
IndexedString replaces String, whereas IndexedSlice replaces &str.
Both of these types as well as anything that implements IndexedStr are characterized by
the fact that their len() and indexing methods operate on characters, not bytes, and
enough information is stored to allow for O(1) slicing and indexing on a character and
byte basis as needed, but the default interface is character-centric.
This all comes at the cost of increased memory usage and some performance overhead when a
IndexedString is created, but the overhead is minimal when using IndexedSlice or
any other type implementing IndexedStr.
It is also worth noting that all of these types will never panic when indexing or slicing,
unlike &str and String, and clamped bounds are used instead.
Required Methods§
sourcefn as_str(&self) -> &str
fn as_str(&self) -> &str
Returns a &str representation of this IndexedStr.
WARNING: Once you cast to a &str, you are leaving the safety provided by
IndexedStr. Only use this method when you need to interface with code that requires
a &str.
sourcefn as_slice(&self) -> IndexedSlice<'_>
fn as_slice(&self) -> IndexedSlice<'_>
Returns a IndexedSlice that represents the entire contents of this IndexedStr.
sourcefn len(&self) -> usize
fn len(&self) -> usize
Returns the length of this IndexedStr in characters, NOT bytes.
sourcefn byte_len(&self) -> usize
fn byte_len(&self) -> usize
Returns the byte length of this IndexedStr. This will be longer than the
len if the string contains multi-byte characters.
sourcefn char_at(&self, index: usize) -> Option<char>
fn char_at(&self, index: usize) -> Option<char>
Returns the character at the given index, if it exists.
sourcefn slice<R: RangeBounds<usize>>(&self, range: R) -> IndexedSlice<'_>
fn slice<R: RangeBounds<usize>>(&self, range: R) -> IndexedSlice<'_>
Returns a sub-slice of this IndexedStr based on the given range in terms of the
characters in the string, not bytes.
The range is automatically clamped to the bounds of the IndexedStr.
sourcefn chars(&self) -> &[char]
fn chars(&self) -> &[char]
Returns a slice containing all characters of this IndexedStr in order.
sourcefn to_indexed_string(&self) -> IndexedString
fn to_indexed_string(&self) -> IndexedString
Converts this IndexedStr into an owned, dynamically allocated IndexedString.
Provided Methods§
sourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Returns true if this IndexedStr is empty (of length 0).
sourcefn to_lowercase(&self) -> IndexedString
fn to_lowercase(&self) -> IndexedString
Returns a new IndexedStr that is the lowercase version of this IndexedStr.
sourcefn to_uppercase(&self) -> IndexedString
fn to_uppercase(&self) -> IndexedString
Returns a new IndexedStr that is the uppercase version of this IndexedStr.