use memchr::memchr;
#[inline]
pub fn is_valid_number_fast(s: &str) -> bool {
if s.is_empty() {
return false;
}
let bytes = s.as_bytes();
let mut has_digit = false;
let mut has_dot = false;
for &b in bytes {
match b {
b'0'..=b'9' => has_digit = true,
b'.' => {
if has_dot {
return false; }
has_dot = true;
}
b'-' if bytes.len() == 1 => return false, b'-' => {} _ => return false, }
}
has_digit }
#[inline]
pub fn contains_latex_special_simd(text: &str) -> bool {
let bytes = text.as_bytes();
memchr(b' ', bytes).is_some()
|| memchr(b'#', bytes).is_some()
|| memchr(b'$', bytes).is_some()
|| memchr(b'%', bytes).is_some()
|| memchr(b'&', bytes).is_some()
|| memchr(b'_', bytes).is_some()
|| memchr(b'{', bytes).is_some()
|| memchr(b'}', bytes).is_some()
|| memchr(b'~', bytes).is_some()
|| memchr(b'^', bytes).is_some()
|| memchr(b'\\', bytes).is_some()
}
#[inline]
pub fn escape_latex_special_chars(text: &str, buffer: &mut String) -> bool {
if !contains_latex_special_simd(text) {
buffer.push_str(text);
return false;
}
for ch in text.chars() {
match ch {
' ' | '#' | '$' | '%' | '&' | '_' | '{' | '}' | '~' | '^' | '\\' => {
buffer.push('\\');
buffer.push(ch);
}
_ => buffer.push(ch),
}
}
true
}
#[inline]
pub fn needs_latex_protection(text: &str) -> bool {
if text.is_empty() {
return true;
}
if memchr(b' ', text.as_bytes()).is_some() {
return true;
}
contains_latex_special_simd(text)
}