xbp 10.39.0

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
//! Small shared text helpers for CLI output and annotation.

/// Truncate to at most `max` Unicode scalar values, appending `…` when shortened.
pub fn truncate_chars(s: &str, max: usize) -> String {
    let count = s.chars().count();
    if count <= max {
        return s.to_string();
    }
    let take = max.saturating_sub(1);
    format!("{}", s.chars().take(take).collect::<String>())
}

/// Flatten whitespace for stable matching / fingerprints.
pub fn normalize_whitespace(s: &str) -> String {
    s.split_whitespace().collect::<Vec<_>>().join(" ")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn truncate_leaves_short_strings() {
        assert_eq!(truncate_chars("hi", 10), "hi");
    }

    #[test]
    fn truncate_shortens_long_strings() {
        assert_eq!(truncate_chars("abcdef", 4), "abc…");
    }

    #[test]
    fn normalize_collapses_space() {
        assert_eq!(normalize_whitespace("  a   b\tc "), "a b c");
    }
}