Function fyi_msg::length_width

source ·
pub fn length_width(bytes: &[u8], stop: usize) -> usize
Available on crate feature fitted only.
Expand description

§Length Width.

Return the maximum byte length for the slice that fits a given display width, such that slice[0..len] will be a valid substring likely to fit.

This method accepts raw bytes for performance reasons, but is Unicode-safe; the return value will always be a valid char boundary. In cases where the input contains invalid UTF-8, only the leading ASCII bytes will be considered/counted.

See the documentation for width for more information.

This requires the fitted crate feature.

§Examples

// Split to a display width of five.
let full: &str = "\x1b[2mBjörk\x1b[0m Guðmundsdóttir";
let idx = fyi_msg::length_width(full.as_bytes(), 5);
assert_eq!(
    &full[..idx],
    "\x1b[2mBjörk\x1b[0m",
);