Skip to content

Commit

Permalink
Merge pull request #162 from embedded-graphics/cleanup
Browse files Browse the repository at this point in the history
Refactor recursive function into a loop
  • Loading branch information
bugadani authored Oct 10, 2023
2 parents 455d48e + ceffbe5 commit bbaf373
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/rendering/line_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,31 +422,30 @@ where
fn process_word<E: ElementHandler>(
&mut self,
handler: &mut E,
w: &str,
mut w: &str,
) -> Result<(), E::Error> {
match w.char_indices().find(|(_, c)| *c == SPEC_CHAR_NBSP) {
Some((space_pos, _)) => {
// If we have anything before the space...
if space_pos != 0 {
let word = unsafe {
// Safety: space_pos must be a character boundary
w.get_unchecked(0..space_pos)
};
handler.printed_characters(word, None)?;
}
loop {
let mut iter = w.char_indices();
match iter.find(|(_, c)| *c == SPEC_CHAR_NBSP) {
Some((space_pos, _)) => {
// If we have anything before the space...
if space_pos != 0 {
let word = unsafe {
// Safety: space_pos must be a character boundary
w.get_unchecked(0..space_pos)
};
handler.printed_characters(word, None)?;
}

handler.whitespace("\u{a0}", 1, self.spaces.consume(1))?;
handler.whitespace("\u{a0}", 1, self.spaces.consume(1))?;

// If we have anything after the space...
if let Some(word) = w.get(space_pos + SPEC_CHAR_NBSP.len_utf8()..) {
return self.process_word(handler, word);
// If we have anything after the space...
w = iter.as_str();
}
}

None => handler.printed_characters(w, None)?,
None => return handler.printed_characters(w, None),
}
}

Ok(())
}

fn should_draw_whitespace<E: ElementHandler>(&self, handler: &E) -> bool {
Expand Down

0 comments on commit bbaf373

Please sign in to comment.