pub struct GraphemeCursor { /* fields omitted */ }
Cursor-based segmenter for grapheme clusters.
implGraphemeCursor
[src]pub fnnew(offset: usize, len: usize, is_extended: bool) ->GraphemeCursor
[src]Create a new cursor. The string and initial offset are given at creationtime, but the contents of the string are not. Theis_extended
parametercontrols whether extended grapheme clusters are selected.
Theoffset
parameter must be on a codepoint boundary.
lets="हिन्दी";letmutlegacy=GraphemeCursor::new(0,s.len(),false);assert_eq!(legacy.next_boundary(s,0),Ok(Some("ह".len())));letmutextended=GraphemeCursor::new(0,s.len(),true);assert_eq!(extended.next_boundary(s,0),Ok(Some("हि".len())));
pub fnset_cursor(&mut self, offset: usize)
[src]Set the cursor to a new location in the same string.
lets="abcd";letmutcursor=GraphemeCursor::new(0,s.len(),false);assert_eq!(cursor.cur_cursor(),0);cursor.set_cursor(2);assert_eq!(cursor.cur_cursor(),2);
pub fncur_cursor(&self) -> usize
[src]The current offset of the cursor. Equal to the last value provided tonew()
orset_cursor()
, or returned fromnext_boundary()
orprev_boundary()
.
// Two flags (🇷🇸🇮🇴), each flag is two RIS codepoints, each RIS is 4 bytes.letflags="\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}";letmutcursor=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);
pub fnprovide_context(&mut self, chunk: &str, chunk_start: usize)
[src]Provide additional pre-context when it is needed to decide a boundary.The end of the chunk must coincide with the value given in theGraphemeIncomplete::PreContext
request.
letflags="\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}";letmutcursor=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-contextcursor.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));
pub fnis_boundary(
&mut self,
chunk: &str,
chunk_start: usize
) ->Result<bool,GraphemeIncomplete>
[src]Determine whether the current cursor location is a grapheme cluster boundary.Only a part of the string need be supplied. Ifchunk_start
is nonzero orthe length ofchunk
is not equal tolen
on creation, then this methodmay returnGraphemeIncomplete::PreContext
. The caller should thencallprovide_context
with the requested chunk, then retry calling thismethod.
For partial chunks, if the cursor is not at the beginning or end of thestring, 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 providescontent for a given slice, all further chunks covering that slice must havethe same content for it).
letflags="\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}";letmutcursor=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));
pub fnnext_boundary(
&mut self,
chunk: &str,
chunk_start: usize
) ->Result<Option<usize>,GraphemeIncomplete>
[src]Find the next boundary after the current cursor position. Only a part ofthe string need be supplied. If the chunk is incomplete, then thismethod might returnGraphemeIncomplete::PreContext
orGraphemeIncomplete::NextChunk
. In the former case, the caller shouldcallprovide_context
with the requested chunk, then retry. In thelatter case, the caller should provide the chunk following the onegiven, then retry.
Seeis_boundary
for expectations on the provided chunk.
letflags="\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}";letmutcursor=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:
lets="abcd";letmutcursor=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));
pub fnprev_boundary(
&mut self,
chunk: &str,
chunk_start: usize
) ->Result<Option<usize>,GraphemeIncomplete>
[src]Find the previous boundary after the current cursor position. Only a partof the string need be supplied. If the chunk is incomplete, then thismethod might returnGraphemeIncomplete::PreContext
orGraphemeIncomplete::PrevChunk
. In the former case, the caller shouldcallprovide_context
with the requested chunk, then retry. In thelatter case, the caller should provide the chunk preceding the onegiven, then retry.
Seeis_boundary
for expectations on the provided chunk.
letflags="\u{1F1F7}\u{1F1F8}\u{1F1EE}\u{1F1F4}";letmutcursor=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 notguaranteed, and may bePrevChunk
orPreContext
arbitrarily):
lets="abcd";letmutcursor=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));
implClone forGraphemeCursor
[src]fnclone(&self) ->GraphemeCursor
[src]pub fnclone_from(&mut self, source: &Self)
1.0.0[src]implDebug forGraphemeCursor
[src]impl<T>Any for Twhere
T: 'static + ?Sized,
[src]impl<T>Borrow<T> for Twhere
T: ?Sized,
[src]impl<T>BorrowMut<T> for Twhere
T: ?Sized,
[src]pub fnborrow_mut(&mut self) -> &mut T
[src]impl<T>From<T> for T
[src]impl<T, U>Into<U> for Twhere
U:From<T>,
[src]impl<T, U>TryFrom<U> for Twhere
U:Into<T>,
[src]typeError =Infallible
The type returned in the event of a conversion error.
pub fntry_from(value: U) ->Result<T, <T asTryFrom<U>>::Error>
[src]impl<T, U>TryInto<U> for Twhere
U:TryFrom<T>,
[src]