Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-core/src/graphemes.rs')
| -rw-r--r-- | helix-core/src/graphemes.rs | 125 |
1 files changed, 0 insertions, 125 deletions
diff --git a/helix-core/src/graphemes.rs b/helix-core/src/graphemes.rs index 4cbb5746..10c8fc7d 100644 --- a/helix-core/src/graphemes.rs +++ b/helix-core/src/graphemes.rs @@ -1,8 +1,6 @@ //! Utility functions to traverse the unicode graphemes of a `Rope`'s text contents. //! //! Based on <https://github.com/cessen/led/blob/c4fa72405f510b7fd16052f90a598c429b3104a6/src/graphemes.rs> -use ropey::{str_utils::byte_to_char_idx, RopeSlice}; -use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete}; use unicode_width::UnicodeWidthStr; use std::borrow::Cow; @@ -119,129 +117,6 @@ pub fn grapheme_width(g: &str) -> usize { } } -// NOTE: for byte indexing versions of these functions see `RopeSliceExt`'s -// `floor_grapheme_boundary` and `ceil_grapheme_boundary` and the rope grapheme iterators. - -#[must_use] -pub fn nth_prev_grapheme_boundary(slice: RopeSlice, char_idx: usize, n: usize) -> usize { - // Bounds check - debug_assert!(char_idx <= slice.len_chars()); - - // We work with bytes for this, so convert. - let mut byte_idx = slice.char_to_byte(char_idx); - - // Get the chunk with our byte index in it. - let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx); - - // Set up the grapheme cursor. - let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true); - - // Find the previous grapheme cluster boundary. - for _ in 0..n { - loop { - match gc.prev_boundary(chunk, chunk_byte_idx) { - Ok(None) => return 0, - Ok(Some(n)) => { - byte_idx = n; - break; - } - Err(GraphemeIncomplete::PrevChunk) => { - let (a, b, c, _) = slice.chunk_at_byte(chunk_byte_idx - 1); - chunk = a; - chunk_byte_idx = b; - chunk_char_idx = c; - } - Err(GraphemeIncomplete::PreContext(n)) => { - let ctx_chunk = slice.chunk_at_byte(n - 1).0; - gc.provide_context(ctx_chunk, n - ctx_chunk.len()); - } - _ => unreachable!(), - } - } - } - let tmp = byte_to_char_idx(chunk, byte_idx - chunk_byte_idx); - chunk_char_idx + tmp -} - -/// Finds the previous grapheme boundary before the given char position. -#[must_use] -#[inline(always)] -pub fn prev_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize { - nth_prev_grapheme_boundary(slice, char_idx, 1) -} - -#[must_use] -pub fn nth_next_grapheme_boundary(slice: RopeSlice, char_idx: usize, n: usize) -> usize { - // Bounds check - debug_assert!(char_idx <= slice.len_chars()); - - // We work with bytes for this, so convert. - let mut byte_idx = slice.char_to_byte(char_idx); - - // Get the chunk with our byte index in it. - let (mut chunk, mut chunk_byte_idx, mut chunk_char_idx, _) = slice.chunk_at_byte(byte_idx); - - // Set up the grapheme cursor. - let mut gc = GraphemeCursor::new(byte_idx, slice.len_bytes(), true); - - // Find the nth next grapheme cluster boundary. - for _ in 0..n { - loop { - match gc.next_boundary(chunk, chunk_byte_idx) { - Ok(None) => return slice.len_chars(), - Ok(Some(n)) => { - byte_idx = n; - break; - } - Err(GraphemeIncomplete::NextChunk) => { - chunk_byte_idx += chunk.len(); - let (a, _, c, _) = slice.chunk_at_byte(chunk_byte_idx); - chunk = a; - chunk_char_idx = c; - } - Err(GraphemeIncomplete::PreContext(n)) => { - let ctx_chunk = slice.chunk_at_byte(n - 1).0; - gc.provide_context(ctx_chunk, n - ctx_chunk.len()); - } - _ => unreachable!(), - } - } - } - let tmp = byte_to_char_idx(chunk, byte_idx - chunk_byte_idx); - chunk_char_idx + tmp -} - -/// Finds the next grapheme boundary after the given char position. -#[must_use] -#[inline(always)] -pub fn next_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize { - nth_next_grapheme_boundary(slice, char_idx, 1) -} - -/// Returns the passed char index if it's already a grapheme boundary, -/// or the next grapheme boundary char index if not. -#[must_use] -#[inline] -pub fn ensure_grapheme_boundary_next(slice: RopeSlice, char_idx: usize) -> usize { - if char_idx == 0 { - char_idx - } else { - next_grapheme_boundary(slice, char_idx - 1) - } -} - -/// Returns the passed char index if it's already a grapheme boundary, -/// or the prev grapheme boundary char index if not. -#[must_use] -#[inline] -pub fn ensure_grapheme_boundary_prev(slice: RopeSlice, char_idx: usize) -> usize { - if char_idx == slice.len_chars() { - char_idx - } else { - prev_grapheme_boundary(slice, char_idx + 1) - } -} - /// A highly compressed Cow<'a, str> that holds /// atmost u31::MAX bytes and is readonly pub struct GraphemeStr<'a> { |