]> git.proxmox.com Git - rustc.git/blame - vendor/unicode-normalization/src/normalize.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / vendor / unicode-normalization / src / normalize.rs
CommitLineData
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 12use crate::lookups::{
6a06907d
XL
13 canonical_fully_decomposed, cjk_compat_variants_fully_decomposed,
14 compatibility_fully_decomposed, composition_table,
f035d41b
XL
15};
16
17use core::{char, ops::FnMut};
abe05a73
XL
18
19/// Compute canonical Unicode decomposition for character.
20/// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/)
21/// for more information.
8faf50e0 22#[inline]
f035d41b
XL
23pub fn decompose_canonical<F>(c: char, emit_char: F)
24where
25 F: FnMut(char),
26{
dfeec247 27 decompose(c, canonical_fully_decomposed, emit_char)
8faf50e0 28}
abe05a73
XL
29
30/// Compute canonical or compatible Unicode decomposition for character.
31/// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/)
32/// for more information.
8faf50e0
XL
33#[inline]
34pub fn decompose_compatible<F: FnMut(char)>(c: char, emit_char: F) {
f035d41b
XL
35 let decompose_char =
36 |c| compatibility_fully_decomposed(c).or_else(|| canonical_fully_decomposed(c));
8faf50e0
XL
37 decompose(c, decompose_char, emit_char)
38}
abe05a73 39
6a06907d
XL
40/// Compute standard-variation decomposition for character.
41///
42/// [Standardized Variation Sequences] are used instead of the standard canonical
43/// decompositions, notably for CJK codepoints with singleton canonical decompositions,
44/// to avoid losing information. See the
45/// [Unicode Variation Sequence FAQ](http://unicode.org/faq/vs.html) and the
46/// "Other Enhancements" section of the
47/// [Unicode 6.3 Release Summary](https://www.unicode.org/versions/Unicode6.3.0/#Summary)
48/// for more information.
49#[inline]
50pub fn decompose_cjk_compat_variants<F>(c: char, mut emit_char: F)
51where
52 F: FnMut(char),
53{
54 // 7-bit ASCII never decomposes
55 if c <= '\x7f' {
56 emit_char(c);
57 return;
58 }
59
60 // Don't perform decomposition for Hangul
61
62 if let Some(decomposed) = cjk_compat_variants_fully_decomposed(c) {
63 for &d in decomposed {
64 emit_char(d);
65 }
66 return;
67 }
68
69 // Finally bottom out.
70 emit_char(c);
71}
72
8faf50e0
XL
73#[inline]
74fn decompose<D, F>(c: char, decompose_char: D, mut emit_char: F)
f035d41b
XL
75where
76 D: Fn(char) -> Option<&'static [char]>,
77 F: FnMut(char),
8faf50e0 78{
abe05a73 79 // 7-bit ASCII never decomposes
8faf50e0
XL
80 if c <= '\x7f' {
81 emit_char(c);
abe05a73
XL
82 return;
83 }
84
8faf50e0
XL
85 // Perform decomposition for Hangul
86 if is_hangul_syllable(c) {
87 decompose_hangul(c, emit_char);
88 return;
abe05a73
XL
89 }
90
8faf50e0
XL
91 if let Some(decomposed) = decompose_char(c) {
92 for &d in decomposed {
93 emit_char(d);
abe05a73 94 }
8faf50e0 95 return;
abe05a73
XL
96 }
97
98 // Finally bottom out.
8faf50e0 99 emit_char(c);
abe05a73
XL
100}
101
102/// Compose two characters into a single character, if possible.
103/// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/)
104/// for more information.
105pub fn compose(a: char, b: char) -> Option<char> {
dfeec247 106 compose_hangul(a, b).or_else(|| composition_table(a, b))
abe05a73
XL
107}
108
109// Constants from Unicode 9.0.0 Section 3.12 Conjoining Jamo Behavior
110// http://www.unicode.org/versions/Unicode9.0.0/ch03.pdf#M9.32468.Heading.310.Combining.Jamo.Behavior
111const S_BASE: u32 = 0xAC00;
112const L_BASE: u32 = 0x1100;
113const V_BASE: u32 = 0x1161;
114const T_BASE: u32 = 0x11A7;
115const L_COUNT: u32 = 19;
116const V_COUNT: u32 = 21;
117const T_COUNT: u32 = 28;
f035d41b
XL
118const N_COUNT: u32 = V_COUNT * T_COUNT;
119const S_COUNT: u32 = L_COUNT * N_COUNT;
abe05a73 120
8faf50e0
XL
121const S_LAST: u32 = S_BASE + S_COUNT - 1;
122const L_LAST: u32 = L_BASE + L_COUNT - 1;
123const V_LAST: u32 = V_BASE + V_COUNT - 1;
124const T_LAST: u32 = T_BASE + T_COUNT - 1;
125
126// Composition only occurs for `TPart`s in `U+11A8 ... U+11C2`,
127// i.e. `T_BASE + 1 ... T_LAST`.
128const T_FIRST: u32 = T_BASE + 1;
129
130pub(crate) fn is_hangul_syllable(c: char) -> bool {
131 (c as u32) >= S_BASE && (c as u32) < (S_BASE + S_COUNT)
132}
133
abe05a73
XL
134// Decompose a precomposed Hangul syllable
135#[allow(unsafe_code)]
136#[inline(always)]
f035d41b
XL
137fn decompose_hangul<F>(s: char, mut emit_char: F)
138where
139 F: FnMut(char),
140{
8faf50e0
XL
141 let s_index = s as u32 - S_BASE;
142 let l_index = s_index / N_COUNT;
abe05a73 143 unsafe {
8faf50e0 144 emit_char(char::from_u32_unchecked(L_BASE + l_index));
abe05a73 145
8faf50e0
XL
146 let v_index = (s_index % N_COUNT) / T_COUNT;
147 emit_char(char::from_u32_unchecked(V_BASE + v_index));
abe05a73 148
8faf50e0
XL
149 let t_index = s_index % T_COUNT;
150 if t_index > 0 {
151 emit_char(char::from_u32_unchecked(T_BASE + t_index));
abe05a73
XL
152 }
153 }
154}
155
8faf50e0
XL
156#[inline]
157pub(crate) fn hangul_decomposition_length(s: char) -> usize {
158 let si = s as u32 - S_BASE;
159 let ti = si % T_COUNT;
f035d41b
XL
160 if ti > 0 {
161 3
162 } else {
163 2
164 }
8faf50e0
XL
165}
166
abe05a73
XL
167// Compose a pair of Hangul Jamo
168#[allow(unsafe_code)]
169#[inline(always)]
dfeec247 170#[allow(ellipsis_inclusive_range_patterns)]
abe05a73 171fn compose_hangul(a: char, b: char) -> Option<char> {
8faf50e0
XL
172 let (a, b) = (a as u32, b as u32);
173 match (a, b) {
174 // Compose a leading consonant and a vowel together into an LV_Syllable
f035d41b 175 (L_BASE...L_LAST, V_BASE...V_LAST) => {
8faf50e0
XL
176 let l_index = a - L_BASE;
177 let v_index = b - V_BASE;
178 let lv_index = l_index * N_COUNT + v_index * T_COUNT;
179 let s = S_BASE + lv_index;
f035d41b
XL
180 Some(unsafe { char::from_u32_unchecked(s) })
181 }
8faf50e0 182 // Compose an LV_Syllable and a trailing consonant into an LVT_Syllable
f035d41b
XL
183 (S_BASE...S_LAST, T_FIRST...T_LAST) if (a - S_BASE) % T_COUNT == 0 => {
184 Some(unsafe { char::from_u32_unchecked(a + (b - T_BASE)) })
185 }
8faf50e0 186 _ => None,
abe05a73 187 }
8faf50e0
XL
188}
189
190#[cfg(test)]
191mod tests {
192 use super::compose_hangul;
193
194 // Regression test from a bugfix where we were composing an LV_Syllable with
195 // T_BASE directly. (We should only compose an LV_Syllable with a character
196 // in the range `T_BASE + 1 ... T_LAST`.)
197 #[test]
198 fn test_hangul_composition() {
199 assert_eq!(compose_hangul('\u{c8e0}', '\u{11a7}'), None);
abe05a73 200 }
abe05a73 201}