From 1e32f5d480ecefa3a1c8fd1c43ac6b4260459266 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 13 Jan 2024 12:31:42 -0800 Subject: Reject files with invalid utf-8. It's easier to only support files with valid utf-8. --- src/main.rs | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 5b9d2f5..0cc9ad4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::fs::File; use std::io::{stderr, stdout, Write}; use std::path::{Path, PathBuf}; -fn open(file: &Path) -> Result { +fn open(file: &Path) -> Result> { let mut f = File::open(file)?; let r = Rope::read(&mut f)?; Ok(r) @@ -38,10 +38,7 @@ fn line_offset(stdin: &mut Reader, line: &Rope, col: u16) -> Result Result> { - const CTRL_Q: u8 = b'Q' - b'@'; - const CTRL_S: u8 = b'S' - b'@'; - const CTRL_Y: u8 = b'Y' - b'@'; - const CTRL_Z: u8 = b'Z' - b'@'; - const BACKSPACE: u8 = b'H' - b'@'; - const OTHER_BACKSPACE: u8 = 127; + const CTRL_Q: char = (b'Q' - b'@') as char; + const CTRL_S: char = (b'S' - b'@') as char; + const CTRL_Y: char = (b'Y' - b'@') as char; + const CTRL_Z: char = (b'Z' - b'@') as char; + const BACKSPACE: char = (b'H' - b'@') as char; + const OTHER_BACKSPACE: char = '\x7f'; let inserting = self.inserting; self.inserting = false; @@ -446,15 +443,15 @@ impl State { self.update_cursor_col()?; stdout().flush()?; } - Key::Byte(CTRL_Q) => { + Key::Char(CTRL_Q) => { return Ok(false); } - Key::Byte(CTRL_S) => { + Key::Char(CTRL_S) => { let mut f = File::create(&self.path)?; self.buf.print(&mut f)?; f.sync_all()?; } - Key::Byte(CTRL_Z) => { + Key::Char(CTRL_Z) => { if let Some(n) = self.n { if n > 0 { self.n = Some(n - 1); @@ -468,7 +465,7 @@ impl State { self.update_cursor_col()?; stdout().flush()?; } - Key::Byte(CTRL_Y) => { + Key::Char(CTRL_Y) => { let Some(n) = self.n else { return Ok(true); }; @@ -481,7 +478,7 @@ impl State { self.update_cursor_col()?; stdout().flush()?; } - Key::Byte(BACKSPACE) | Key::Byte(OTHER_BACKSPACE) => { + Key::Char(BACKSPACE) | Key::Char(OTHER_BACKSPACE) => { self.insert(inserting); if self.line_offset == 0 && self.cursor_row == 0 && self.row_start == 0 { return Ok(true); @@ -519,7 +516,7 @@ impl State { self.update_cursor_col()?; stdout().flush()?; } - Key::Byte(b'\r') | Key::Byte(b'\n') => { + Key::Char('\r') | Key::Char('\n') => { self.insert(inserting); let size = term::size()?; let offset = self @@ -529,7 +526,7 @@ impl State { self.buf = self .buf .slice(0, offset) - .concat(&Rope::new(&[b'\n'])) + .concat(&Rope::new("\n")) .concat(&self.buf.slice(offset, self.buf.len())); if self.cursor_row < size.ws_row - 1 { self.cursor_row += 1; @@ -542,20 +539,22 @@ impl State { write!(stdout(), "\r\n")?; stdout().flush()?; } - Key::Byte(c) => { + Key::Char(c) => { self.insert(inserting); let offset = self .buf .line_start(self.row_start + usize::from(self.cursor_row)) + self.line_offset; + let mut buf = [0; 4]; + let s = c.encode_utf8(&mut buf); self.buf = self .buf .slice(0, offset) - .concat(&Rope::new(&[c])) + .concat(&Rope::new(s)) .concat(&self.buf.slice(offset, self.buf.len())); - self.line_offset += 1; + self.line_offset += s.len(); self.need_repaint_line = true; - stdout().write_all(&[c])?; + stdout().write_all(s.as_bytes())?; stdout().flush()?; } } -- cgit v1.3.1