]>
Commit | Line | Data |
---|---|---|
abe05a73 XL |
1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! Functions for computing canonical and compatible decompositions for Unicode characters. | |
f035d41b XL |
12 | use crate::lookups::{ |
13 | canonical_fully_decomposed, compatibility_fully_decomposed, composition_table, | |
14 | }; | |
15 | ||
16 | use core::{char, ops::FnMut}; | |
abe05a73 XL |
17 | |
18 | /// Compute canonical Unicode decomposition for character. | |
19 | /// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/) | |
20 | /// for more information. | |
8faf50e0 | 21 | #[inline] |
f035d41b XL |
22 | pub fn decompose_canonical<F>(c: char, emit_char: F) |
23 | where | |
24 | F: FnMut(char), | |
25 | { | |
dfeec247 | 26 | decompose(c, canonical_fully_decomposed, emit_char) |
8faf50e0 | 27 | } |
abe05a73 XL |
28 | |
29 | /// Compute canonical or compatible Unicode decomposition for character. | |
30 | /// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/) | |
31 | /// for more information. | |
8faf50e0 XL |
32 | #[inline] |
33 | pub fn decompose_compatible<F: FnMut(char)>(c: char, emit_char: F) { | |
f035d41b XL |
34 | let decompose_char = |
35 | |c| compatibility_fully_decomposed(c).or_else(|| canonical_fully_decomposed(c)); | |
8faf50e0 XL |
36 | decompose(c, decompose_char, emit_char) |
37 | } | |
abe05a73 | 38 | |
8faf50e0 XL |
39 | #[inline] |
40 | fn decompose<D, F>(c: char, decompose_char: D, mut emit_char: F) | |
f035d41b XL |
41 | where |
42 | D: Fn(char) -> Option<&'static [char]>, | |
43 | F: FnMut(char), | |
8faf50e0 | 44 | { |
abe05a73 | 45 | // 7-bit ASCII never decomposes |
8faf50e0 XL |
46 | if c <= '\x7f' { |
47 | emit_char(c); | |
abe05a73 XL |
48 | return; |
49 | } | |
50 | ||
8faf50e0 XL |
51 | // Perform decomposition for Hangul |
52 | if is_hangul_syllable(c) { | |
53 | decompose_hangul(c, emit_char); | |
54 | return; | |
abe05a73 XL |
55 | } |
56 | ||
8faf50e0 XL |
57 | if let Some(decomposed) = decompose_char(c) { |
58 | for &d in decomposed { | |
59 | emit_char(d); | |
abe05a73 | 60 | } |
8faf50e0 | 61 | return; |
abe05a73 XL |
62 | } |
63 | ||
64 | // Finally bottom out. | |
8faf50e0 | 65 | emit_char(c); |
abe05a73 XL |
66 | } |
67 | ||
68 | /// Compose two characters into a single character, if possible. | |
69 | /// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/) | |
70 | /// for more information. | |
71 | pub fn compose(a: char, b: char) -> Option<char> { | |
dfeec247 | 72 | compose_hangul(a, b).or_else(|| composition_table(a, b)) |
abe05a73 XL |
73 | } |
74 | ||
75 | // Constants from Unicode 9.0.0 Section 3.12 Conjoining Jamo Behavior | |
76 | // http://www.unicode.org/versions/Unicode9.0.0/ch03.pdf#M9.32468.Heading.310.Combining.Jamo.Behavior | |
77 | const S_BASE: u32 = 0xAC00; | |
78 | const L_BASE: u32 = 0x1100; | |
79 | const V_BASE: u32 = 0x1161; | |
80 | const T_BASE: u32 = 0x11A7; | |
81 | const L_COUNT: u32 = 19; | |
82 | const V_COUNT: u32 = 21; | |
83 | const T_COUNT: u32 = 28; | |
f035d41b XL |
84 | const N_COUNT: u32 = V_COUNT * T_COUNT; |
85 | const S_COUNT: u32 = L_COUNT * N_COUNT; | |
abe05a73 | 86 | |
8faf50e0 XL |
87 | const S_LAST: u32 = S_BASE + S_COUNT - 1; |
88 | const L_LAST: u32 = L_BASE + L_COUNT - 1; | |
89 | const V_LAST: u32 = V_BASE + V_COUNT - 1; | |
90 | const T_LAST: u32 = T_BASE + T_COUNT - 1; | |
91 | ||
92 | // Composition only occurs for `TPart`s in `U+11A8 ... U+11C2`, | |
93 | // i.e. `T_BASE + 1 ... T_LAST`. | |
94 | const T_FIRST: u32 = T_BASE + 1; | |
95 | ||
96 | pub(crate) fn is_hangul_syllable(c: char) -> bool { | |
97 | (c as u32) >= S_BASE && (c as u32) < (S_BASE + S_COUNT) | |
98 | } | |
99 | ||
abe05a73 XL |
100 | // Decompose a precomposed Hangul syllable |
101 | #[allow(unsafe_code)] | |
102 | #[inline(always)] | |
f035d41b XL |
103 | fn decompose_hangul<F>(s: char, mut emit_char: F) |
104 | where | |
105 | F: FnMut(char), | |
106 | { | |
8faf50e0 XL |
107 | let s_index = s as u32 - S_BASE; |
108 | let l_index = s_index / N_COUNT; | |
abe05a73 | 109 | unsafe { |
8faf50e0 | 110 | emit_char(char::from_u32_unchecked(L_BASE + l_index)); |
abe05a73 | 111 | |
8faf50e0 XL |
112 | let v_index = (s_index % N_COUNT) / T_COUNT; |
113 | emit_char(char::from_u32_unchecked(V_BASE + v_index)); | |
abe05a73 | 114 | |
8faf50e0 XL |
115 | let t_index = s_index % T_COUNT; |
116 | if t_index > 0 { | |
117 | emit_char(char::from_u32_unchecked(T_BASE + t_index)); | |
abe05a73 XL |
118 | } |
119 | } | |
120 | } | |
121 | ||
8faf50e0 XL |
122 | #[inline] |
123 | pub(crate) fn hangul_decomposition_length(s: char) -> usize { | |
124 | let si = s as u32 - S_BASE; | |
125 | let ti = si % T_COUNT; | |
f035d41b XL |
126 | if ti > 0 { |
127 | 3 | |
128 | } else { | |
129 | 2 | |
130 | } | |
8faf50e0 XL |
131 | } |
132 | ||
abe05a73 XL |
133 | // Compose a pair of Hangul Jamo |
134 | #[allow(unsafe_code)] | |
135 | #[inline(always)] | |
dfeec247 | 136 | #[allow(ellipsis_inclusive_range_patterns)] |
abe05a73 | 137 | fn compose_hangul(a: char, b: char) -> Option<char> { |
8faf50e0 XL |
138 | let (a, b) = (a as u32, b as u32); |
139 | match (a, b) { | |
140 | // Compose a leading consonant and a vowel together into an LV_Syllable | |
f035d41b | 141 | (L_BASE...L_LAST, V_BASE...V_LAST) => { |
8faf50e0 XL |
142 | let l_index = a - L_BASE; |
143 | let v_index = b - V_BASE; | |
144 | let lv_index = l_index * N_COUNT + v_index * T_COUNT; | |
145 | let s = S_BASE + lv_index; | |
f035d41b XL |
146 | Some(unsafe { char::from_u32_unchecked(s) }) |
147 | } | |
8faf50e0 | 148 | // Compose an LV_Syllable and a trailing consonant into an LVT_Syllable |
f035d41b XL |
149 | (S_BASE...S_LAST, T_FIRST...T_LAST) if (a - S_BASE) % T_COUNT == 0 => { |
150 | Some(unsafe { char::from_u32_unchecked(a + (b - T_BASE)) }) | |
151 | } | |
8faf50e0 | 152 | _ => None, |
abe05a73 | 153 | } |
8faf50e0 XL |
154 | } |
155 | ||
156 | #[cfg(test)] | |
157 | mod tests { | |
158 | use super::compose_hangul; | |
159 | ||
160 | // Regression test from a bugfix where we were composing an LV_Syllable with | |
161 | // T_BASE directly. (We should only compose an LV_Syllable with a character | |
162 | // in the range `T_BASE + 1 ... T_LAST`.) | |
163 | #[test] | |
164 | fn test_hangul_composition() { | |
165 | assert_eq!(compose_hangul('\u{c8e0}', '\u{11a7}'), None); | |
abe05a73 | 166 | } |
abe05a73 | 167 | } |