use crate::Sealed;
pub trait RoundCharBoundaryExt: Sealed {
fn floor_char_boundary_ib(&self, index: usize) -> usize;
fn ceil_char_boundary_ib(&self, index: usize) -> usize;
}
impl RoundCharBoundaryExt for str {
#[inline]
fn floor_char_boundary_ib(&self, index: usize) -> usize {
if index >= self.len() {
self.len()
} else {
let lower_bound = index.saturating_sub(3);
let new_index = self.as_bytes()[lower_bound..=index].iter().rposition(|&b| {
(b as i8) >= -0x40
});
unsafe { lower_bound + new_index.unwrap_unchecked() }
}
}
#[inline]
fn ceil_char_boundary_ib(&self, index: usize) -> usize {
if index > self.len() {
self.len()
} else {
let upper_bound = Ord::min(index + 4, self.len());
self.as_bytes()[index..upper_bound]
.iter()
.position(|&b| {
(b as i8) >= -0x40
})
.map_or(upper_bound, |pos| pos + index)
}
}
}