Maximize chunks occupation by splitting chunks appropriately
This commit is contained in:
@@ -59,11 +59,7 @@ impl Rope {
|
|||||||
let mut text = ArrayString::<[_; 4 * CHUNK_BASE]>::new();
|
let mut text = ArrayString::<[_; 4 * CHUNK_BASE]>::new();
|
||||||
text.push_str(&last_chunk.0);
|
text.push_str(&last_chunk.0);
|
||||||
text.push_str(&first_new_chunk_ref.0);
|
text.push_str(&first_new_chunk_ref.0);
|
||||||
let mut midpoint = text.len() / 2;
|
let (left, right) = text.split_at(find_split_ix(&text));
|
||||||
while !text.is_char_boundary(midpoint) {
|
|
||||||
midpoint += 1;
|
|
||||||
}
|
|
||||||
let (left, right) = text.split_at(midpoint);
|
|
||||||
last_chunk.0.clear();
|
last_chunk.0.clear();
|
||||||
last_chunk.0.push_str(left);
|
last_chunk.0.push_str(left);
|
||||||
first_new_chunk_ref.0.clear();
|
first_new_chunk_ref.0.clear();
|
||||||
@@ -221,7 +217,7 @@ impl<'a> Cursor<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
struct Chunk(ArrayString<[u8; 4 * CHUNK_BASE]>);
|
struct Chunk(ArrayString<[u8; 2 * CHUNK_BASE]>);
|
||||||
|
|
||||||
impl Chunk {
|
impl Chunk {
|
||||||
fn to_point(&self, target: usize) -> Point {
|
fn to_point(&self, target: usize) -> Point {
|
||||||
@@ -397,6 +393,25 @@ impl<'a> Iterator for Chars<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn find_split_ix(text: &str) -> usize {
|
||||||
|
let mut ix = text.len() / 2;
|
||||||
|
while !text.is_char_boundary(ix) {
|
||||||
|
if ix < 2 * CHUNK_BASE {
|
||||||
|
ix += 1;
|
||||||
|
} else {
|
||||||
|
ix = (text.len() / 2) - 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while !text.is_char_boundary(ix) {
|
||||||
|
ix -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
debug_assert!(ix <= 2 * CHUNK_BASE);
|
||||||
|
debug_assert!(text.len() - ix <= 2 * CHUNK_BASE);
|
||||||
|
ix
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::util::RandomCharIter;
|
use crate::util::RandomCharIter;
|
||||||
|
|||||||
Reference in New Issue
Block a user