#text #width #unicode #truncate #pad

no-std unicode-truncate

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

3 unstable releases

0.2.0 Dec 17, 2020
0.1.1 Aug 15, 2019
0.1.0 Aug 15, 2019

#291 in Text processing

Download history 1924/week @ 2023-10-20 1992/week @ 2023-10-27 1663/week @ 2023-11-03 1967/week @ 2023-11-10 1633/week @ 2023-11-17 1578/week @ 2023-11-24 2545/week @ 2023-12-01 2288/week @ 2023-12-08 3793/week @ 2023-12-15 1828/week @ 2023-12-22 2323/week @ 2023-12-29 3366/week @ 2024-01-05 3626/week @ 2024-01-12 3663/week @ 2024-01-19 2839/week @ 2024-01-26 2257/week @ 2024-02-02

13,022 downloads per month
Used in 31 crates (23 directly)

MIT/Apache

16KB
250 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() {
    let (rv, w) = "你好吗".unicode_truncate(5);
    assert_eq!(rv, "你好");
    assert_eq!(w, 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 rv = "你好吗".unicode_pad(5, Alignment::Left, true);
    assert_eq!(rv, "你好 ");
    assert_eq!(rv.width(), 5);
}

features

unicode-truncate can be built without libstd 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

~67KB