xi_unicode

Struct LineBreakLeafIter

Source
pub struct LineBreakLeafIter { /* private fields */ }
Expand description

A struct useful for computing line breaks in a rope or other non-contiguous string representation. This is a trickier problem than iterating in a string for a few reasons, the trickiest of which is that in the general case, line breaks require an indeterminate amount of look-behind.

This is something of an “expert-level” interface, and should only be used if the caller is prepared to respect all the invariants. Otherwise, you might get inconsistent breaks depending on start position and leaf boundaries.

Implementations§

Source§

impl LineBreakLeafIter

Source

pub fn new(s: &str, ix: usize) -> LineBreakLeafIter

Create a new line break iterator suitable for leaves in a rope. Precondition: ix is at a code point boundary within s.

Examples found in repository?
examples/runtestdata.rs (line 57)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
fn check_lb(s: &str) -> bool {
    let breaks = LineBreakIterator::new(s).collect::<Vec<_>>();
    for i in 0..breaks.len() - 1 {
        let mut cursor = LineBreakLeafIter::new(s, breaks[i].0);
        for &bk in &breaks[i + 1..] {
            let mut next = cursor.next(s);
            if next.0 == s.len() {
                next = (s.len(), true);
            }
            if next != bk {
                println!("failed case: \"{}\"", quote_str(s));
                println!("expected {:?} actual {:?}", bk, next);
                return false;
            }
        }
    }
    true
}
Source

pub fn next(&mut self, s: &str) -> (usize, bool)

Return break pos and whether it’s a hard break. Note: hard break indication may go away, this may not be useful in actual application. If end of leaf is found, return leaf’s len. This does not indicate a break, as that requires at least one more codepoint of context. If it is a break, then subsequent next call will return an offset of 0. EOT is always a break, so in the EOT case it’s up to the caller to figure that out.

For consistent results, always supply same s until end of leaf is reached (and initially this should be the same as in the new call).

Examples found in repository?
examples/runtestdata.rs (line 59)
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
fn check_lb(s: &str) -> bool {
    let breaks = LineBreakIterator::new(s).collect::<Vec<_>>();
    for i in 0..breaks.len() - 1 {
        let mut cursor = LineBreakLeafIter::new(s, breaks[i].0);
        for &bk in &breaks[i + 1..] {
            let mut next = cursor.next(s);
            if next.0 == s.len() {
                next = (s.len(), true);
            }
            if next != bk {
                println!("failed case: \"{}\"", quote_str(s));
                println!("expected {:?} actual {:?}", bk, next);
                return false;
            }
        }
    }
    true
}

Trait Implementations§

Source§

impl Clone for LineBreakLeafIter

Source§

fn clone(&self) -> LineBreakLeafIter

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 Default for LineBreakLeafIter

Source§

fn default() -> LineBreakLeafIter

Returns the “default value” for a type. Read more
Source§

impl Copy for LineBreakLeafIter

Auto Trait Implementations§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.