|
1 | 1 | //! Character Tables
|
2 | 2 | use unicode_bidi::{bidi_class, BidiClass};
|
3 | 3 | use std::cmp::Ordering;
|
| 4 | +use std::str::Chars; |
4 | 5 |
|
5 | 6 | use super::rfc3454;
|
6 | 7 |
|
@@ -30,19 +31,31 @@ pub fn commonly_mapped_to_nothing(c: char) -> bool {
|
30 | 31 | }
|
31 | 32 |
|
32 | 33 | /// B.2 Mapping for case-folding used with NFKC.
|
33 |
| -pub fn case_fold_for_nfkc(s: &str) -> String { |
34 |
| - let mut result = String::new(); |
35 |
| - |
36 |
| - // Each character either maps to a sequence of replacement characters, |
37 |
| - // or is passed through as-is. |
38 |
| - for c in s.chars() { |
39 |
| - match rfc3454::B_2.binary_search_by_key(&c, |e| e.0) { |
40 |
| - Ok(idx) => result.push_str(rfc3454::B_2[idx].1), |
41 |
| - Err(_) => result.push(c), |
| 34 | +pub fn case_fold_for_nfkc(c: char) -> CaseFoldForNfkc { |
| 35 | + let inner = match rfc3454::B_2.binary_search_by_key(&c, |e| e.0) { |
| 36 | + Ok(idx) => FoldInner::Chars(rfc3454::B_2[idx].1.chars()), |
| 37 | + Err(_) => FoldInner::Char(Some(c)), |
| 38 | + }; |
| 39 | + CaseFoldForNfkc(inner) |
| 40 | +} |
| 41 | + |
| 42 | +enum FoldInner { |
| 43 | + Chars(Chars<'static>), |
| 44 | + Char(Option<char>), |
| 45 | +} |
| 46 | + |
| 47 | +/// The iterator returned by `case_fold_for_nfkc`. |
| 48 | +pub struct CaseFoldForNfkc(FoldInner); |
| 49 | + |
| 50 | +impl Iterator for CaseFoldForNfkc { |
| 51 | + type Item = char; |
| 52 | + |
| 53 | + fn next(&mut self) -> Option<char> { |
| 54 | + match self.0 { |
| 55 | + FoldInner::Chars(ref mut it) => it.next(), |
| 56 | + FoldInner::Char(ref mut ch) => ch.take(), |
42 | 57 | }
|
43 | 58 | }
|
44 |
| - |
45 |
| - result |
46 | 59 | }
|
47 | 60 |
|
48 | 61 | /// C.1.1 ASCII space characters
|
|
0 commit comments