diff options
author | Nathan Vegdahl | 2021-06-26 22:37:32 +0000 |
---|---|---|
committer | Nathan Vegdahl | 2021-07-01 21:22:28 +0000 |
commit | d07074740bc44b71de83cf23dd692fa90c2854a9 (patch) | |
tree | 941542d44d101bebc082151ba924fe10888d2d38 /helix-core/src/graphemes.rs | |
parent | c1b0a7197556ec0383d6b0d0e7f510ecf4dcd21f (diff) |
Add `Range` methods for various kinds of validation.
Diffstat (limited to 'helix-core/src/graphemes.rs')
-rw-r--r-- | helix-core/src/graphemes.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/helix-core/src/graphemes.rs b/helix-core/src/graphemes.rs index f71b6d5f..f7bf66c0 100644 --- a/helix-core/src/graphemes.rs +++ b/helix-core/src/graphemes.rs @@ -123,14 +123,24 @@ pub fn next_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize { /// Returns the passed char index if it's already a grapheme boundary, /// or the next grapheme boundary char index if not. -pub fn ensure_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> usize { +pub fn ensure_grapheme_boundary_next(slice: RopeSlice, char_idx: usize) -> usize { if char_idx == 0 { - 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. +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) + } +} + /// Returns whether the given char position is a grapheme boundary. pub fn is_grapheme_boundary(slice: RopeSlice, char_idx: usize) -> bool { // Bounds check |