#unicode-width #unicode #unicode-characters #width #truncate #pad #unicode-text

no-std unicode-truncate

Unicode-aware algorithm to pad or truncate str in terms of displayed width

5 releases (2 stable)

1.1.0 Jul 9, 2024
1.0.0 Apr 27, 2024
0.2.0 Dec 17, 2020
0.1.1 Aug 15, 2019
0.1.0 Aug 15, 2019

#112 in Text processing

Download history 3583/week @ 2024-04-01 3124/week @ 2024-04-08 4460/week @ 2024-04-15 3447/week @ 2024-04-22 4312/week @ 2024-04-29 4186/week @ 2024-05-06 5055/week @ 2024-05-13 21057/week @ 2024-05-20 22984/week @ 2024-05-27 32371/week @ 2024-06-03 41917/week @ 2024-06-10 41342/week @ 2024-06-17 45503/week @ 2024-06-24 45923/week @ 2024-07-01 50800/week @ 2024-07-08 45822/week @ 2024-07-15

190,692 downloads per month
Used in 400 crates (25 directly)

MIT/Apache

27KB
395 lines

unicode-truncate

Unicode-aware algorithm to pad or truncate str in terms of displayed width.

crates.io Documentation Build Status

Examples

Safely truncate string to display width even not at character boundaries.

use unicode_truncate::UnicodeTruncateStr;

fn main() {
    assert_eq!("你好吗".unicode_truncate(5), ("你好", 4));
}

Making sure the string is displayed in exactly number of columns by combining padding and truncating.

use unicode_truncate::UnicodeTruncateStr;
use unicode_truncate::Alignment;
use unicode_width::UnicodeWidthStr;

fn main() {
    let str = "你好吗".unicode_pad(5, Alignment::Left, true);
    assert_eq!(str, "你好 ");
    assert_eq!(str.width(), 5);
}

Features

unicode-truncate can be built without std by disabling the default feature std. However, in that case unicode_truncate::UnicodeTruncateStr::unicode_pad won't be available because it depends on std::string::String and std::borrow::Cow.

Dependencies

~1.5MB
~17K SLoC