aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-01-11 18:01:10 -0800
committerRose Hogenson <rosehogenson@posteo.net>2024-01-11 18:01:10 -0800
commit7c66a5c1216f334bfa29bddd7e4c3a451900efbb (patch)
treed1308f8bdbac9816d5192c6b21d2ebbe2692231f /src/main.rs
parentFix the cursor behavior at line endings. (diff)
downloadeditor-7c66a5c1216f334bfa29bddd7e4c3a451900efbb.tar.zst
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.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 2b6a814..0dfe335 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -20,6 +20,25 @@ fn line_offset(
line: &Rope,
col: u16,
) -> Result<usize, Box<dyn Error>> {
+ let mut n = 0;
+ for i in 0.. {
+ if i == line.len() {
+ return Ok(i);
+ }
+ let c = line.byte(i);
+ if c > 127 {
+ break;
+ }
+ if n == col {
+ return Ok(i);
+ }
+ if c == b'\t' {
+ n += 8 - n % 8;
+ } else {
+ n += 1;
+ }
+ }
+
// Binary search :D
let mut lo = 0;
let mut hi = line.len() + 1;