block: count indent in chars instead of bytes

This commit is contained in:
Noah Hellman 2023-02-01 20:24:52 +01:00
parent ca7f3c7e89
commit 59be7070de
2 changed files with 18 additions and 12 deletions

View file

@ -313,13 +313,15 @@ impl<'s> TreeParser<'s> {
lines.iter_mut().skip(1).for_each(|sp| {
let src = sp.of(self.src);
let src_t = src.trim();
let spaces = src.len() - src.trim_start().len();
let spaces = src.chars().take_while(|c| c.is_whitespace()).count();
let skip = match k {
Kind::Blockquote => {
if src_t == ">" {
spaces + 1
} else if src_t.starts_with("> ") {
spaces + "> ".len()
} else if src_t.starts_with('>')
&& src_t.chars().nth(1).map_or(false, char::is_whitespace)
{
spaces + 1 + usize::from(src_t.len() > 1)
} else {
0
}
@ -329,8 +331,8 @@ impl<'s> TreeParser<'s> {
| Kind::Fenced { indent, .. } => spaces.min(*indent),
_ => panic!("non-container {:?}", k),
};
let len = sp.len() - usize::from(sp.of(self.src).ends_with('\n'));
*sp = sp.skip(skip.min(len));
let count = sp.of(self.src).chars().take_while(|c| *c != '\n').count();
*sp = sp.skip_chars(skip.min(count), self.src);
});
if let ListItem(ty) = c {
@ -571,18 +573,17 @@ struct IdentifiedBlock {
impl IdentifiedBlock {
fn new(line: &str) -> Self {
let indent = line
.chars()
let mut chars = line.chars();
let indent = chars
.clone()
.take_while(|c| *c != '\n' && c.is_whitespace())
.map(char::len_utf8)
.sum();
let line = &line[indent..];
.count();
(&mut chars).take(indent).last();
let line = chars.as_str();
let line_t = line.trim_end();
let l = line.len();
let lt = line_t.len();
let mut chars = line.chars();
let first = if let Some(c) = chars.next() {
c
} else {

View file

@ -89,6 +89,11 @@ impl Span {
&s[self.start()..self.end()]
}
pub fn skip_chars(self, n: usize, s: &str) -> Self {
let n_bytes: usize = self.of(s).chars().take(n).map(char::len_utf8).sum();
Self::new(self.start() + n_bytes, self.end())
}
pub fn trim_start_matches<P: FnMut(char) -> bool>(self, s: &str, pat: P) -> Self {
Self::from_slice(s, self.of(s).trim_start_matches(pat))
}