aboutsummaryrefslogtreecommitdiffstats
path: root/src/rope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/rope.rs')
-rw-r--r--src/rope.rs21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/rope.rs b/src/rope.rs
index 3d72833..c176da3 100644
--- a/src/rope.rs
+++ b/src/rope.rs
@@ -60,14 +60,12 @@ impl Rope {
}
}
- fn empty() -> Rope {
- Rope(Node::Leaf(Leaf {
- unsafe_buf: None,
- start: 0,
- end: 0,
- lines: 0,
- }))
- }
+ const EMPTY: Rope = Rope(Node::Leaf(Leaf{
+ unsafe_buf: None,
+ start: 0,
+ end: 0,
+ lines: 0,
+ }));
fn leaf(buf: &[u8]) -> Rope {
if buf.len() > MAX_NODE_SIZE {
@@ -207,7 +205,7 @@ impl Rope {
pub fn read(r: &mut dyn Read) -> Result<Rope, std::io::Error> {
let mut buf = vec![0; MAX_NODE_SIZE];
- let mut rope = Rope::empty();
+ let mut rope = Rope::EMPTY;
loop {
let n = match r.read(&mut buf) {
Ok(n) => n,
@@ -227,6 +225,9 @@ impl Rope {
}
pub fn new(buf: &[u8]) -> Rope {
+ if buf.is_empty() {
+ return Rope::EMPTY;
+ }
if buf.len() <= MAX_NODE_SIZE {
return Rope::leaf(buf);
}
@@ -361,7 +362,7 @@ impl Rope {
}))
}
Rope(Node::Branch(b)) => {
- let mut s = Rope::empty();
+ let mut s = Rope::EMPTY;
let mut start = start;
let mut end = end;
for c in b.children.iter() {