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
impl LineBreakLeafIter
Sourcepub fn new(s: &str, ix: usize) -> LineBreakLeafIter
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?
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
}
Sourcepub fn next(&mut self, s: &str) -> (usize, bool)
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?
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
impl Clone for LineBreakLeafIter
Source§fn clone(&self) -> LineBreakLeafIter
fn clone(&self) -> LineBreakLeafIter
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Default for LineBreakLeafIter
impl Default for LineBreakLeafIter
Source§fn default() -> LineBreakLeafIter
fn default() -> LineBreakLeafIter
impl Copy for LineBreakLeafIter
Auto Trait Implementations§
impl Freeze for LineBreakLeafIter
impl RefUnwindSafe for LineBreakLeafIter
impl Send for LineBreakLeafIter
impl Sync for LineBreakLeafIter
impl Unpin for LineBreakLeafIter
impl UnwindSafe for LineBreakLeafIter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)