#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

6 releases (3 stable)

new 2.0.0 Nov 10, 2024
1.1.0 Jul 9, 2024
1.0.0 Apr 27, 2024
0.2.0 Dec 17, 2020
0.1.1 Aug 15, 2019

#81 in Text processing

Download history 51373/week @ 2024-07-21 55162/week @ 2024-07-28 59680/week @ 2024-08-04 59334/week @ 2024-08-11 55904/week @ 2024-08-18 54169/week @ 2024-08-25 54659/week @ 2024-09-01 53572/week @ 2024-09-08 51750/week @ 2024-09-15 59747/week @ 2024-09-22 57918/week @ 2024-09-29 57372/week @ 2024-10-06 63085/week @ 2024-10-13 63896/week @ 2024-10-20 60686/week @ 2024-10-27 54950/week @ 2024-11-03

247,376 downloads per month
Used in 593 crates (25 directly)

MIT/Apache

27KB
399 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

~2.5MB
~36K SLoC