Skip to content

Commit

Permalink
Simplify longest_fitting_substr
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Sep 4, 2023
1 parent ee630ab commit 37daf58
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/rendering/line_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,20 @@ where
&mut self,
handler: &E,
w: &'a str,
) -> (&'a str, Option<&'a str>) {
) -> (&'a str, &'a str) {
let mut width = 0;
for (idx, c) in w.char_indices() {
let char_width = handler.measure(unsafe {
// SAFETY: we are working on character boundaries
w.get_unchecked(idx..idx + c.len_utf8())
});
if !self.cursor.fits_in_line(width + char_width) {
debug_assert!(w.is_char_boundary(idx));
return (
unsafe {
// SAFETY: we are working on character boundaries
w.get_unchecked(0..idx)
},
w.get(idx..),
);
return w.split_at(idx);
}
width += char_width;
}

(w, None)
(w, "")
}

fn next_word_fits<E: ElementHandler>(&self, space_width: i32, handler: &E) -> bool {
Expand Down Expand Up @@ -342,7 +335,7 @@ where
let (word, remainder) = if self.move_cursor(width.saturating_as()).is_ok() {
// We can move the cursor here since `process_word()`
// doesn't depend on it.
(w, None)
(w, "")
} else if self.empty {
// This word does not fit into an empty line. Find longest part
// that fits and push the rest to the next line.
Expand All @@ -366,7 +359,7 @@ where
self.process_word(handler, word)?;
}

if remainder.is_some() {
if !remainder.is_empty() {
// Consume what was printed.
self.plugin.consume_partial(word.len());
return Ok(LineEndType::LineBreak);
Expand Down

0 comments on commit 37daf58

Please sign in to comment.