Avoid too many lookaheads in scan_plain_scalar.

This commit is contained in:
Ethiraric 2024-04-18 23:34:38 +02:00
parent d27bae9fa5
commit 693cc19042

View file

@ -2169,17 +2169,24 @@ impl<T: Input> Scanner<T> {
// We can unroll the first iteration of the loop. // We can unroll the first iteration of the loop.
string.push(self.input.peek()); string.push(self.input.peek());
self.skip_non_blank(); self.skip_non_blank();
self.input.lookahead(2); string.reserve(self.input.bufmaxlen());
// Add content non-blank characters to the scalar. // Add content non-blank characters to the scalar.
while !is_blank_or_breakz(self.input.peek()) { let mut end = false;
if !self.next_can_be_plain_scalar() { while !end {
break; // Fill the buffer once and process all characters in the buffer until the next
// fetch. Note that `next_can_be_plain_scalar` needs 2 lookahead characters,
// hence the `for` loop looping `self.input.bufmaxlen() - 1` times.
self.input.lookahead(self.input.bufmaxlen());
for _ in 0..self.input.bufmaxlen() - 1 {
if is_blank_or_breakz(self.input.peek()) || !self.next_can_be_plain_scalar()
{
end = true;
break;
}
string.push(self.input.peek());
self.skip_non_blank();
} }
string.push(self.input.peek());
self.skip_non_blank();
self.input.lookahead(2);
} }
} }