Struct GraphemeCursor
pub struct GraphemeCursor { /* private fields */ }
Cursor-based segmenter for grapheme clusters.
This allows working with ropes and other datastructures where the string is not contiguous or fully known at initialization time.
Implementations
impl GraphemeCursor
fn new(offset: usize, len: usize, is_extended: bool) -> GraphemeCursorCreate a new cursor. The string and initial offset are given at creation time, but the contents of the string are not. The
is_extendedparameter controls whether extended grapheme clusters are selected.The
offsetparameter must be on a codepoint boundary.# use unicode_segmentation::GraphemeCursor; let s = "हिन्दी"; let mut legacy = GraphemeCursor::new(0, s.len(), false); assert_eq!(legacy.next_boundary(s, 0), Ok(Some("ह".len()))); let mut extended = GraphemeCursor::new(0, s.len(), true); assert_eq!(extended.next_boundary(s, 0), Ok(Some("हि".len())));fn set_cursor(&mut self, offset: usize)Set the cursor to a new location in the same string.
# use unicode_segmentation::GraphemeCursor; let s = "abcd"; let mut cursor = GraphemeCursor::new(0, s.len(), false); assert_eq!(cursor.cur_cursor(), 0); cursor.set_cursor(2); assert_eq!(cursor.cur_cursor(), 2);fn cur_cursor(&self) -> usizeThe current offset of the cursor. Equal to the last value provided to
new()orset_cursor(), or returned fromnext_boundary()orprev_boundary().# use unicode_segmentation::GraphemeCursor; // Two flags (🇷🇸🇮🇴), each flag is two RIS codepoints, each RIS is 4 bytes. let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = GraphemeCursor::new(4, flags.len(), false); assert_eq!(cursor.cur_cursor(), 4); assert_eq!(cursor.next_boundary(flags, 0), Ok(Some(8))); assert_eq!(cursor.cur_cursor(), 8);fn provide_context(&mut self, chunk: &str, chunk_start: usize)Provide additional pre-context when it is needed to decide a boundary. The end of the chunk must coincide with the value given in the
GraphemeIncomplete::PreContextrequest.# use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete}; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = GraphemeCursor::new(8, flags.len(), false); // Not enough pre-context to decide if there's a boundary between the two flags. assert_eq!(cursor.is_boundary(&flags[8..], 8), Err(GraphemeIncomplete::PreContext(8))); // Provide one more Regional Indicator Symbol of pre-context cursor.provide_context(&flags[4..8], 4); // Still not enough context to decide. assert_eq!(cursor.is_boundary(&flags[8..], 8), Err(GraphemeIncomplete::PreContext(4))); // Provide additional requested context. cursor.provide_context(&flags[0..4], 0); // That's enough to decide (it always is when context goes to the start of the string) assert_eq!(cursor.is_boundary(&flags[8..], 8), Ok(true));fn is_boundary(&mut self, chunk: &str, chunk_start: usize) -> Result<bool, GraphemeIncomplete>Determine whether the current cursor location is a grapheme cluster boundary. Only a part of the string need be supplied. If
chunk_startis nonzero or the length ofchunkis not equal tolenon creation, then this method may returnGraphemeIncomplete::PreContext. The caller should then callprovide_contextwith the requested chunk, then retry calling this method.For partial chunks, if the cursor is not at the beginning or end of the string, the chunk should contain at least the codepoint following the cursor. If the string is nonempty, the chunk must be nonempty.
All calls should have consistent chunk contents (ie, if a chunk provides content for a given slice, all further chunks covering that slice must have the same content for it).
# use unicode_segmentation::GraphemeCursor; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = GraphemeCursor::new(8, flags.len(), false); assert_eq!(cursor.is_boundary(flags, 0), Ok(true)); cursor.set_cursor(12); assert_eq!(cursor.is_boundary(flags, 0), Ok(false));fn next_boundary(&mut self, chunk: &str, chunk_start: usize) -> Result<Option<usize>, GraphemeIncomplete>Find the next boundary after the current cursor position. Only a part of the string need be supplied. If the chunk is incomplete, then this method might return
GraphemeIncomplete::PreContextorGraphemeIncomplete::NextChunk. In the former case, the caller should callprovide_contextwith the requested chunk, then retry. In the latter case, the caller should provide the chunk following the one given, then retry.See
is_boundaryfor expectations on the provided chunk.# use unicode_segmentation::GraphemeCursor; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = GraphemeCursor::new(4, flags.len(), false); assert_eq!(cursor.next_boundary(flags, 0), Ok(Some(8))); assert_eq!(cursor.next_boundary(flags, 0), Ok(Some(16))); assert_eq!(cursor.next_boundary(flags, 0), Ok(None));And an example that uses partial strings:
# use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete}; let s = "abcd"; let mut cursor = GraphemeCursor::new(0, s.len(), false); assert_eq!(cursor.next_boundary(&s[..2], 0), Ok(Some(1))); assert_eq!(cursor.next_boundary(&s[..2], 0), Err(GraphemeIncomplete::NextChunk)); assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(Some(2))); assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(Some(3))); assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(Some(4))); assert_eq!(cursor.next_boundary(&s[2..4], 2), Ok(None));fn prev_boundary(&mut self, chunk: &str, chunk_start: usize) -> Result<Option<usize>, GraphemeIncomplete>Find the previous boundary after the current cursor position. Only a part of the string need be supplied. If the chunk is incomplete, then this method might return
GraphemeIncomplete::PreContextorGraphemeIncomplete::PrevChunk. In the former case, the caller should callprovide_contextwith the requested chunk, then retry. In the latter case, the caller should provide the chunk preceding the one given, then retry.See
is_boundaryfor expectations on the provided chunk.# use unicode_segmentation::GraphemeCursor; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = GraphemeCursor::new(12, flags.len(), false); assert_eq!(cursor.prev_boundary(flags, 0), Ok(Some(8))); assert_eq!(cursor.prev_boundary(flags, 0), Ok(Some(0))); assert_eq!(cursor.prev_boundary(flags, 0), Ok(None));And an example that uses partial strings (note the exact return is not guaranteed, and may be
PrevChunkorPreContextarbitrarily):# use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete}; let s = "abcd"; let mut cursor = GraphemeCursor::new(4, s.len(), false); assert_eq!(cursor.prev_boundary(&s[2..4], 2), Ok(Some(3))); assert_eq!(cursor.prev_boundary(&s[2..4], 2), Err(GraphemeIncomplete::PrevChunk)); assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(Some(2))); assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(Some(1))); assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(Some(0))); assert_eq!(cursor.prev_boundary(&s[0..2], 0), Ok(None));
Trait Implementations
impl Clone for GraphemeCursor
fn clone(&self) -> Self
impl Debug for GraphemeCursor
fn fmt(&self, f: &mut Formatter<'_>) -> Result