fn is_word_char(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'_'
}
fn match_constrained_boundary(b: u8) -> bool {
!is_word_char(b) && !matches!(b, b':' | b';' | b'}')
}
pub(super) fn check_constrained_opening_boundary(
pos: usize,
input: &[u8],
outer_delimiter: Option<u8>,
marker: u8,
) -> bool {
if pos == 0 {
return outer_delimiter.is_none_or(|d| !is_word_char(d));
}
match input.get(pos - 1) {
None => true,
Some(b'&') if marker == b'#' => false,
Some(&b) if b.is_ascii() => match_constrained_boundary(b),
Some(_) => char_ending_at(input, pos).is_none_or(|c| !c.is_alphanumeric()),
}
}
fn char_ending_at(input: &[u8], end: usize) -> Option<char> {
if end == 0 || end > input.len() {
return None;
}
let mut start = end - 1;
while start > 0
&& input
.get(start)
.is_some_and(|&b| b & 0b1100_0000 == 0b1000_0000)
{
start -= 1;
}
std::str::from_utf8(input.get(start..end)?)
.ok()?
.chars()
.next()
}
pub(super) fn check_constrained_closing_at_end(
end: usize,
input_len: usize,
outer_delimiter: Option<u8>,
) -> bool {
end < input_len || outer_delimiter.is_none_or(|d| !is_word_char(d))
}