11
11
//! Functions for computing canonical and compatible decompositions for Unicode characters.
12
12
13
13
use core:: cmp:: Ordering :: { Equal , Less , Greater } ;
14
+ use core:: ops:: FnMut ;
14
15
use core:: option:: Option ;
15
16
use core:: option:: Option :: { Some , None } ;
16
17
use core:: slice:: SliceExt ;
@@ -32,14 +33,15 @@ fn bsearch_table<T>(c: char, r: &'static [(char, &'static [T])]) -> Option<&'sta
32
33
}
33
34
34
35
/// Compute canonical Unicode decomposition for character
35
- pub fn decompose_canonical ( c : char , i: | char| ) { d ( c, i, false ) ; }
36
+ pub fn decompose_canonical < F > ( c : char , mut i : F ) where F : FnMut ( char ) { d ( c, & mut i, false ) ; }
36
37
37
38
/// Compute canonical or compatible Unicode decomposition for character
38
- pub fn decompose_compatible ( c : char , i: | char| ) { d ( c, i, true ) ; }
39
+ pub fn decompose_compatible < F > ( c : char , mut i : F ) where F : FnMut ( char ) { d ( c, & mut i, true ) ; }
39
40
40
- fn d ( c : char , i: |char|, k: bool ) {
41
+ // FIXME(#19596) This is a workaround, we should use `F` instead of `&mut F`
42
+ fn d < F > ( c : char , i : & mut F , k : bool ) where F : FnMut ( char ) {
41
43
// 7-bit ASCII never decomposes
42
- if c <= '\x7f' { i ( c) ; return ; }
44
+ if c <= '\x7f' { ( * i ) ( c) ; return ; }
43
45
44
46
// Perform decomposition for Hangul
45
47
if ( c as u32 ) >= S_BASE && ( c as u32 ) < ( S_BASE + S_COUNT ) {
@@ -51,29 +53,29 @@ fn d(c: char, i: |char|, k: bool) {
51
53
match bsearch_table ( c, canonical_table) {
52
54
Some ( canon) => {
53
55
for x in canon. iter ( ) {
54
- d ( * x, |b| i ( b ) , k) ;
56
+ d ( * x, i , k) ;
55
57
}
56
58
return ;
57
59
}
58
60
None => ( )
59
61
}
60
62
61
63
// Bottom out if we're not doing compat.
62
- if !k { i ( c) ; return ; }
64
+ if !k { ( * i ) ( c) ; return ; }
63
65
64
66
// Then check the compatibility decompositions
65
67
match bsearch_table ( c, compatibility_table) {
66
68
Some ( compat) => {
67
69
for x in compat. iter ( ) {
68
- d ( * x, |b| i ( b ) , k) ;
70
+ d ( * x, i , k) ;
69
71
}
70
72
return ;
71
73
}
72
74
None => ( )
73
75
}
74
76
75
77
// Finally bottom out.
76
- i ( c) ;
78
+ ( * i ) ( c) ;
77
79
}
78
80
79
81
pub fn compose ( a : char , b : char ) -> Option < char > {
@@ -108,23 +110,24 @@ const T_COUNT: u32 = 28;
108
110
const N_COUNT : u32 = ( V_COUNT * T_COUNT ) ;
109
111
const S_COUNT : u32 = ( L_COUNT * N_COUNT ) ;
110
112
113
+ // FIXME(#19596) This is a workaround, we should use `F` instead of `&mut F`
111
114
// Decompose a precomposed Hangul syllable
112
115
#[ inline( always) ]
113
- fn decompose_hangul ( s : char , f: | char| ) {
116
+ fn decompose_hangul < F > ( s : char , f : & mut F ) where F : FnMut ( char ) {
114
117
use core:: mem:: transmute;
115
118
116
119
let si = s as u32 - S_BASE ;
117
120
118
121
let li = si / N_COUNT ;
119
122
unsafe {
120
- f ( transmute ( L_BASE + li) ) ;
123
+ ( * f ) ( transmute ( L_BASE + li) ) ;
121
124
122
125
let vi = ( si % N_COUNT ) / T_COUNT ;
123
- f ( transmute ( V_BASE + vi) ) ;
126
+ ( * f ) ( transmute ( V_BASE + vi) ) ;
124
127
125
128
let ti = si % T_COUNT ;
126
129
if ti > 0 {
127
- f ( transmute ( T_BASE + ti) ) ;
130
+ ( * f ) ( transmute ( T_BASE + ti) ) ;
128
131
}
129
132
}
130
133
}
0 commit comments