diff --git a/Cargo.toml b/Cargo.toml index 904a8a5..df965e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,8 @@ git = "https://github.com/cessen/ropey.git" [dependencies] time = "0.1.30" rustc-serialize = "0.3.0" +unicode-segmentation = "0.1.*" +unicode-width = "0.1.*" docopt = "0.6.*" encoding = "*" rustbox = "0.6.3" diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index d9aa5e2..f010a1b 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -105,7 +105,7 @@ impl Buffer { let cpos = self.text.grapheme_index_to_char_index(pos); self._insert_text(text, cpos); - self.undo_stack.push(InsertText(String::from_str(text), cpos)); + self.undo_stack.push(InsertText(text.to_string(), cpos)); } fn _insert_text(&mut self, text: &str, pos: usize) { diff --git a/src/editor/cursor.rs b/src/editor/cursor.rs index 500e75d..466db65 100644 --- a/src/editor/cursor.rs +++ b/src/editor/cursor.rs @@ -57,11 +57,11 @@ impl CursorSet { } pub fn iter<'a>(&'a self) -> Iter<'a, Cursor> { - self.cursors.as_slice().iter() + (&self.cursors[..]).iter() } pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, Cursor> { - self.cursors.as_mut_slice().iter_mut() + (&mut self.cursors[..]).iter_mut() } pub fn make_consistent(&mut self) { diff --git a/src/main.rs b/src/main.rs index 7651c3c..a39cff7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,15 @@ -#![feature(core)] -#![feature(collections)] -#![feature(test)] -#![feature(std_misc)] +//#![feature(core)] +//#![feature(collections)] +//#![feature(test)] +//#![feature(std_misc)] extern crate time; -extern crate test; +//extern crate test; extern crate rustbox; extern crate docopt; extern crate rustc_serialize; +extern crate unicode_segmentation; +extern crate unicode_width; extern crate encoding; extern crate ropey; //extern crate freetype; diff --git a/src/string_utils.rs b/src/string_utils.rs index c1d9b94..46dfaf0 100644 --- a/src/string_utils.rs +++ b/src/string_utils.rs @@ -2,6 +2,7 @@ //! Misc helpful utility functions for TextBuffer related stuff. use std::iter::repeat; +use unicode_segmentation::UnicodeSegmentation; pub fn is_line_ending(text: &str) -> bool { @@ -53,7 +54,7 @@ pub fn is_whitespace(text: &str) -> bool { pub fn line_ending_count(text: &str) -> usize { let mut count = 0; - for g in text.graphemes(true) { + for g in UnicodeSegmentation::graphemes(text, true) { if is_line_ending(g) { count += 1; } @@ -71,7 +72,7 @@ pub fn char_count(text: &str) -> usize { pub fn grapheme_count(text: &str) -> usize { let mut count = 0; - for _ in text.graphemes(true) { + for _ in UnicodeSegmentation::graphemes(text, true) { count += 1; } return count; @@ -79,7 +80,7 @@ pub fn grapheme_count(text: &str) -> usize { pub fn grapheme_count_is_less_than(text: &str, n: usize) -> bool { let mut count = 0; - for _ in text.graphemes(true) { + for _ in UnicodeSegmentation::graphemes(text, true) { count += 1; if count >= n { return false; @@ -93,7 +94,7 @@ pub fn grapheme_and_line_ending_count(text: &str) -> (usize, usize) { let mut grapheme_count = 0; let mut line_ending_count = 0; - for g in text.graphemes(true) { + for g in UnicodeSegmentation::graphemes(text, true) { grapheme_count += 1; if is_line_ending(g) { line_ending_count += 1; @@ -123,7 +124,7 @@ pub fn char_pos_to_byte_pos(text: &str, pos: usize) -> usize { pub fn grapheme_pos_to_byte_pos(text: &str, pos: usize) -> usize { let mut i: usize = 0; - for (offset, _) in text.grapheme_indices(true) { + for (offset, _) in UnicodeSegmentation::grapheme_indices(text, true) { if i == pos { return offset; } @@ -162,7 +163,7 @@ pub fn insert_text_at_grapheme_index(s: &mut String, text: &str, pos: usize) { // Copy new bytes in // TODO: use copy_memory() let mut i = byte_pos; - for g in text.graphemes(true) { + for g in UnicodeSegmentation::graphemes(text, true) { for b in g.bytes() { byte_vec[i] = b; @@ -216,7 +217,7 @@ pub fn split_string_at_grapheme_index(s1: &mut String, pos: usize) -> String { let byte_vec_1 = unsafe { s1.as_mut_vec() }; let byte_vec_2 = unsafe { s2.as_mut_vec() }; - byte_vec_2.push_all(&byte_vec_1[byte_pos..]); + byte_vec_2.extend((&byte_vec_1[byte_pos..]).iter().cloned()); byte_vec_1.truncate(byte_pos); } diff --git a/src/term_ui/formatter.rs b/src/term_ui/formatter.rs index 7dbe64b..a7d15f3 100644 --- a/src/term_ui/formatter.rs +++ b/src/term_ui/formatter.rs @@ -1,5 +1,6 @@ use std::cmp::max; +use unicode_width::UnicodeWidthStr; use string_utils::{is_line_ending, is_whitespace}; use formatter::{LineFormatter, RoundingBehavior}; @@ -290,7 +291,7 @@ fn grapheme_vis_width_at_vis_pos(g: &str, pos: usize, tab_width: usize) -> usize return 1; } else { - return g.width(true); + return UnicodeWidthStr::width(g); } } }