Struct GraphemeCursor
struct GraphemeCursor { ... }
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 GraphemeCursor; let s = "हिन्दी"; let mut legacy = new; assert_eq!; let mut extended = new; assert_eq!;fn set_cursor(self: &mut Self, offset: usize)Set the cursor to a new location in the same string.
# use GraphemeCursor; let s = "abcd"; let mut cursor = new; assert_eq!; cursor.set_cursor; assert_eq!;fn cur_cursor(self: &Self) -> usizeThe current offset of the cursor. Equal to the last value provided to
new()orset_cursor(), or returned fromnext_boundary()orprev_boundary().# use 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 = new; assert_eq!; assert_eq!; assert_eq!;fn provide_context(self: &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 ; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = new; // Not enough pre-context to decide if there's a boundary between the two flags. assert_eq!; // Provide one more Regional Indicator Symbol of pre-context cursor.provide_context; // Still not enough context to decide. assert_eq!; // Provide additional requested context. cursor.provide_context; // That's enough to decide (it always is when context goes to the start of the string) assert_eq!;fn is_boundary(self: &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 GraphemeCursor; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = new; assert_eq!; cursor.set_cursor; assert_eq!;fn next_boundary(self: &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 GraphemeCursor; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = new; assert_eq!; assert_eq!; assert_eq!;And an example that uses partial strings:
# use ; let s = "abcd"; let mut cursor = new; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn prev_boundary(self: &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 GraphemeCursor; let flags = "\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}"; let mut cursor = new; assert_eq!; assert_eq!; assert_eq!;And an example that uses partial strings (note the exact return is not guaranteed, and may be
PrevChunkorPreContextarbitrarily):# use ; let s = "abcd"; let mut cursor = new; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
impl Clone for GraphemeCursor
fn clone(self: &Self) -> GraphemeCursor
impl Debug for GraphemeCursor
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Freeze for GraphemeCursor
impl RefUnwindSafe for GraphemeCursor
impl Send for GraphemeCursor
impl Sync for GraphemeCursor
impl Unpin for GraphemeCursor
impl UnsafeUnpin for GraphemeCursor
impl UnwindSafe for GraphemeCursor
impl<T> Any for GraphemeCursor
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for GraphemeCursor
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for GraphemeCursor
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for GraphemeCursor
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for GraphemeCursor
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into for GraphemeCursor
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for GraphemeCursor
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for GraphemeCursor
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>