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.
12 use crate::lookups::{
13 canonical_fully_decomposed, compatibility_fully_decomposed, composition_table,
14 };
15
16 use core::{char, ops::FnMut};
17
18 /// Compute canonical Unicode decomposition for character.
19 /// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/)
20 /// for more information.
21 #[inline]
decompose_canonical<F>(c: char, emit_char: F) where F: FnMut(char),22 pub fn decompose_canonical<F>(c: char, emit_char: F)
23 where
24 F: FnMut(char),
25 {
26 decompose(c, canonical_fully_decomposed, emit_char)
27 }
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.
32 #[inline]
decompose_compatible<F: FnMut(char)>(c: char, emit_char: F)33 pub fn decompose_compatible<F: FnMut(char)>(c: char, emit_char: F) {
34 let decompose_char =
35 |c| compatibility_fully_decomposed(c).or_else(|| canonical_fully_decomposed(c));
36 decompose(c, decompose_char, emit_char)
37 }
38
39 #[inline]
decompose<D, F>(c: char, decompose_char: D, mut emit_char: F) where D: Fn(char) -> Option<&'static [char]>, F: FnMut(char),40 fn decompose<D, F>(c: char, decompose_char: D, mut emit_char: F)
41 where
42 D: Fn(char) -> Option<&'static [char]>,
43 F: FnMut(char),
44 {
45 // 7-bit ASCII never decomposes
46 if c <= '\x7f' {
47 emit_char(c);
48 return;
49 }
50
51 // Perform decomposition for Hangul
52 if is_hangul_syllable(c) {
53 decompose_hangul(c, emit_char);
54 return;
55 }
56
57 if let Some(decomposed) = decompose_char(c) {
58 for &d in decomposed {
59 emit_char(d);
60 }
61 return;
62 }
63
64 // Finally bottom out.
65 emit_char(c);
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.
compose(a: char, b: char) -> Option<char>71 pub fn compose(a: char, b: char) -> Option<char> {
72 compose_hangul(a, b).or_else(|| composition_table(a, b))
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;
84 const N_COUNT: u32 = V_COUNT * T_COUNT;
85 const S_COUNT: u32 = L_COUNT * N_COUNT;
86
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
is_hangul_syllable(c: char) -> bool96 pub(crate) fn is_hangul_syllable(c: char) -> bool {
97 (c as u32) >= S_BASE && (c as u32) < (S_BASE + S_COUNT)
98 }
99
100 // Decompose a precomposed Hangul syllable
101 #[allow(unsafe_code)]
102 #[inline(always)]
decompose_hangul<F>(s: char, mut emit_char: F) where F: FnMut(char),103 fn decompose_hangul<F>(s: char, mut emit_char: F)
104 where
105 F: FnMut(char),
106 {
107 let s_index = s as u32 - S_BASE;
108 let l_index = s_index / N_COUNT;
109 unsafe {
110 emit_char(char::from_u32_unchecked(L_BASE + l_index));
111
112 let v_index = (s_index % N_COUNT) / T_COUNT;
113 emit_char(char::from_u32_unchecked(V_BASE + v_index));
114
115 let t_index = s_index % T_COUNT;
116 if t_index > 0 {
117 emit_char(char::from_u32_unchecked(T_BASE + t_index));
118 }
119 }
120 }
121
122 #[inline]
hangul_decomposition_length(s: char) -> usize123 pub(crate) fn hangul_decomposition_length(s: char) -> usize {
124 let si = s as u32 - S_BASE;
125 let ti = si % T_COUNT;
126 if ti > 0 {
127 3
128 } else {
129 2
130 }
131 }
132
133 // Compose a pair of Hangul Jamo
134 #[allow(unsafe_code)]
135 #[inline(always)]
136 #[allow(ellipsis_inclusive_range_patterns)]
compose_hangul(a: char, b: char) -> Option<char>137 fn compose_hangul(a: char, b: char) -> Option<char> {
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
141 (L_BASE...L_LAST, V_BASE...V_LAST) => {
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;
146 Some(unsafe { char::from_u32_unchecked(s) })
147 }
148 // Compose an LV_Syllable and a trailing consonant into an LVT_Syllable
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 }
152 _ => None,
153 }
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]
test_hangul_composition()164 fn test_hangul_composition() {
165 assert_eq!(compose_hangul('\u{c8e0}', '\u{11a7}'), None);
166 }
167 }
168