]> git.proxmox.com Git - rustc.git/blob - src/librustc_unicode/normalize.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / librustc_unicode / normalize.rs
1 // Copyright 2012-2014 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
13 use core::cmp::Ordering::{Equal, Less, Greater};
14 use core::ops::FnMut;
15 use core::option::Option;
16 use core::option::Option::{Some, None};
17 use core::slice::SliceExt;
18 use core::result::Result::{Ok, Err};
19 use tables::normalization::{canonical_table, compatibility_table, composition_table};
20
21 fn bsearch_table<T>(c: char, r: &'static [(char, &'static [T])]) -> Option<&'static [T]> {
22 match r.binary_search_by(|&(val, _)| {
23 if c == val { Equal }
24 else if val < c { Less }
25 else { Greater }
26 }) {
27 Ok(idx) => {
28 let (_, result) = r[idx];
29 Some(result)
30 }
31 Err(_) => None
32 }
33 }
34
35 /// Compute canonical Unicode decomposition for character
36 #[deprecated(reason = "use the crates.io `unicode-normalization` library instead",
37 since = "1.0.0")]
38 #[unstable(feature = "unicode",
39 reason = "this functionality will be moved to crates.io")]
40 pub fn decompose_canonical<F>(c: char, mut i: F) where F: FnMut(char) { d(c, &mut i, false); }
41
42 /// Compute canonical or compatible Unicode decomposition for character
43 #[deprecated(reason = "use the crates.io `unicode-normalization` library instead",
44 since = "1.0.0")]
45 #[unstable(feature = "unicode",
46 reason = "this functionality will be moved to crates.io")]
47 pub fn decompose_compatible<F>(c: char, mut i: F) where F: FnMut(char) { d(c, &mut i, true); }
48
49 // FIXME(#19596) This is a workaround, we should use `F` instead of `&mut F`
50 fn d<F>(c: char, i: &mut F, k: bool) where F: FnMut(char) {
51 // 7-bit ASCII never decomposes
52 if c <= '\x7f' { (*i)(c); return; }
53
54 // Perform decomposition for Hangul
55 if (c as u32) >= S_BASE && (c as u32) < (S_BASE + S_COUNT) {
56 decompose_hangul(c, i);
57 return;
58 }
59
60 // First check the canonical decompositions
61 match bsearch_table(c, canonical_table) {
62 Some(canon) => {
63 for x in canon {
64 d(*x, i, k);
65 }
66 return;
67 }
68 None => ()
69 }
70
71 // Bottom out if we're not doing compat.
72 if !k { (*i)(c); return; }
73
74 // Then check the compatibility decompositions
75 match bsearch_table(c, compatibility_table) {
76 Some(compat) => {
77 for x in compat {
78 d(*x, i, k);
79 }
80 return;
81 }
82 None => ()
83 }
84
85 // Finally bottom out.
86 (*i)(c);
87 }
88
89 #[deprecated(reason = "use the crates.io `unicode-normalization` library instead",
90 since = "1.0.0")]
91 #[unstable(feature = "unicode",
92 reason = "this functionality will be moved to crates.io")]
93 pub fn compose(a: char, b: char) -> Option<char> {
94 compose_hangul(a, b).or_else(|| {
95 match bsearch_table(a, composition_table) {
96 None => None,
97 Some(candidates) => {
98 match candidates.binary_search_by(|&(val, _)| {
99 if b == val { Equal }
100 else if val < b { Less }
101 else { Greater }
102 }) {
103 Ok(idx) => {
104 let (_, result) = candidates[idx];
105 Some(result)
106 }
107 Err(_) => None
108 }
109 }
110 }
111 })
112 }
113
114 // Constants from Unicode 6.3.0 Section 3.12 Conjoining Jamo Behavior
115 const S_BASE: u32 = 0xAC00;
116 const L_BASE: u32 = 0x1100;
117 const V_BASE: u32 = 0x1161;
118 const T_BASE: u32 = 0x11A7;
119 const L_COUNT: u32 = 19;
120 const V_COUNT: u32 = 21;
121 const T_COUNT: u32 = 28;
122 const N_COUNT: u32 = (V_COUNT * T_COUNT);
123 const S_COUNT: u32 = (L_COUNT * N_COUNT);
124
125 // FIXME(#19596) This is a workaround, we should use `F` instead of `&mut F`
126 // Decompose a precomposed Hangul syllable
127 #[inline(always)]
128 fn decompose_hangul<F>(s: char, f: &mut F) where F: FnMut(char) {
129 use core::mem::transmute;
130
131 let si = s as u32 - S_BASE;
132
133 let li = si / N_COUNT;
134 unsafe {
135 (*f)(transmute(L_BASE + li));
136
137 let vi = (si % N_COUNT) / T_COUNT;
138 (*f)(transmute(V_BASE + vi));
139
140 let ti = si % T_COUNT;
141 if ti > 0 {
142 (*f)(transmute(T_BASE + ti));
143 }
144 }
145 }
146
147 // Compose a pair of Hangul Jamo
148 #[inline(always)]
149 fn compose_hangul(a: char, b: char) -> Option<char> {
150 use core::mem::transmute;
151 let l = a as u32;
152 let v = b as u32;
153 // Compose an LPart and a VPart
154 if L_BASE <= l && l < (L_BASE + L_COUNT) && V_BASE <= v && v < (V_BASE + V_COUNT) {
155 let r = S_BASE + (l - L_BASE) * N_COUNT + (v - V_BASE) * T_COUNT;
156 return unsafe { Some(transmute(r)) };
157 }
158 // Compose an LVPart and a TPart
159 if S_BASE <= l && l <= (S_BASE+S_COUNT-T_COUNT) && T_BASE <= v && v < (T_BASE+T_COUNT) {
160 let r = l + (v - T_BASE);
161 return unsafe { Some(transmute(r)) };
162 }
163 None
164 }