use std::cell::RefCell;
use super::Native;
use super::deps::Deps;
use crate::Engine;
use crate::token::{Token, TokenList};
pub(super) const IFBLANK: Native = Native {
name: "ifblank",
fingerprints: &[(20, 0xcd65_4780_b191_5887)],
executes: false,
run: run_ifblank,
};
pub(super) const NOTBLANK: Native = Native {
name: "notblank",
fingerprints: &[(20, 0x973b_8576_eeee_4f45)],
executes: false,
run: run_notblank,
};
const NAMES: &[&str] = &[
"@gobble",
"@firstoftwo",
"@secondoftwo",
"expandafter",
"ifx",
"relax",
"detokenize",
"else",
"fi",
];
const KNOWN: &[u64] = &[0xff95_f8cb_5f58_c485];
thread_local! {
static DEPS: RefCell<Deps> = RefCell::new(Deps::new("ifblank", NAMES, KNOWN));
}
fn run_ifblank(e: &mut Engine, args: &[TokenList]) -> bool {
run(e, args, true)
}
fn run_notblank(e: &mut Engine, args: &[TokenList]) -> bool {
run(e, args, false)
}
fn run(e: &mut Engine, args: &[TokenList], if_blank: bool) -> bool {
if !DEPS.with(|d| d.borrow_mut().check(e)) {
return false;
}
let blank = args[0].iter().all(|&t| t == Token::SPACE);
let name: &[u8] = if blank == if_blank {
b"@firstoftwo"
} else {
b"@secondoftwo"
};
let Some(cs) = e.hash.lookup(name) else {
return false;
};
e.native_insert(vec![Token::cs(cs)]);
true
}