pub fn trim(s: &mut String) {
*s = s.trim().to_string();
}
pub fn trim_lowercase(s: &mut String) {
*s = s.trim().to_lowercase();
}
pub fn collapse_whitespace(s: &mut String) {
let mut result = String::with_capacity(s.len());
let mut prev_was_space = false;
for ch in s.chars() {
if ch.is_whitespace() {
if !prev_was_space {
result.push(' ');
}
prev_was_space = true;
} else {
result.push(ch);
prev_was_space = false;
}
}
*s = result;
}
pub fn strip_html(s: &mut String) {
*s = super::html::html_to_text(s);
}
pub fn truncate(s: &mut String, max_chars: usize) {
if let Some((idx, _)) = s.char_indices().nth(max_chars) {
s.truncate(idx);
}
}
pub fn normalize_email(s: &mut String) {
trim(s);
*s = s.to_lowercase();
if let Some((local, domain)) = s.split_once('@') {
let local = match local.split_once('+') {
Some((base, _)) => base,
None => local,
};
*s = format!("{local}@{domain}");
}
}