Skip to content

Commit e989636

Browse files
committed
refactor: refactor parsing attr name
1 parent a4a0d06 commit e989636

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

markup_fmt/src/parser.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -676,26 +676,26 @@ impl<'s> Parser<'s> {
676676
chars.next();
677677
if let Some((_, '{')) = chars.next() {
678678
let end =
679-
start + self.parse_mustache_interpolation()?.0.len() + "{{}}".len() - 1;
679+
start + self.parse_mustache_interpolation()?.0.len() + "{{}}".len();
680680
Some((start, end))
681681
} else {
682682
None
683683
}
684684
}
685-
Some((_, c)) if is_attr_name_char(*c) => {
686-
self.chars.next().map(|(start, _)| (start, start))
687-
}
685+
Some((_, c)) if is_attr_name_char(*c) => self
686+
.chars
687+
.next()
688+
.map(|(start, c)| (start, start + c.len_utf8())),
688689
_ => None,
689690
}) else {
690691
return Err(self.emit_error(SyntaxErrorKind::ExpectAttrName));
691692
};
692693

693-
while let Some((i, c)) = self.chars.peek() {
694+
while let Some((_, c)) = self.chars.peek() {
694695
if is_attr_name_char(*c) && *c != '{' {
695-
end = *i;
696+
end += c.len_utf8();
696697
self.chars.next();
697698
} else if *c == '{' {
698-
let i = *i;
699699
let mut chars = self.chars.clone();
700700
chars.next();
701701
match chars.next() {
@@ -705,28 +705,30 @@ impl<'s> Parser<'s> {
705705
Some((_, '{')) => {
706706
end += self.parse_mustache_interpolation()?.0.len() + "{{}}".len();
707707
}
708-
_ => {
709-
end = i;
708+
Some((_, c)) => {
709+
end += c.len_utf8();
710710
self.chars.next();
711711
}
712+
None => break,
712713
}
713714
} else {
714715
break;
715716
}
716717
}
717718

718-
unsafe { Ok(self.source.get_unchecked(start..=end)) }
719+
unsafe { Ok(self.source.get_unchecked(start..end)) }
719720
} else {
720-
let Some((start, _)) = self.chars.next_if(|(_, c)| is_attr_name_char(*c)) else {
721+
let Some((start, start_char)) = self.chars.next_if(|(_, c)| is_attr_name_char(*c))
722+
else {
721723
return Err(self.emit_error(SyntaxErrorKind::ExpectAttrName));
722724
};
723-
let mut end = start;
725+
let mut end = start + start_char.len_utf8();
724726

725-
while let Some((i, _)) = self.chars.next_if(|(_, c)| is_attr_name_char(*c)) {
726-
end = i;
727+
while let Some((_, c)) = self.chars.next_if(|(_, c)| is_attr_name_char(*c)) {
728+
end += c.len_utf8();
727729
}
728730

729-
unsafe { Ok(self.source.get_unchecked(start..=end)) }
731+
unsafe { Ok(self.source.get_unchecked(start..end)) }
730732
}
731733
}
732734

0 commit comments

Comments
 (0)