From 7c66a5c1216f334bfa29bddd7e4c3a451900efbb Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 11 Jan 2024 18:01:10 -0800 Subject: Optimise line_offset for the ASCII case. In most cases we really won't need the full binary search. We can check quickly for ASCII without ever printing anything, and fall back to binary search if we find any Unicode characters. --- src/rope.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/rope.rs') diff --git a/src/rope.rs b/src/rope.rs index 31c3296..737d949 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -355,4 +355,23 @@ impl Rope { } } } + + pub fn byte(&self, i: usize) -> u8 { + if i >= self.len() { + panic!("Index {} out of range 0..{}", i, self.len()); + } + match self { + Rope(Node::Leaf(l)) => l.bytes()[i], + Rope(Node::Branch(b)) => { + let mut i = i; + for c in b.children().iter() { + if i < c.len() { + return c.byte(i); + } + i -= c.len(); + } + panic!("unreachable"); + } + } + } } -- cgit v1.3.1