Function fyi_msg::width

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

§Width.

Find the “display width” of a byte string.

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

Like anything having to do with width vs length, this should be considered at best an approximation. For ASCII, every byte that is not a control character or part of an ANSI CSI or OSC sequence is counted as having a length of 1. For Unicode, the unicode_width crate is used to determine width.

Note: line breaks are ignored; the cumulative width of all lines is returned. If you’re trying to calculate line widths, split the slice first and pass each chunk separately.

This requires the fitted crate feature.

§Examples

// Line breaks have no width:
assert_ne!(
    fyi_msg::width(b"Hello World"),
    fyi_msg::width(b"Hello\nWorld"),
);