use std::fmt::Debug;
use std::ops::*;
use crate::indexing::*;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct IndexedString {
pub utf8_char_vec: Vec<Box<String>>
}
impl IndexedString {
fn string(&self) -> String {
self.to_string()
}
}
impl ToString for IndexedString {
fn to_string(&self) -> String {
let mut string = String::new();
for item in &self.utf8_char_vec {
string.push_str(item.as_ref());
}
string
}
}
impl Into<String> for IndexedString {
fn into(self) -> String {
self.to_string()
}
}
impl From<String> for IndexedString {
fn from(string: String) -> Self {
let mut utf8_char_vec: Vec<Box<String>> = Vec::new();
let mut index = string.first_index();
loop {
if let Some(current_index) = index {
let utf8_char = string.utf8_char_at(¤t_index).to_string();
utf8_char_vec.push(Box::from(utf8_char));
index = string.index_after(¤t_index);
} else {
break;
}
}
Self {
utf8_char_vec
}
}
}
impl From<&str> for IndexedString {
fn from(str: &str) -> Self {
Self::from(str.to_string())
}
}
impl Index<usize> for IndexedString {
type Output = String;
fn index(&self, index: usize) -> &Self::Output {
&self.utf8_char_vec[index].as_ref()
}
}
impl IndexMut<usize> for IndexedString {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.utf8_char_vec[index].as_mut()
}
}
impl Deref for IndexedString {
type Target = Vec<Box<String>>;
fn deref(&self) -> &Self::Target {
&self.utf8_char_vec
}
}
impl DerefMut for IndexedString {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.utf8_char_vec
}
}