Skip to main content

truncate_utf8_to_max_bytes

Function truncate_utf8_to_max_bytes 

Source
pub fn truncate_utf8_to_max_bytes(
    text: &mut String,
    max_bytes: usize,
) -> Option<usize>
Expand description

Truncates a UTF-8 string in place to at most max_bytes, backing off to the nearest grapheme-cluster boundary via grapheme_safe_cutoff so multi-codepoint characters are never split.

Returns the new byte length when truncation happened, or None when the string already fit within max_bytes. When not even the first grapheme cluster fits, the string is truncated to empty.