use unicode_segmentation::GraphemeCursor;
pub fn previous_grapheme_boundary(text: &str, offset: usize) -> usize {
cursor(text, offset)
.prev_boundary(text, 0)
.ok()
.flatten()
.unwrap_or(0)
}
pub fn next_grapheme_boundary(text: &str, offset: usize) -> usize {
cursor(text, offset)
.next_boundary(text, 0)
.ok()
.flatten()
.unwrap_or(text.len())
}
pub fn floor_grapheme_boundary(text: &str, offset: usize) -> usize {
let mut at = cursor(text, offset);
if at.is_boundary(text, 0).unwrap_or(true) {
at.cur_cursor()
} else {
previous_grapheme_boundary(text, offset)
}
}
fn cursor(text: &str, offset: usize) -> GraphemeCursor {
GraphemeCursor::new(text.floor_char_boundary(offset), text.len(), true)
}