Skip to content

Commit

Permalink
break in itemizer when encountering non-ascii characters
Browse files Browse the repository at this point in the history
  • Loading branch information
medicalwei authored and jacobmischka committed Oct 12, 2021
1 parent a422654 commit 89d1112
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/render/itemize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use std::str::CharIndices;
pub struct ItemizeIterator<'a> {
char_iter: CharIndices<'a>,
line: &'a str,
prev_char: Option<(usize, char)>,
}

impl<'a> ItemizeIterator<'a> {
pub fn new(line: &'a str) -> Self {
ItemizeIterator {
char_iter: line.char_indices(),
line,
prev_char: None,
}
}
}
Expand All @@ -21,13 +23,20 @@ impl<'a> Iterator for ItemizeIterator<'a> {
let mut start_index = None;

let end_index = loop {
if let Some((index, ch)) = self.char_iter.next() {
let cha = self.prev_char.or_else(|| self.char_iter.next());
self.prev_char = None;
if let Some((index, ch)) = cha {
let is_whitespace = ch.is_whitespace();
let is_ascii = ch.is_ascii();

if start_index.is_none() && !is_whitespace {
start_index = Some(index);
if !is_ascii {
break index + ch.len_utf8();
}
}
if start_index.is_some() && is_whitespace {
if start_index.is_some() && (is_whitespace || !is_ascii) {
self.prev_char = cha;
break index;
}
} else {
Expand Down

0 comments on commit 89d1112

Please sign in to comment.