Optimize loopage of graphemes, finish chapter 5
This commit is contained in:
parent
297ff6b80b
commit
fa70f3fd61
86
src/row.rs
86
src/row.rs
@ -9,13 +9,10 @@ pub struct Row {
|
|||||||
|
|
||||||
impl From<&str> for Row {
|
impl From<&str> for Row {
|
||||||
fn from(slice: &str) -> Self {
|
fn from(slice: &str) -> Self {
|
||||||
let mut row = Self {
|
Self {
|
||||||
string: String::from(slice),
|
string: String::from(slice),
|
||||||
len: 0,
|
len: slice.graphemes(true).count(),
|
||||||
};
|
}
|
||||||
row.update_len();
|
|
||||||
row
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,55 +47,80 @@ impl Row {
|
|||||||
self.len == 0
|
self.len == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_len(&mut self) {
|
|
||||||
self.len = self.string[..].graphemes(true).count();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert(&mut self, at: usize, c: char) {
|
pub fn insert(&mut self, at: usize, c: char) {
|
||||||
if at >= self.len() {
|
if at >= self.len() {
|
||||||
self.string.push(c);
|
self.string.push(c);
|
||||||
} else {
|
self.len += 1;
|
||||||
let mut result: String = self.string[..].graphemes(true).take(at).collect();
|
|
||||||
let remainder: String = self.string[..].graphemes(true).skip(at).collect();
|
|
||||||
|
|
||||||
result.push(c);
|
return;
|
||||||
result.push_str(&remainder);
|
|
||||||
|
|
||||||
self.string = result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.update_len();
|
let mut result = String::new();
|
||||||
|
let mut length = 0;
|
||||||
|
|
||||||
|
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
||||||
|
length += 1;
|
||||||
|
|
||||||
|
if index == at {
|
||||||
|
length += 1;
|
||||||
|
result.push(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push_str(grapheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.len = length;
|
||||||
|
self.string = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::integer_arithmetic)]
|
#[allow(clippy::integer_arithmetic)]
|
||||||
pub fn delete(&mut self, at: usize) {
|
pub fn delete(&mut self, at: usize) {
|
||||||
if at >= self.len() {
|
if at >= self.len() {
|
||||||
return
|
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();
|
let mut result = String::new();
|
||||||
|
let mut length = 0;
|
||||||
|
|
||||||
|
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
||||||
|
if index != at {
|
||||||
|
length += 1;
|
||||||
|
result.push_str(grapheme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.len = length;
|
||||||
|
self.string = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append(&mut self, new: &Self) {
|
pub fn append(&mut self, new: &Self) {
|
||||||
self.string = format!("{}{}", self.string, new.string);
|
self.string = format!("{}{}", self.string, new.string);
|
||||||
self.update_len();
|
self.len += new.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn split(&mut self, at: usize) -> Self {
|
pub fn split(&mut self, at: usize) -> Self {
|
||||||
let beginning: String = self.string[..].graphemes(true).take(at).collect();
|
let mut row = String::new();
|
||||||
let remainder: String = self.string[..].graphemes(true).skip(at).collect();
|
let mut length = 0;
|
||||||
|
let mut splitted_row = String::new();
|
||||||
|
let mut splittend_length = 0;
|
||||||
|
|
||||||
self.string = beginning;
|
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
||||||
self.update_len();
|
if index < at {
|
||||||
|
length += 1;
|
||||||
|
row.push_str(grapheme);
|
||||||
|
} else {
|
||||||
|
splittend_length += 1;
|
||||||
|
splitted_row.push_str(grapheme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Self::from(&remainder[..])
|
self.string = row;
|
||||||
|
self.len = length;
|
||||||
|
|
||||||
|
Self {
|
||||||
|
string: splitted_row,
|
||||||
|
len: splittend_length,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_bytes(&self) -> &[u8] {
|
pub fn as_bytes(&self) -> &[u8] {
|
||||||
|
Loading…
Reference in New Issue
Block a user