Struct IndexableStr

Source
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§

Source§

impl<'a> IndexableStr<'a>

Source

pub fn new(str: &'a str) -> IndexableStr<'_>

Returns an indexable string.

§Arguments
  • str - A string slice to be indexed.
§Examples
use indexable_str::IndexableStr;
 
let s = IndexableStr::new("0😀2345678😀");
Source

pub fn as_str(&self) -> &'a str

Returns the original string slice.

§Examples
use indexable_str::IndexableStr;
 
let s = IndexableStr::new("0😀2345678😀");
 
assert_eq!(s.as_str(), "0😀2345678😀");
Source

pub fn len(&self) -> usize

Returns a usize for the number of chars in the string.

§Examples
use indexable_str::IndexableStr;
 
let s = IndexableStr::new("0😀2345678😀");
 
assert_eq!(s.len(), 10);

Trait Implementations§

Source§

impl<'a> Display for IndexableStr<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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.
Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, range: Range<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> Index<RangeFrom<usize>> for IndexableStr<'a>

§Panics

  • If the range end is less than the range start.
Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: RangeFrom<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> Index<RangeTo<usize>> for IndexableStr<'a>

§Panics

  • If the range end is greater than the number of characters in the string.
Source§

type Output = str

The returned type after indexing.
Source§

fn index(&self, index: RangeTo<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> Index<usize> for IndexableStr<'a>

Source§

type Output = char

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &char

Performs the indexing (container[index]) operation. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.