Rust的ToLowercase

时间:2016-03-01 06:32:52

标签: rust

Rust char有一个to_lowercase函数,似乎返回结构ToLowercase,它似乎是一个迭代器,总是有一个元素。

不会直接返回char更加自然和简单吗?

2 个答案:

答案 0 :(得分:22)

  

直接返回一个字符会更加自然和简单吗?

自然,简单,和错误。 Unicode过于复杂,无法工作。根本问题在于,char 不足以始终代表单个,逻辑上完整的字符",对于"字符的某些定义"

  

它似乎是一个总是有一个元素的迭代器。

通过运行一个简单的程序,可以轻易地证明这是错误的,该程序是大小写的每个有效的Unicode代码点。该计划:

/*!
Add the following to a `Cargo.toml` file:

```cargo
[dependencies]
arrayvec = "0.3.15"
```
*/
extern crate arrayvec;
use arrayvec::ArrayVec;

fn main() {
    let mut expanded_lcs = 0;
    let mut expanded_ucs = 0;

    let usvs = (0..0xd7ff).chain(0xe000..0x10ffff)
        .flat_map(|v| std::char::from_u32(v).into_iter());

    for c in usvs {
        let lc: ArrayVec<[_; 4]> = c.to_lowercase().collect();
        let uc: ArrayVec<[_; 4]> = c.to_uppercase().collect();

        if lc.len() != 1 {
            expanded_lcs += 1;
            print!("'{}' U+{:04X} L -> ", c, c as u32);
            for c in lc {
                print!("'{}' U+{:04X} ", c, c as u32);
            }
            println!("");
        }

        if uc.len() != 1 {
            expanded_ucs += 1;
            print!("'{}' U+{:04X} U -> ", c, c as u32);
            for c in uc {
                print!("'{}' U+{:04X} ", c, c as u32);
            }
            println!("");
        }
    }

    println!("\n-----\n");

    println!("Found {} chars with expanded lowercase conversions.", expanded_lcs);
    println!("Found {} chars with expanded uppercase conversions.", expanded_ucs);
}

及其输出,每晚给出rustc 1.8:

'ß' U+00DF U -> 'S' U+0053 'S' U+0053 
'İ' U+0130 L -> 'i' U+0069 '̇' U+0307 
'ʼn' U+0149 U -> 'ʼ' U+02BC 'N' U+004E 
'ǰ' U+01F0 U -> 'J' U+004A '̌' U+030C 
'ΐ' U+0390 U -> 'Ι' U+0399 '̈' U+0308 '́' U+0301 
'ΰ' U+03B0 U -> 'Υ' U+03A5 '̈' U+0308 '́' U+0301 
'և' U+0587 U -> 'Ե' U+0535 'Ւ' U+0552 
'ẖ' U+1E96 U -> 'H' U+0048 '̱' U+0331 
'ẗ' U+1E97 U -> 'T' U+0054 '̈' U+0308 
'ẘ' U+1E98 U -> 'W' U+0057 '̊' U+030A 
'ẙ' U+1E99 U -> 'Y' U+0059 '̊' U+030A 
'ẚ' U+1E9A U -> 'A' U+0041 'ʾ' U+02BE 
'ὐ' U+1F50 U -> 'Υ' U+03A5 '̓' U+0313 
'ὒ' U+1F52 U -> 'Υ' U+03A5 '̓' U+0313 '̀' U+0300 
'ὔ' U+1F54 U -> 'Υ' U+03A5 '̓' U+0313 '́' U+0301 
'ὖ' U+1F56 U -> 'Υ' U+03A5 '̓' U+0313 '͂' U+0342 
'ᾀ' U+1F80 U -> 'Ἀ' U+1F08 'Ι' U+0399 
'ᾁ' U+1F81 U -> 'Ἁ' U+1F09 'Ι' U+0399 
'ᾂ' U+1F82 U -> 'Ἂ' U+1F0A 'Ι' U+0399 
'ᾃ' U+1F83 U -> 'Ἃ' U+1F0B 'Ι' U+0399 
'ᾄ' U+1F84 U -> 'Ἄ' U+1F0C 'Ι' U+0399 
'ᾅ' U+1F85 U -> 'Ἅ' U+1F0D 'Ι' U+0399 
'ᾆ' U+1F86 U -> 'Ἆ' U+1F0E 'Ι' U+0399 
'ᾇ' U+1F87 U -> 'Ἇ' U+1F0F 'Ι' U+0399 
'ᾈ' U+1F88 U -> 'Ἀ' U+1F08 'Ι' U+0399 
'ᾉ' U+1F89 U -> 'Ἁ' U+1F09 'Ι' U+0399 
'ᾊ' U+1F8A U -> 'Ἂ' U+1F0A 'Ι' U+0399 
'ᾋ' U+1F8B U -> 'Ἃ' U+1F0B 'Ι' U+0399 
'ᾌ' U+1F8C U -> 'Ἄ' U+1F0C 'Ι' U+0399 
'ᾍ' U+1F8D U -> 'Ἅ' U+1F0D 'Ι' U+0399 
'ᾎ' U+1F8E U -> 'Ἆ' U+1F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ᾧ' U+1FA7 U -> 'Ὧ' U+1F6F 'Ι' U+0399 
'ᾨ' U+1FA8 U -> 'Ὠ' U+1F68 'Ι' U+0399 
'ᾩ' U+1FA9 U -> 'Ὡ' U+1F69 'Ι' U+0399 
'ᾪ' U+1FAA U -> 'Ὢ' U+1F6A 'Ι' U+0399 
'ᾫ' U+1FAB U -> 'Ὣ' U+1F6B 'Ι' U+0399 
'ᾬ' U+1FAC U -> 'Ὤ' U+1F6C 'Ι' U+0399 
'ᾭ' U+1FAD U -> 'Ὥ' U+1F6D 'Ι' U+0399 
'ᾮ' U+1FAE U -> 'Ὦ' U+1F6E 'Ι' U+0399 
'ᾯ' U+1FAF U -> 'Ὧ' U+1F6F 'Ι' U+0399 
'ᾲ' U+1FB2 U -> 'Ὰ' U+1FBA 'Ι' U+0399 
'ᾳ' U+1FB3 U -> 'Α' U+0391 'Ι' U+0399 
'ᾴ' U+1FB4 U -> 'Ά' U+0386 'Ι' U+0399 
'ᾶ' U+1FB6 U -> 'Α' U+0391 '͂' U+0342 
'ᾷ' U+1FB7 U -> 'Α' U+0391 '͂' U+0342 'Ι' U+0399 
'ᾼ' U+1FBC U -> 'Α' U+0391 'Ι' U+0399 
'ῂ' U+1FC2 U -> 'Ὴ' U+1FCA 'Ι' U+0399 
'ῃ' U+1FC3 U -> 'Η' U+0397 'Ι' U+0399 
'ῄ' U+1FC4 U -> 'Ή' U+0389 'Ι' U+0399 
'ῆ' U+1FC6 U -> 'Η' U+0397 '͂' U+0342 
'ῇ' U+1FC7 U -> 'Η' U+0397 '͂' U+0342 'Ι' U+0399 
'ῌ' U+1FCC U -> 'Η' U+0397 'Ι' U+0399 
'ῒ' U+1FD2 U -> 'Ι' U+0399 '̈' U+0308 '̀' U+0300 
'ΐ' U+1FD3 U -> 'Ι' U+0399 '̈' U+0308 '́' U+0301 
'ῖ' U+1FD6 U -> 'Ι' U+0399 '͂' U+0342 
'ῗ' U+1FD7 U -> 'Ι' U+0399 '̈' U+0308 '͂' U+0342 
'ῢ' U+1FE2 U -> 'Υ' U+03A5 '̈' U+0308 '̀' U+0300 
'ΰ' U+1FE3 U -> 'Υ' U+03A5 '̈' U+0308 '́' U+0301 
'ῤ' U+1FE4 U -> 'Ρ' U+03A1 '̓' U+0313 
'ῦ' U+1FE6 U -> 'Υ' U+03A5 '͂' U+0342 
'ῧ' U+1FE7 U -> 'Υ' U+03A5 '̈' U+0308 '͂' U+0342 
'ῲ' U+1FF2 U -> 'Ὼ' U+1FFA 'Ι' U+0399 
'ῳ' U+1FF3 U -> 'Ω' U+03A9 'Ι' U+0399 
'ῴ' U+1FF4 U -> 'Ώ' U+038F 'Ι' U+0399 
'ῶ' U+1FF6 U -> 'Ω' U+03A9 '͂' U+0342 
'ῷ' U+1FF7 U -> 'Ω' U+03A9 '͂' U+0342 'Ι' U+0399 
'ῼ' U+1FFC U -> 'Ω' U+03A9 'Ι' U+0399 
'ff' U+FB00 U -> 'F' U+0046 'F' U+0046 
'fi' U+FB01 U -> 'F' U+0046 'I' U+0049 
'fl' U+FB02 U -> 'F' U+0046 'L' U+004C 
'ffi' U+FB03 U -> 'F' U+0046 'F' U+0046 'I' U+0049 
'ffl' U+FB04 U -> 'F' U+0046 'F' U+0046 'L' U+004C 
'ſt' U+FB05 U -> 'S' U+0053 'T' U+0054 
'st' U+FB06 U -> 'S' U+0053 'T' U+0054 
'ﬓ' U+FB13 U -> 'Մ' U+0544 'Ն' U+0546 
'ﬔ' U+FB14 U -> 'Մ' U+0544 'Ե' U+0535 
'ﬕ' U+FB15 U -> 'Մ' U+0544 'Ի' U+053B 
'ﬖ' U+FB16 U -> 'Վ' U+054E 'Ն' U+0546 
'ﬗ' U+FB17 U -> 'Մ' U+0544 'Խ' U+053D 

-----

Found 1 chars with expanded lowercase conversions.
Found 102 chars with expanded uppercase conversions.

请注意,此不会考虑区域设置,这可能会改变输出。

答案 1 :(得分:4)

  

它似乎是一个总是有一个元素的迭代器。

并非总是如此。在某些情况下,单个字符表示小写符号,而大写符号表示两个字符。

SpecialCasting Unicode文档中涵盖的那些案例。引用Rust文档:

  

这执行复杂的无条件映射而没有剪裁:它   根据,将一个Unicode字符映射到其小写等价物   Unicode数据库和其他复杂映射   SpecialCasing.txt。