abbrev.rs (600B)
1 #[inline] 2 pub fn floor_char_boundary(s: &str, index: usize) -> usize { 3 if index >= s.len() { 4 s.len() 5 } else { 6 let lower_bound = index.saturating_sub(3); 7 let new_index = s.as_bytes()[lower_bound..=index] 8 .iter() 9 .rposition(|b| is_utf8_char_boundary(*b)); 10 11 // SAFETY: we know that the character boundary will be within four bytes 12 unsafe { lower_bound + new_index.unwrap_unchecked() } 13 } 14 } 15 16 #[inline] 17 fn is_utf8_char_boundary(c: u8) -> bool { 18 // This is bit magic equivalent to: b < 128 || b >= 192 19 (c as i8) >= -0x40 20 }