@@ -27,23 +27,11 @@ pub fn grapheme_width(g: &str) -> usize {
2727}
2828
2929pub 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;
34− for _ in 0..n {
35− char_idx = prev_grapheme_boundary(slice, char_idx);
36− }
37− char_idx
38−}
39−
40−/// Finds the previous grapheme boundary before the given char position.
41−pub fn prev_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
4230 // Bounds check
4331 debug_assert!(char_idx <= slice.len_chars());
4432
4533 // 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);
4735
4836 // Get the chunk with our byte index in it.
4937 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 {
5240 let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
5341
5442 // Find the previous grapheme cluster boundary.
55− loop {
56− match gc.prev_boundary(chunk, chunk_byte_idx) {
57− Ok(None) => return 0,
58− Ok(Some(n)) => {
59− let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
60− return chunk_char_idx + tmp;
61− }
62− Err(GraphemeIncomplete::PrevChunk) => {
63− let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1);
64− chunk = a;
65− chunk_byte_idx = b;
66− chunk_char_idx = c;
67− }
68− Err(GraphemeIncomplete::PreContext(n)) => {
69− let ctx_chunk = slice.chunk_at_byte(n - 1).0;
70− gc.provide_context(ctx_chunk, n - ctx_chunk.len());
43+ for _ in 0..n {
44+ loop {
45+ match gc.prev_boundary(chunk, chunk_byte_idx) {
46+ Ok(None) => return 0,
47+ Ok(Some(n)) => {
48+ byte_idx = n;
49+ break;
50+ }
51+ Err(GraphemeIncomplete::PrevChunk) => {
52+ let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1);
53+ chunk = a;
54+ chunk_byte_idx = b;
55+ chunk_char_idx = c;
56+ }
57+ Err(GraphemeIncomplete::PreContext(n)) => {
58+ let ctx_chunk = slice.chunk_at_byte(n - 1).0;
59+ gc.provide_context(ctx_chunk, n - ctx_chunk.len());
60+ }
61+ _ => unreachable!(),
7162 }
72− _ => unreachable!(),
7363 }
7464 }
65+ let tmp = byte_to_char_idx(chunk, byte_idx + chunk_byte_idx);
66+ chunk_char_idx + tmp
7567}
7668
77−pub fn nth_next_grapheme_boundary(slice: RopeSlice, char_idx: usize, n: usize) -> usize {
78− // TODO: implement this more efficiently. This has to do a lot of
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;
82− for _ in 0..n {
83− char_idx = next_grapheme_boundary(slice, char_idx);
84− }
85− char_idx
69+/// Finds the previous grapheme boundary before the given char position.
70+pub fn prev_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize {
71+ nth_prev_grapheme_boundary(slice, char_idx, 1)
8672}
8773
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 {
9075 // Bounds check
9176 debug_assert!(char_idx <= slice.len_chars());
9277
9378 // 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);
9580
9681 // Get the chunk with our byte index in it.
9782 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 {
9984 // Set up the grapheme cursor.
10085 let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true);
10186
102− // Find the next grapheme cluster boundary.
103− loop {
104− match gc.next_boundary(chunk, chunk_byte_idx) {
105− Ok(None) => return slice.len_chars(),
106− Ok(Some(n)) => {
107− let tmp = byte_to_char_idx(chunk, n - chunk_byte_idx);
108− return chunk_char_idx + tmp;
109− }
110− Err(GraphemeIncomplete::NextChunk) => {
111− chunk_byte_idx += chunk.len();
112− let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx);
113− chunk = a;
114− chunk_char_idx = c;
115− }
116− Err(GraphemeIncomplete::PreContext(n)) => {
117− let ctx_chunk = slice.chunk_at_byte(n - 1).0;
118− gc.provide_context(ctx_chunk, n - ctx_chunk.len());
87+ // Find the nth next grapheme cluster boundary.
88+ for _ in 0..n {
89+ loop {
90+ match gc.next_boundary(chunk, chunk_byte_idx) {
91+ Ok(None) => return slice.len_chars(),
92+ Ok(Some(n)) => {
93+ byte_idx = n;
94+ break;
95+ }
96+ Err(GraphemeIncomplete::NextChunk) => {
97+ chunk_byte_idx += chunk.len();
98+ let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx);
99+ chunk = a;
100+ chunk_char_idx = c;
101+ }
102+ Err(GraphemeIncomplete::PreContext(n)) => {
103+ let ctx_chunk = slice.chunk_at_byte(n - 1).0;
104+ gc.provide_context(ctx_chunk, n - ctx_chunk.len());
105+ }
106+ _ => unreachable!(),
119107 }
120− _ => unreachable!(),
121108 }
122109 }
110+ let tmp = byte_to_char_idx(chunk, byte_idx - chunk_byte_idx);
111+ chunk_char_idx + tmp
112+}
113+
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)
123117}
124118
125119/// Returns whether the given char position is a grapheme boundary.