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.
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.
11 //! Functions for computing canonical and compatible decompositions for Unicode characters.
13 use core
::cmp
::Ordering
::{Equal, Less, Greater}
;
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}
;
21 fn bsearch_table
<T
>(c
: char, r
: &'
static [(char, &'
static [T
])]) -> Option
<&'
static [T
]> {
22 match r
.binary_search_by(|&(val
, _
)| {
24 else if val
< c { Less }
28 let (_
, result
) = r
[idx
];
35 /// Compute canonical Unicode decomposition for character
36 #[deprecated(reason = "use the crates.io `unicode-normalization` library instead",
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); }
42 /// Compute canonical or compatible Unicode decomposition for character
43 #[deprecated(reason = "use the crates.io `unicode-normalization` library instead",
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); }
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; }
54 // Perform decomposition for Hangul
55 if (c
as u32) >= S_BASE
&& (c
as u32) < (S_BASE
+ S_COUNT
) {
56 decompose_hangul(c
, i
);
60 // First check the canonical decompositions
61 match bsearch_table(c
, canonical_table
) {
71 // Bottom out if we're not doing compat.
72 if !k { (*i)(c); return; }
74 // Then check the compatibility decompositions
75 match bsearch_table(c
, compatibility_table
) {
85 // Finally bottom out.
89 #[deprecated(reason = "use the crates.io `unicode-normalization` library instead",
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
) {
98 match candidates
.binary_search_by(|&(val
, _
)| {
100 else if val
< b { Less }
104 let (_
, result
) = candidates
[idx
];
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
);
125 // FIXME(#19596) This is a workaround, we should use `F` instead of `&mut F`
126 // Decompose a precomposed Hangul syllable
128 fn decompose_hangul
<F
>(s
: char, f
: &mut F
) where F
: FnMut(char) {
129 use core
::mem
::transmute
;
131 let si
= s
as u32 - S_BASE
;
133 let li
= si
/ N_COUNT
;
135 (*f
)(transmute(L_BASE
+ li
));
137 let vi
= (si
% N_COUNT
) / T_COUNT
;
138 (*f
)(transmute(V_BASE
+ vi
));
140 let ti
= si
% T_COUNT
;
142 (*f
)(transmute(T_BASE
+ ti
));
147 // Compose a pair of Hangul Jamo
149 fn compose_hangul(a
: char, b
: char) -> Option
<char> {
150 use core
::mem
::transmute
;
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)) }
;
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)) }
;