diff options
Diffstat (limited to 'src/rope.rs')
| -rw-r--r-- | src/rope.rs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/rope.rs b/src/rope.rs index 11a030b..5454557 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -152,11 +152,24 @@ impl Rope { new_buf[pos + 1..].copy_from_slice(&l.bytes()[pos..]); return Rope::leaf(new_buf); } - let mut buf_left = vec![0; pos + 1]; - buf_left[..pos].copy_from_slice(&l.bytes()[..pos]); - buf_left[pos] = c; - let mut buf_right = Vec::new(); - buf_right.extend_from_slice(&l.bytes()[pos..]); + let half = MAX_NODE_SIZE / 2; + let mut buf_left; + let mut buf_right; + if pos <= half { + buf_left = vec![0; half+1]; + buf_right = vec![0; half]; + buf_left[..pos].copy_from_slice(&l.bytes()[..pos]); + buf_left[pos] = c; + buf_left[pos+1..].copy_from_slice(&l.bytes()[pos..half]); + buf_right.copy_from_slice(&l.bytes()[half..]); + } else { + buf_left = vec![0; half]; + buf_right = vec![0; half+1]; + buf_left.copy_from_slice(&l.bytes()[..half]); + buf_right[..pos-half].copy_from_slice(&l.bytes()[half..pos]); + buf_right[pos-half] = c; + buf_right[pos-half+1..].copy_from_slice(&l.bytes()[pos..]); + } return Rope::leaf(buf_left).concat(&Rope::leaf(buf_right)); } Rope(Node::Branch(b)) => { |
