Struct StrCursor

Source
pub struct StrCursor<'a> { /* private fields */ }
Expand description

This type represents a cursor into a string slice; that is, in addition to having a beginning and end, it also has a current position between those two. This position can be seeked left and right within those bounds.

Note: the cursor may validly be positioned at the end of the string. That is, in a position where there are no code points or grapheme clusters to the right of the cursor, and the entire contents of the string is to the left of the cursor.

The main reason for this is that sometimes, you want the ability to do things like “advance a character”, and the existing APIs for this can be somewhat verbose.

In addition, unstable support for grapheme clusters is exposed by the standard library, which conflicts with the stable support provided by the unicode-segmentation crate, which makes doing “the right thing” painful. StrCursor exposes grapheme clusters by default, and makes them cleaner to work with.

The cursor guarantees the following at all times:

  • The cursor position cannot be outside of the original string slice it was constructed with.
  • The cursor position cannot lie between Unicode code points, meaning that you cannot generate an invalid string slice from a cursor.
  • If the code point-specific methods are not used, the cursor will always lie between grapheme clusters.

This last point is somewhat important: the cursor is designed to favour operating on grapheme clusters, rather than code points. If you misalign the cursor with respect to grapheme clusters, the behaviour of methods that deal with grapheme clusters is officially undefined, but is generally well-behaved.

The methods that operate on the cursor will either return a fresh Option<StrCursor> (depending on whether the seek operation is valid or not), or mutate the existing cursor (in which case, they will panic if the seek operation is not valid).

Implementations§

Source§

impl<'a> StrCursor<'a>

Source

pub fn new_at_start(s: &'a str) -> StrCursor<'a>

Create a new cursor at the start of s.

Source

pub fn new_at_end(s: &'a str) -> StrCursor<'a>

Create a new cursor past at the end of s.

Source

pub fn new_at_left_of_byte_pos(s: &'a str, byte_pos: usize) -> StrCursor<'a>

Create a new cursor at the first grapheme cluster which begins at or to the left of the given byte position.

Source

pub fn new_at_right_of_byte_pos(s: &'a str, byte_pos: usize) -> StrCursor<'a>

Create a new cursor at the first grapheme cluster which begins at or to the right of the given byte position.

Source

pub fn new_at_cp_left_of_byte_pos(s: &'a str, byte_pos: usize) -> StrCursor<'a>

Create a new cursor at the first code point which begins at or to the left of the given byte position.

§Note

Where possible, you should prefer new_at_left_of_byte_pos.

Source

pub fn new_at_cp_right_of_byte_pos(s: &'a str, byte_pos: usize) -> StrCursor<'a>

Create a new cursor at the first code point which begins at or to the right of the given byte position.

§Note

Where possible, you should prefer new_at_right_of_byte_pos.

Source

pub fn at_prev(self) -> Option<StrCursor<'a>>

Returns a new cursor at the beginning of the previous grapheme cluster, or None if the cursor is currently positioned at the beginning of the string.

Source

pub fn at_next(self) -> Option<StrCursor<'a>>

Returns a new cursor at the beginning of the next grapheme cluster, or None if the cursor is currently positioned at the end of the string.

Source

pub fn at_prev_cp(self) -> Option<StrCursor<'a>>

Returns a new cursor at the beginning of the previous code point, or None if the cursor is currently positioned at the beginning of the string.

§Note

Where possible, you should prefer at_prev.

Source

pub fn at_next_cp(self) -> Option<StrCursor<'a>>

Returns a new cursor at the beginning of the next code point, or None if the cursor is currently positioned at the end of the string.

§Note

Where possible, you should prefer at_next.

Source

pub fn seek_prev(&mut self)

Seeks the cursor to the beginning of the previous grapheme cluster.

§Panics

If the cursor is currently at the start of the string, then this function will panic.

Source

pub fn seek_next(&mut self)

Seeks the cursor to the beginning of the next grapheme cluster.

§Panics

If the cursor is currently at the end of the string, then this function will panic.

Source

pub fn seek_prev_cp(&mut self)

Seeks the cursor to the beginning of the previous code point.

§Panics

If the cursor is currently at the start of the string, then this function will panic.

§Note

Where possible, you should prefer seek_prev.

Source

pub fn seek_next_cp(&mut self)

Seeks the cursor to the beginning of the next code point.

§Panics

If the cursor is currently at the end of the string, then this function will panic.

§Note

Where possible, you should prefer seek_next.

Source

pub fn prev(self) -> Option<(&'a Gc, StrCursor<'a>)>

Returns both the previous grapheme cluster and the cursor having seeked before it.

This may be more efficient than doing both operations individually.

Source

pub fn prev_cp(self) -> Option<(char, StrCursor<'a>)>

Returns both the previous code point and the cursor having seeked before it.

This may be more efficient than doing both operations individually.

§Note

Where possible, you should prefer prev.

Source

pub fn next(self) -> Option<(&'a Gc, StrCursor<'a>)>

Returns both the next grapheme cluster and the cursor having seeked past it.

This may be more efficient than doing both operations individually.

Source

pub fn next_cp(self) -> Option<(char, StrCursor<'a>)>

Returns both the next code point and the cursor having seeked past it.

This may be more efficient than doing both operations individually.

§Note

Where possible, you should prefer next.

Source

pub fn before(&self) -> Option<&'a Gc>

Returns the grapheme cluster immediately to the left of the cursor, or None is the cursor is at the start of the string.

Source

pub fn after(&self) -> Option<&'a Gc>

Returns the grapheme cluster immediately to the right of the cursor, or None is the cursor is at the end of the string.

Source

pub fn slice_before(&self) -> &'a str

Returns the contents of the string to the left of the cursor.

Source

pub fn slice_after(&self) -> &'a str

Returns the contents of the string to the right of the cursor.

Source

pub fn slice_between(&self, until: StrCursor<'a>) -> Option<&'a str>

Returns the contents of the string between this cursor and another cursor.

Returns None if the cursors are from different strings (even different subsets of the same string).

Source

pub fn cp_before(&self) -> Option<char>

Returns the code point immediately to the left of the cursor, or None is the cursor is at the start of the string.

Source

pub fn cp_after(&self) -> Option<char>

Returns the code point immediately to the right of the cursor, or None is the cursor is at the end of the string.

Source

pub fn slice_all(&self) -> &'a str

Returns the entire string slice behind the cursor.

Source

pub fn byte_pos(&self) -> usize

Returns the cursor’s current position within the string as the number of UTF-8 code units from the beginning of the string.

Source

pub unsafe fn unsafe_seek_left(&mut self, bytes: usize)

Seeks exactly bytes left, without performing any bounds or validity checks.

Source

pub unsafe fn unsafe_seek_right(&mut self, bytes: usize)

Seeks exactly bytes right, without performing any bounds or validity checks.

Source

pub unsafe fn unsafe_set_at(&mut self, s: &'a str)

Seeks to the start of s, without performing any bounds or validity checks.

Trait Implementations§

Source§

impl<'a> Clone for StrCursor<'a>

Source§

fn clone(&self) -> StrCursor<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for StrCursor<'a>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> Hash for StrCursor<'a>

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> PartialEq for StrCursor<'a>

Source§

fn eq(&self, other: &StrCursor<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &StrCursor<'a>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for StrCursor<'a>

Source§

fn partial_cmp(&self, other: &StrCursor<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Copy for StrCursor<'a>

Source§

impl<'a> Eq for StrCursor<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for StrCursor<'a>

§

impl<'a> RefUnwindSafe for StrCursor<'a>

§

impl<'a> !Send for StrCursor<'a>

§

impl<'a> !Sync for StrCursor<'a>

§

impl<'a> Unpin for StrCursor<'a>

§

impl<'a> UnwindSafe for StrCursor<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.