@@ -27,23 +27,11 @@ pub fn grapheme_width(g: &str) -> usize {
29pub fn nth_prev_grapheme_boundary(slice: RopeSlice, char_idx: usize, n: usize) -> usize {
29pub fn nth_prev_grapheme_boundary(slice: RopeSlice, char_idx: usize, n: usize) -> usize {
30− // TODO: implement this more efficiently. This has to do a lot of
31− // re-scanning of rope chunks. Probably move the main implementation here,
32− // and have prev_grapheme_boundary call this instead.
33− let mut char_idx = char_idx;
35− char_idx = prev_grapheme_boundary(slice, char_idx);
40−/// Finds the previous grapheme boundary before the given char position.
41−pub fn prev_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
42 // Bounds check
30 // Bounds check
43 debug_assert!(char_idx <= slice.len_chars());
31 debug_assert!(char_idx <= slice.len_chars());
45 // We work with bytes for this, so convert.
33 // We work with bytes for this, so convert.
46− let byte_idx = slice.char_to_byte(char_idx);
34+ let mut byte_idx = slice.char_to_byte(char_idx);
48 // Get the chunk with our byte index in it.
36 // Get the chunk with our byte index in it.
49 let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
37 let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
@@ -52,46 +40,43 @@ pub fn prev_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
52 let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
40 let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
54 // Find the previous grapheme cluster boundary.
42 // Find the previous grapheme cluster boundary.
56− match gc.prev_boundary(chunk, chunk_byte_idx) {
45+ match gc.prev_boundary(chunk, chunk_byte_idx) {
57− Ok(None) => return 0,
46+ Ok(None) => return 0,
58− Ok(Some(n)) => {
47+ Ok(Some(n)) => {
59− let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
48+ byte_idx = n;
60− return chunk_char_idx + tmp;
49+ break;
62− Err(GraphemeIncomplete::PrevChunk) => {
51+ Err(GraphemeIncomplete::PrevChunk) => {
63− let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1);
52+ let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1);
64− chunk = a;
53+ chunk = a;
65− chunk_byte_idx = b;
54+ chunk_byte_idx = b;
66− chunk_char_idx = c;
55+ chunk_char_idx = c;
68− Err(GraphemeIncomplete::PreContext(n)) => {
57+ Err(GraphemeIncomplete::PreContext(n)) => {
69− let ctx_chunk = slice.chunk_at_byte(n - 1).0;
58+ let ctx_chunk = slice.chunk_at_byte(n - 1).0;
70− gc.provide_context(ctx_chunk, n - ctx_chunk.len());
59+ gc.provide_context(ctx_chunk, n - ctx_chunk.len());
65+ let tmp = byte_to_char_idx(chunk, byte_idx + chunk_byte_idx);
69+/// Finds the previous grapheme boundary before the given char position.
77−pub fn nth_next_grapheme_boundary(slice: RopeSlice, char_idx: usize, n: usize) -> usize {
70+pub fn prev_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
78− // TODO: implement this more efficiently. This has to do a lot of
71+ nth_prev_grapheme_boundary(slice, char_idx, 1)
79− // re-scanning of rope chunks. Probably move the main implementation here,
80− // and have next_grapheme_boundary call this instead.
81− let mut char_idx = char_idx;
83− char_idx = next_grapheme_boundary(slice, char_idx);
88−/// Finds the next grapheme boundary after the given char position.
89−pub fn next_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
74+pub fn nth_next_grapheme_boundary(slice: RopeSlice, char_idx: usize, n: usize) -> usize {
90 // Bounds check
75 // Bounds check
91 debug_assert!(char_idx <= slice.len_chars());
76 debug_assert!(char_idx <= slice.len_chars());
93 // We work with bytes for this, so convert.
78 // We work with bytes for this, so convert.
94− let byte_idx = slice.char_to_byte(char_idx);
79+ let mut byte_idx = slice.char_to_byte(char_idx);
96 // Get the chunk with our byte index in it.
81 // Get the chunk with our byte index in it.
97 let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
82 let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx);
@@ -99,27 +84,36 @@ pub fn next_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
99 // Set up the grapheme cursor.
84 // Set up the grapheme cursor.
100 let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
85 let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
102− // Find the next grapheme cluster boundary.
87+ // Find the nth next grapheme cluster boundary.
104− match gc.next_boundary(chunk, chunk_byte_idx) {
90+ match gc.next_boundary(chunk, chunk_byte_idx) {
105− Ok(None) => return slice.len_chars(),
91+ Ok(None) => return slice.len_chars(),
106− Ok(Some(n)) => {
92+ Ok(Some(n)) => {
107− let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
93+ byte_idx = n;
108− return chunk_char_idx + tmp;
94+ break;
110− Err(GraphemeIncomplete::NextChunk) => {
96+ Err(GraphemeIncomplete::NextChunk) => {
111− chunk_byte_idx += chunk.len();
97+ chunk_byte_idx += chunk.len();
112− let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx);
98+ let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx);
113− chunk = a;
99+ chunk = a;
114− chunk_char_idx = c;
100+ chunk_char_idx = c;
116− Err(GraphemeIncomplete::PreContext(n)) => {
102+ Err(GraphemeIncomplete::PreContext(n)) => {
117− let ctx_chunk = slice.chunk_at_byte(n - 1).0;
103+ let ctx_chunk = slice.chunk_at_byte(n - 1).0;
118− gc.provide_context(ctx_chunk, n - ctx_chunk.len());
104+ gc.provide_context(ctx_chunk, n - ctx_chunk.len());
106+ _ => unreachable!(),
120− _ => unreachable!(),
110+ let tmp = byte_to_char_idx(chunk, byte_idx - chunk_byte_idx);
111+ chunk_char_idx + tmp
114+/// Finds the next grapheme boundary after the given char position.
115+pub fn next_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
116+ nth_next_grapheme_boundary(slice, char_idx, 1)
125/// Returns whether the given char position is a grapheme boundary.
119/// Returns whether the given char position is a grapheme boundary.