pub struct IndexableStr<'a> { /* private fields */ }
Expand description
IndexableStr
is a struct
for creating immutable string objects that make text parsing with Rust a bit more elegant.
§Examples
// Gets a char from a specified index.
use indexable_str::IndexableStr;
let s = IndexableStr::new("0😀2345678😀");
assert_eq!(s[1], '😀');
// Gets a string slice from a specified range.
use indexable_str::IndexableStr;
let s = IndexableStr::new("0😀2345678😀");
assert_eq!(&s[1..9], "😀2345678")
// Parses a string of signed integers, which are separated by whitespace
use regex::Regex;
use indexable_str::IndexableStr;
let text = IndexableStr::new("0 1 2\n -11 -12 -13\n");
let signed_integer_pattern: Regex = Regex::new(r#"\b(0)|(-?[1-9]\d*)\b"#).unwrap();
let mut signed_integer_vec: Vec<i64> = Vec::new();
let mut cursor: usize = 0;
while cursor < text.len() {
let c = text[cursor];
match c {
' ' | '\t' | '\r' | '\n' => {
cursor += 1;
continue;
},
_=> (),
}
if let Some(captures) = signed_integer_pattern.captures(&text[cursor..]) {
let num_string = captures[0].to_string();
let num = num_string.parse::<i64>();
signed_integer_vec.push(num.unwrap());
cursor += num_string.len();
continue;
}
panic!("Unexpected character '{}' at position ({})!", c, cursor);
}
assert_eq!(signed_integer_vec.len(), 6);
assert_eq!(signed_integer_vec[0], 0);
assert_eq!(signed_integer_vec[1], 1);
assert_eq!(signed_integer_vec[2], 2);
assert_eq!(signed_integer_vec[3], -11);
assert_eq!(signed_integer_vec[4], -12);
assert_eq!(signed_integer_vec[5], -13);
IndexableStr
is designed to work well with all valid UTF-8 characters.
You should note that IndexableStr
creates a vector of objects that holds a char
and the starting byte offset of the char
’s position in the underlying string as a usize
. This requires additional memory resources. However, the convenience of IndexableStr
should outweigh the additional memory requirements for most applications.
Implementations§
Trait Implementations§
Source§impl<'a> Display for IndexableStr<'a>
impl<'a> Display for IndexableStr<'a>
Source§impl<'a> Index<Range<usize>> for IndexableStr<'a>
§Panics
- If the range end is greater than the number of characters in the string.
- If the range end is less than the range start.
impl<'a> Index<Range<usize>> for IndexableStr<'a>
§Panics
- If the range end is greater than the number of characters in the string.
- If the range end is less than the range start.
Source§impl<'a> Index<RangeFrom<usize>> for IndexableStr<'a>
§Panics
- If the range end is less than the range start.
impl<'a> Index<RangeFrom<usize>> for IndexableStr<'a>
§Panics
- If the range end is less than the range start.
Auto Trait Implementations§
impl<'a> Freeze for IndexableStr<'a>
impl<'a> RefUnwindSafe for IndexableStr<'a>
impl<'a> Send for IndexableStr<'a>
impl<'a> Sync for IndexableStr<'a>
impl<'a> Unpin for IndexableStr<'a>
impl<'a> UnwindSafe for IndexableStr<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more