use std::borrow::Cow;
pub fn strip_control_codes(text: &str) -> Cow<'_, str> {
if !text
.chars()
.any(|c| matches!(c as u32, 7 | 8 | 11 | 12 | 13))
{
return Cow::Borrowed(text);
}
Cow::Owned(
text.chars()
.filter(|c| !matches!(*c as u32, 7 | 8 | 11 | 12 | 13))
.collect(),
)
}
pub fn char_to_byte_index(s: &str, char_idx: usize) -> usize {
s.char_indices()
.nth(char_idx)
.map(|(i, _)| i)
.unwrap_or(s.len())
}
pub fn char_slice(s: &str, start: usize, end: usize) -> &str {
let byte_start = char_to_byte_index(s, start);
let byte_end = char_to_byte_index(s, end);
&s[byte_start..byte_end]
}
pub fn gcd(mut a: usize, mut b: usize) -> usize {
while b != 0 {
let t = b;
b = a % b;
a = t;
}
a
}