#[inline]
pub(crate) fn floor_char_boundary(s: &str, index: usize) -> usize {
#[allow(clippy::unwrap_used)]
if index >= s.len() {
s.len()
} else {
let lower_bound = index.saturating_sub(3);
let new_index = s.as_bytes()[lower_bound..=index]
.iter()
.rposition(|b| (*b as i8) >= -0x40);
lower_bound + new_index.unwrap()
}
}
pub(crate) fn next_code_point<'a, I: Iterator<Item = &'a u8>>(bytes: &mut I) -> Option<u32> {
let x = *bytes.next()?;
if x < 128 {
return Some(x as u32);
}
let init = utf8_first_byte(x, 2);
let y = unsafe { *bytes.next().unwrap_unchecked() };
let mut ch = utf8_acc_cont_byte(init, y);
if x >= 0xE0 {
let z = *bytes.next()?;
let y_z = utf8_acc_cont_byte((y & CONT_MASK) as u32, z);
ch = (init << 12) | y_z;
if x >= 0xF0 {
let w = *bytes.next()?;
ch = ((init & 7) << 18) | utf8_acc_cont_byte(y_z, w);
}
}
Some(ch)
}
#[inline]
const fn utf8_acc_cont_byte(ch: u32, byte: u8) -> u32 {
(ch << 6) | (byte & CONT_MASK) as u32
}
#[inline]
const fn utf8_first_byte(byte: u8, width: u32) -> u32 {
(byte & (0x7F >> width)) as u32
}
const CONT_MASK: u8 = 0b0011_1111;