scanner/anchor: cache! before fetch

This commit is contained in:
Paul Stemmet 2021-09-09 12:26:53 +00:00
parent 39b33ccd7e
commit bca953716f
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
2 changed files with 18 additions and 8 deletions

View File

@ -1,5 +1,6 @@
use super::{
error::{ScanError, ScanResult as Result},
flag::Flags,
stats::MStats,
ALIAS, ANCHOR,
};
@ -8,6 +9,7 @@ use crate::token::Token;
/// Scan an anchor or alias from the underlying .buffer
/// returning the relevant Token
pub(in crate::scanner) fn scan_anchor<'de>(
opts: Flags,
buffer: &mut &'de str,
stats: &mut MStats,
kind: &AnchorKind,
@ -17,7 +19,7 @@ pub(in crate::scanner) fn scan_anchor<'de>(
// *anchor 'rest of the line'
// ^^^^^^
let anchor = take_while(buffer.as_bytes(), u8::is_ascii_alphanumeric);
let anchor = take_while(opts, buffer.as_bytes(), u8::is_ascii_alphanumeric)?;
let anchor = advance!(<- *buffer, :stats, anchor.len());
@ -33,6 +35,7 @@ pub(in crate::scanner) fn scan_anchor<'de>(
// There does not necessarily need to be a whitespace so we
// also check against a list of valid starting
// tokens
cache!(~buffer, 1, opts)?;
check!(~buffer
=> b' ' | b'\n' | b'?' | b',' | b']' | b'}' | b'%' | b'@' | b'`',
else ScanError::InvalidAnchorName
@ -73,7 +76,7 @@ impl AnchorKind
}
}
fn take_while<F>(b: &[u8], f: F) -> &[u8]
fn take_while<F>(opts: Flags, base: &[u8], f: F) -> Result<&[u8]>
where
F: Fn(&u8) -> bool,
{
@ -81,10 +84,12 @@ where
loop
{
match b.get(index)
let i = cache!(base, @index, 1, opts)?;
match base.get(index)
{
Some(b) if f(b) => index += 1,
_ => return &b[..index],
Some(b) if f(b) => index += i,
_ => return Ok(&base[..index]),
}
}
}

View File

@ -178,7 +178,7 @@ impl Scanner
},
// Is it an anchor or alias?
[ANCHOR, ..] | [ALIAS, ..] => self.fetch_anchor(base, tokens),
[ANCHOR, ..] | [ALIAS, ..] => self.fetch_anchor(opts, base, tokens),
// Is it a tag?
[TAG, ..] => self.fetch_tag(opts, base, tokens),
@ -353,7 +353,12 @@ impl Scanner
Ok(())
}
fn fetch_anchor<'de>(&mut self, base: &mut &'de str, tokens: &mut Tokens<'de>) -> Result<()>
fn fetch_anchor<'de>(
&mut self,
opts: Flags,
base: &mut &'de str,
tokens: &mut Tokens<'de>,
) -> Result<()>
{
let mut buffer = *base;
let mut stats = MStats::new();
@ -373,7 +378,7 @@ impl Scanner
self.save_key(!REQUIRED)?;
// Scan the token from the .buffer
let token = scan_anchor(&mut buffer, &mut stats, &kind)?;
let token = scan_anchor(opts, &mut buffer, &mut stats, &kind)?;
// A key may not start after an anchor (only before)
self.simple_key_allowed = false;