pub fn split_at_width(s: &str, max_cols: u16) -> (&str, &str)Expand description
Splits s at the byte index where its display width reaches max_cols.
Splits on a whole-character boundary; a character that would push the
total over max_cols is left in the second half along with the rest of
s. Returns (prefix, rest) such that width(prefix) <= max_cols and
prefix is the longest prefix of s for which that holds.
Each candidate prefix is measured with width_usize (the same
UnicodeWidthStr logic behind the postcondition above), not a sum of
individual char_widths: UnicodeWidthStr measures some multi-codepoint
clusters (emoji presentation/ZWJ/modifier sequences) as a unit whose width
differs from the sum of its parts, and per-char summing would let such a
cluster violate the postcondition in either direction.
That re-measurement is bounded to a trailing window of the last CLUSTER_LOOKBACK
characters rather than the whole prefix seen so far:
UnicodeWidthStr’s boundary effects never reach further back than a
handful of codepoints (variation selectors, a single ZWJ join, an emoji
modifier, or a short flag/tag run), so a bounded window reproduces the
same result as re-measuring from byte 0 while keeping this function
linear in s’s length instead of quadratic.
§Examples
use retroglyph_core::text::split_at_width;
assert_eq!(split_at_width("hello world", 5), ("hello", " world"));
assert_eq!(split_at_width("hi", 10), ("hi", ""));