aboutsummaryrefslogtreecommitdiffstats
path: root/src/rope.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/rope.rs')
-rw-r--r--src/rope.rs42
1 files changed, 16 insertions, 26 deletions
diff --git a/src/rope.rs b/src/rope.rs
index b1b5831..3a01e8b 100644
--- a/src/rope.rs
+++ b/src/rope.rs
@@ -56,12 +56,7 @@ impl Node {
}
}
- fn print_lines(
- &self,
- out: &mut dyn Write,
- start: usize,
- end: usize,
- ) -> Result<(), std::io::Error> {
+ fn print_line(&self, out: &mut dyn Write, n: usize) -> Result<(), std::io::Error> {
match self {
Node::Leaf(v) => {
let mut nl_count = 0;
@@ -72,11 +67,12 @@ impl Node {
continue;
}
nl_count += 1;
- if nl_count == start {
- start_pos = i;
+ if nl_count == n {
+ start_pos = i + 1;
}
- if nl_count == end {
- end_pos = i + 1;
+ if nl_count == n + 1 {
+ end_pos = i;
+ break;
}
}
if let Err(err) = out.write(&v[start_pos..end_pos]) {
@@ -85,19 +81,18 @@ impl Node {
return Ok(());
}
Node::Branch(b) => {
- let right_start;
- if start <= b.left.lines() {
- if let Err(err) = b.left.print_lines(out, start, end) {
+ let left_lines = b.left.lines();
+ if n <= left_lines {
+ if let Err(err) = b.left.print_line(out, n) {
return Err(err);
}
- right_start = 0;
- } else {
- right_start = start - b.left.lines();
}
- if end <= b.left.lines() {
- return Ok(());
+ if n >= left_lines {
+ if let Err(err) = b.right.print_line(out, n - left_lines) {
+ return Err(err);
+ }
}
- return b.right.print_lines(out, right_start, end - b.left.lines());
+ return Ok(());
}
}
}
@@ -157,14 +152,9 @@ impl Rope {
return Ok(rope);
}
- pub fn print_lines(
- &self,
- out: &mut dyn Write,
- start: usize,
- end: usize,
- ) -> Result<(), std::io::Error> {
+ pub fn print_line(&self, out: &mut dyn Write, n: usize) -> Result<(), std::io::Error> {
let Rope(me) = self;
- return me.print_lines(out, start, end);
+ return me.print_line(out, n);
}
}