swiftide_core/util.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//! Utility functions for Swiftide
/// Safely truncates a string to a maximum number of characters.
///
/// Respects utf8 character boundaries.
pub fn safe_truncate_utf8(s: impl AsRef<str>, max_chars: usize) -> String {
s.as_ref().chars().take(max_chars).collect()
}
/// Debug print a long string by truncating to n characters
///
/// # Example
///
/// ```
/// # use swiftide_core::util::debug_long_utf8;
/// let s = debug_long_utf8("🦀".repeat(10), 3);
///
/// assert_eq!(s, "🦀🦀🦀 (10)");
/// ```
pub fn debug_long_utf8(s: impl AsRef<str>, max_chars: usize) -> String {
let trunc = safe_truncate_utf8(&s, max_chars);
format!("{} ({})", trunc, s.as_ref().chars().count())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_safe_truncate_str_with_utf8_char_boundary() {
let s = "🦀".repeat(101);
// Single char
assert_eq!(safe_truncate_utf8(&s, 100).chars().count(), 100);
// With invalid char boundary
let s = "Jürgen".repeat(100);
assert_eq!(safe_truncate_utf8(&s, 100).chars().count(), 100);
}
}