2021-03-08 10:43:40 -05:00
|
|
|
use std::cmp;
|
2021-03-08 14:21:24 -05:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2021-03-08 10:43:40 -05:00
|
|
|
|
2021-03-10 13:48:21 -05:00
|
|
|
#[derive(Default)]
|
2021-03-08 10:21:06 -05:00
|
|
|
pub struct Row {
|
|
|
|
string: String,
|
2021-03-08 14:21:24 -05:00
|
|
|
len: usize,
|
2021-03-08 10:43:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&str> for Row {
|
|
|
|
fn from(slice: &str) -> Self {
|
2021-03-08 14:21:24 -05:00
|
|
|
let mut row = Self {
|
2021-03-08 10:43:40 -05:00
|
|
|
string: String::from(slice),
|
2021-03-08 14:21:24 -05:00
|
|
|
len: 0,
|
|
|
|
};
|
|
|
|
row.update_len();
|
|
|
|
row
|
|
|
|
|
2021-03-08 10:43:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Row {
|
|
|
|
pub fn render(&self, start: usize, end: usize) -> String {
|
|
|
|
let end = cmp::min(end, self.string.len());
|
|
|
|
let start = cmp::min(start, end);
|
2021-03-08 14:21:24 -05:00
|
|
|
|
|
|
|
let mut result = String::new();
|
|
|
|
|
2021-03-10 16:09:46 -05:00
|
|
|
#[allow(clippy::integer_arithmetic)]
|
2021-03-08 14:21:24 -05:00
|
|
|
for grapheme in self.string[..]
|
|
|
|
.graphemes(true)
|
|
|
|
.skip(start)
|
|
|
|
.take(end - start)
|
|
|
|
{
|
|
|
|
if grapheme == "\t" {
|
|
|
|
result.push_str(" ");
|
|
|
|
} else {
|
|
|
|
result.push_str(grapheme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.len
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_len(&mut self) {
|
|
|
|
self.len = self.string[..].graphemes(true).count();
|
2021-03-08 10:43:40 -05:00
|
|
|
}
|
2021-03-10 13:48:21 -05:00
|
|
|
|
|
|
|
pub fn insert(&mut self, at: usize, c: char) {
|
|
|
|
if at >= self.len() {
|
|
|
|
self.string.push(c);
|
|
|
|
} else {
|
|
|
|
let mut result: String = self.string[..].graphemes(true).take(at).collect();
|
|
|
|
let remainder: String = self.string[..].graphemes(true).skip(at).collect();
|
|
|
|
|
|
|
|
result.push(c);
|
|
|
|
result.push_str(&remainder);
|
|
|
|
|
|
|
|
self.string = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.update_len();
|
|
|
|
}
|
|
|
|
|
2021-03-10 16:09:46 -05:00
|
|
|
#[allow(clippy::integer_arithmetic)]
|
2021-03-10 13:48:21 -05:00
|
|
|
pub fn delete(&mut self, at: usize) {
|
|
|
|
if at >= self.len() {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
let mut result: String = self.string[..].graphemes(true).take(at).collect();
|
|
|
|
let remainder: String = self.string[..].graphemes(true).skip(at + 1).collect();
|
|
|
|
|
|
|
|
result.push_str(&remainder);
|
|
|
|
|
|
|
|
self.string = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.update_len();
|
|
|
|
}
|
2021-03-10 14:37:58 -05:00
|
|
|
|
|
|
|
pub fn append(&mut self, new: &Self) {
|
|
|
|
self.string = format!("{}{}", self.string, new.string);
|
|
|
|
self.update_len();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(&mut self, at: usize) -> Self {
|
|
|
|
let beginning: String = self.string[..].graphemes(true).take(at).collect();
|
|
|
|
let remainder: String = self.string[..].graphemes(true).skip(at).collect();
|
|
|
|
|
|
|
|
self.string = beginning;
|
|
|
|
self.update_len();
|
|
|
|
|
|
|
|
Self::from(&remainder[..])
|
|
|
|
}
|
2021-03-10 14:51:11 -05:00
|
|
|
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
self.string.as_bytes()
|
|
|
|
}
|
2021-03-08 10:21:06 -05:00
|
|
|
}
|