#unicode-width #unicode-characters #unicode #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

#151 in Text processing

Download history 50800/week @ 2024-07-08 48602/week @ 2024-07-15 52132/week @ 2024-07-22 55211/week @ 2024-07-29 60075/week @ 2024-08-05 59745/week @ 2024-08-12 56025/week @ 2024-08-19 53099/week @ 2024-08-26 54988/week @ 2024-09-02 53734/week @ 2024-09-09 52056/week @ 2024-09-16 60456/week @ 2024-09-23 57369/week @ 2024-09-30 58017/week @ 2024-10-07 62532/week @ 2024-10-14 63635/week @ 2024-10-21

247,267 downloads per month
Used in 574 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

~2MB
~32K SLoC