#title-case #unicode #locale #string #chars #utilities #turkish

unicode_titlecase

一个库,为字符和字符串添加 Unicode 标题化以及土耳其语和阿塞拜疆语地区的上下文无关大小写工具

10 个稳定版本

2.3.0 2023年9月21日
2.2.1 2023年8月12日
2.2.0 2023年7月18日
2.1.0 2023年6月23日
1.1.0 2022年5月27日

#202文本处理

Download history 68/week @ 2024-03-11 29/week @ 2024-03-18 43/week @ 2024-03-25 78/week @ 2024-04-01 39/week @ 2024-04-08 121/week @ 2024-04-15 4/week @ 2024-04-22 39/week @ 2024-04-29 32/week @ 2024-05-20 39/week @ 2024-05-27 6/week @ 2024-06-03 17/week @ 2024-06-10 22/week @ 2024-06-17 14/week @ 2024-06-24

61 每月下载量
3 crates 中使用

MIT/Apache

290KB
381

Unicode 标题化

为字符和字符串提供 Unicode 标题化操作。该库支持 TR/AZ 区域的附加功能。

GitHub Workflow Status docs.rs Crates.io Crates.io

用法

字符

char 转换为其标题化等效的 [char; 3] 数组

use unicode_titlecase::to_titlecase;

assert_eq!(to_titlecase('A'), ['A', '\0', '\0']);
assert_eq!(to_titlecase('DŽ'), ['Dž', '\0', '\0']);
assert_eq!(to_titlecase(''), ['F', 'f', 'l']);

或者使用与标准库格式相同的迭代器版本。该库定义了一个在 char 上实现的 Trait

use unicode_titlecase::TitleCase;
assert_eq!('i'.to_titlecase().to_string(), "I");
assert_eq!('A'.to_titlecase().to_string(), "A");
assert_eq!('DŽ'.to_titlecase().to_string(), "Dž");
assert_eq!(''.to_titlecase().to_string(), "Ffl");

字符串

str 上定义了类似的 trait。这将标题化字符串的第一个字符,其余部分保持不变,并返回一个新的 String

use unicode_titlecase::StrTitleCase;
assert_eq!("iii".to_titlecase(), "Iii");
assert_eq!("ABC".to_titlecase(), "ABC");
assert_eq!("DŽDŽ".to_titlecase(), "DžDŽ");
assert_eq!("fflabc".to_titlecase(), "Fflabc");

或者,您可以降低字符串其余部分的字母大小写

use unicode_titlecase::StrTitleCase;
assert_eq!("iIi".to_titlecase_lower_rest(), "Iii");
assert_eq!("ABC".to_titlecase_lower_rest(), "Abc");
assert_eq!("DŽDŽ".to_titlecase_lower_rest(), "Dždž");
assert_eq!("fflabc".to_titlecase_lower_rest(), "Fflabc");

测试字符或字符串

要查看字符是否已经是标题化,提供了 is_titlecase

use unicode_titlecase::TitleCase;
assert!('A'.is_titlecase());
assert!('Dž'.is_titlecase());
assert!('İ'.is_titlecase());

assert!(!'a'.is_titlecase());
assert!(!'DŽ'.is_titlecase());
assert!(!''.is_titlecase());

要测试字符串是否已经是标题化,提供了两种选择。第一个,starts_titlecase 如果第一个字符是标题化则返回 true,忽略字符串的其余部分。第二个 starts_titlecase_rest_lower 仅在第一个字符是标题化且字符串的其余部分是小写的情况下返回 true。

use unicode_titlecase::StrTitleCase;
assert!("Abc".starts_titlecase());
assert!("ABC".starts_titlecase());
assert!(!"abc".starts_titlecase());

assert!("Abc".starts_titlecase_rest_lower());
assert!("İbc".starts_titlecase_rest_lower());
assert!(!"abc".starts_titlecase_rest_lower());
assert!(!"ABC".starts_titlecase_rest_lower());
assert!(!"İİ".starts_titlecase_rest_lower());

所有测试函数无论在哪个区域都按相同的方式工作。

区域

土耳其语和阿塞拜疆语(TR/AZ)区域对如何标题化某些字符有不同的规则。to_titlecase 函数假设区域不是这两个位置之一。提供了每个函数的 "tr_or_az" 版本。

use unicode_titlecase::{to_titlecase_tr_or_az, StrTitleCase, TitleCase};
assert_eq!(to_titlecase_tr_or_az('i'), ['İ', '\0', '\0']);
assert_eq!('i'.to_titlecase_tr_or_az().to_string(), "İ");
assert_eq!("iIi".to_titlecase_tr_or_az(), "İIi");
assert_eq!("iIi".to_titlecase_tr_or_az_lower_rest(), "İıi");

此外,在 tr_az 模块中提供了土耳其语和阿塞拜疆语的大小写工具。

use unicode_titlecase::tr_az::{to_uppercase_tr_or_az, to_lowercase_tr_or_az, StrTrAzCasing};
assert_eq!(to_uppercase_tr_or_az('i').to_string(), "İ");
assert_eq!(to_lowercase_tr_or_az('I'), 'ı');
assert_eq!("İIAb".to_lowercase_tr_az(), "iıab");
assert_eq!("iıab".to_uppercase_tr_az(), "İIAB");

许可证

许可协议为以下之一

根据您的要求。

贡献

除非您明确声明,否则您有意提交并希望包含在本作品中的任何贡献,根据Apache-2.0许可证定义,应按上述方式双重许可,不附加任何额外的条款或条件。

无运行时依赖