pub fn truncate_chars(s: &str, max_chars: usize) -> &str {
match s.char_indices().nth(max_chars) {
Some((byte_idx, _)) => &s[..byte_idx],
None => s,
}
}
pub fn truncate_bytes(s: &str, max_bytes: usize) -> &str {
if s.len() <= max_bytes {
return s;
}
let mut end = max_bytes;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
pub fn find_ci(hay: &str, needle: &str) -> Option<usize> {
find_ci_from(hay, needle, 0)
}
pub fn find_ci_from(hay: &str, needle: &str, from: usize) -> Option<usize> {
if needle.is_empty() || from > hay.len() {
return None;
}
let needle_lower: Vec<char> = needle.chars().flat_map(char::to_lowercase).collect();
if needle_lower.is_empty() {
return None;
}
for (idx, _) in hay.char_indices() {
if idx < from {
continue;
}
let mut hay_chars = hay[idx..].chars().flat_map(char::to_lowercase);
let mut matched = true;
for want in &needle_lower {
match hay_chars.next() {
Some(got) if got == *want => {}
_ => {
matched = false;
break;
}
}
}
if matched {
return Some(idx);
}
}
None
}
#[inline]
pub fn char_len(s: &str) -> usize {
s.chars().count()
}
#[inline]
pub fn is_word_char(c: char) -> bool {
c.is_alphanumeric()
|| (('\u{0900}'..='\u{0DFF}').contains(&c) && !matches!(c, '\u{0964}' | '\u{0965}'))
|| matches!(c, '\u{200C}' | '\u{200D}')
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn indic_conjuncts_stay_whole_words() {
let split = |s: &str| -> Vec<String> {
s.split(|c: char| !is_word_char(c))
.filter(|t| !t.is_empty())
.map(str::to_string)
.collect()
};
assert_eq!(split("क्या है"), vec!["क्या", "है"]);
assert_eq!(split("प्रधानमंत्री।"), vec!["प्रधानमंत्री"]);
assert_eq!(split("हिन्दी, தமிழ்"), vec!["हिन्दी", "தமிழ்"]);
assert_eq!(split("rust-lang 2.0"), vec!["rust", "lang", "2", "0"]);
}
#[test]
fn truncate_chars_never_panics_on_devanagari() {
let cases = [
"क".repeat(80),
format!("5{}", "क".repeat(80)),
format!("IP{}", "क".repeat(80)),
"🔥".repeat(40),
format!("{{{}", "क".repeat(200)),
"मुझे भारत और पाकिस्तान के बीच विश्व कप मुकाबले की पूरी जानकारी चाहिए".to_string(),
];
for c in &cases {
let out = truncate_chars(c, 120);
assert!(out.chars().count() <= 120);
assert!(c.starts_with(out), "truncation must be a prefix");
}
}
#[test]
fn truncate_chars_is_noop_when_short() {
assert_eq!(truncate_chars("hello", 120), "hello");
assert_eq!(truncate_chars("", 10), "");
}
#[test]
fn truncate_bytes_backs_up_to_boundary() {
let s = format!("5{}", "क".repeat(80)); let out = truncate_bytes(&s, 120);
assert!(out.len() <= 120);
assert!(s.starts_with(out));
assert!(out.len() < 120);
}
#[test]
fn find_ci_handles_length_changing_lowercase() {
let hay = "aİb<THINK>x</THINK>";
let idx = find_ci(hay, "<think>").expect("should find");
assert!(hay.is_char_boundary(idx));
assert_eq!(&hay[idx..idx + 7], "<THINK>");
}
#[test]
fn find_ci_from_respects_start_offset() {
let hay = "<think>a</think><think>b</think>";
let first = find_ci(hay, "<think>").unwrap();
let close = find_ci_from(hay, "</think>", first + 7).unwrap();
assert!(close > first);
}
#[test]
fn find_ci_returns_none_when_absent() {
assert_eq!(find_ci("hello world", "<think>"), None);
assert_eq!(find_ci("", "x"), None);
assert_eq!(find_ci("abc", ""), None);
}
}