aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2023-12-27 08:40:32 -0800
committerRose Hogenson <rosehogenson@posteo.net>2023-12-27 08:40:32 -0800
commit82913ff77a7fc8a4489128cc61b23c1b18b31690 (patch)
tree744aac2693b7856685d842fb563a0c804be141f5 /src
parent9f6447adb277ac7ea6332600d56b98666189168e (diff)
downloadeditor-82913ff77a7fc8a4489128cc61b23c1b18b31690.tar.zst
Fix a bug in how unicode is printed.
Technically a single character can be split across multiple chunks, so we can just join all the chunks before printing. It's not the most efficient, but this is only for debugging.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs2
-rw-r--r--src/rope.rs49
2 files changed, 28 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs
index bb3797d..10d7647 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -29,7 +29,7 @@ fn main() {
println!("Usage: edit <filename>");
std::process::exit(1);
}
- if let Err(err) = edit(&args[0]) {
+ if let Err(err) = edit(&args[1]) {
println!("FAIL: {}", err);
std::process::exit(1);
}
diff --git a/src/rope.rs b/src/rope.rs
index 437d5e3..f26457c 100644
--- a/src/rope.rs
+++ b/src/rope.rs
@@ -4,7 +4,8 @@ use std::io::{ErrorKind, Read};
use std::path::Path;
use std::rc::Rc;
-const MAX_NODE_SIZE: usize = 512;
+// Set to a small value to help identify bugs in the implementation.
+const MAX_NODE_SIZE: usize = 4;
struct Branch {
len: usize,
@@ -17,40 +18,37 @@ enum Node {
Branch(Branch),
}
-impl Display for Node {
- fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+impl Node {
+ fn len(&self) -> usize {
+ return match self {
+ Node::Leaf(v) => v.len(),
+ Node::Branch(b) => b.len,
+ };
+ }
+
+ fn write(&self, out: &mut Vec<u8>) {
match self {
- Node::Leaf(s) => {
- return write!(f, "{}", String::from_utf8_lossy(s));
+ Node::Leaf(v) => {
+ out.extend(v);
}
Node::Branch(b) => {
- if let Err(err) = write!(f, "{}", b.left) {
- return Err(err);
- }
- return write!(f, "{}", b.right);
+ b.left.write(out);
+ b.right.write(out);
}
}
}
}
-impl Node {
- fn len(&self) -> usize {
- return match self {
- Node::Leaf(v) => v.len(),
- Node::Branch(b) => b.len,
- };
+impl Display for Node {
+ fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
+ let mut buf = Vec::new();
+ self.write(&mut buf);
+ return write!(f, "{}", String::from_utf8_lossy(&buf));
}
}
pub struct Rope(Rc<Node>);
-impl Display for Rope {
- fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
- let Rope(n) = self;
- return write!(f, "{}", n);
- }
-}
-
impl Rope {
fn concat(self, Rope(other): Rope) -> Rope {
let Rope(me) = self;
@@ -94,3 +92,10 @@ impl Rope {
return Ok(rope);
}
}
+
+impl Display for Rope {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
+ let Rope(n) = self;
+ return write!(f, "{}", n);
+ }
+}