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