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

无需std unicode-truncate

基于显示宽度的Unicode感知算法,用于填充或截断str

5个版本 (2个稳定版)

1.1.0 2024年7月9日
1.0.0 2024年4月27日
0.2.0 2020年12月17日
0.1.1 2019年8月15日
0.1.0 2019年8月15日

#139 in 文本处理

Download history 3951/week @ 2024-04-23 4059/week @ 2024-04-30 4150/week @ 2024-05-07 6494/week @ 2024-05-14 22555/week @ 2024-05-21 24186/week @ 2024-05-28 34575/week @ 2024-06-04 42838/week @ 2024-06-11 42099/week @ 2024-06-18 46457/week @ 2024-06-25 44003/week @ 2024-07-02 51610/week @ 2024-07-09 49508/week @ 2024-07-16 50844/week @ 2024-07-23 57404/week @ 2024-07-30 60449/week @ 2024-08-06

每月下载量227,051次
447个crate(25直接使用)中使用

MIT/Apache

27KB
395 行代码

unicode-truncate

基于显示宽度的Unicode感知算法,用于填充或截断str

crates.io Documentation Build Status

示例

安全地截断字符串到显示宽度,即使在字符边界之外。

use unicode_truncate::UnicodeTruncateStr;

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

通过结合填充和截断确保字符串以精确的列数显示。

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);
}

特性

unicode-truncate可以通过禁用默认功能std在无需std的情况下构建。然而,在这种情况下,unicode_truncate::UnicodeTruncateStr::unicode_pad将不可用,因为它依赖于std::string::Stringstd::borrow::Cow

依赖

~1.5MB
~17K SLoC