about summary refs log tree commit diff
path: root/src/libunicode/normalize.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libunicode/normalize.rs')
-rw-r--r--src/libunicode/normalize.rs41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/libunicode/normalize.rs b/src/libunicode/normalize.rs
index d239cb82896..c6f86ccd9d4 100644
--- a/src/libunicode/normalize.rs
+++ b/src/libunicode/normalize.rs
@@ -11,35 +11,37 @@
 //! Functions for computing canonical and compatible decompositions for Unicode characters.
 
 use core::cmp::Ordering::{Equal, Less, Greater};
+use core::ops::FnMut;
 use core::option::Option;
 use core::option::Option::{Some, None};
-use core::slice;
 use core::slice::SliceExt;
+use core::result::Result::{Ok, Err};
 use tables::normalization::{canonical_table, compatibility_table, composition_table};
 
 fn bsearch_table<T>(c: char, r: &'static [(char, &'static [T])]) -> Option<&'static [T]> {
-    match r.binary_search(|&(val, _)| {
+    match r.binary_search_by(|&(val, _)| {
         if c == val { Equal }
         else if val < c { Less }
         else { Greater }
     }) {
-        slice::BinarySearchResult::Found(idx) => {
+        Ok(idx) => {
             let (_, result) = r[idx];
             Some(result)
         }
-        slice::BinarySearchResult::NotFound(_) => None
+        Err(_) => None
     }
 }
 
 /// Compute canonical Unicode decomposition for character
-pub fn decompose_canonical(c: char, i: |char|) { d(c, i, false); }
+pub fn decompose_canonical<F>(c: char, mut i: F) where F: FnMut(char) { d(c, &mut i, false); }
 
 /// Compute canonical or compatible Unicode decomposition for character
-pub fn decompose_compatible(c: char, i: |char|) { d(c, i, true); }
+pub fn decompose_compatible<F>(c: char, mut i: F) where F: FnMut(char) { d(c, &mut i, true); }
 
-fn d(c: char, i: |char|, k: bool) {
+// FIXME(#19596) This is a workaround, we should use `F` instead of `&mut F`
+fn d<F>(c: char, i: &mut F, k: bool) where F: FnMut(char) {
     // 7-bit ASCII never decomposes
-    if c <= '\x7f' { i(c); return; }
+    if c <= '\x7f' { (*i)(c); return; }
 
     // Perform decomposition for Hangul
     if (c as u32) >= S_BASE && (c as u32) < (S_BASE + S_COUNT) {
@@ -51,7 +53,7 @@ fn d(c: char, i: |char|, k: bool) {
     match bsearch_table(c, canonical_table) {
         Some(canon) => {
             for x in canon.iter() {
-                d(*x, |b| i(b), k);
+                d(*x, i, k);
             }
             return;
         }
@@ -59,13 +61,13 @@ fn d(c: char, i: |char|, k: bool) {
     }
 
     // Bottom out if we're not doing compat.
-    if !k { i(c); return; }
+    if !k { (*i)(c); return; }
 
     // Then check the compatibility decompositions
     match bsearch_table(c, compatibility_table) {
         Some(compat) => {
             for x in compat.iter() {
-                d(*x, |b| i(b), k);
+                d(*x, i, k);
             }
             return;
         }
@@ -73,7 +75,7 @@ fn d(c: char, i: |char|, k: bool) {
     }
 
     // Finally bottom out.
-    i(c);
+    (*i)(c);
 }
 
 pub fn compose(a: char, b: char) -> Option<char> {
@@ -81,16 +83,16 @@ pub fn compose(a: char, b: char) -> Option<char> {
         match bsearch_table(a, composition_table) {
             None => None,
             Some(candidates) => {
-                match candidates.binary_search(|&(val, _)| {
+                match candidates.binary_search_by(|&(val, _)| {
                     if b == val { Equal }
                     else if val < b { Less }
                     else { Greater }
                 }) {
-                    slice::BinarySearchResult::Found(idx) => {
+                    Ok(idx) => {
                         let (_, result) = candidates[idx];
                         Some(result)
                     }
-                    slice::BinarySearchResult::NotFound(_) => None
+                    Err(_) => None
                 }
             }
         }
@@ -108,23 +110,24 @@ const T_COUNT: u32 = 28;
 const N_COUNT: u32 = (V_COUNT * T_COUNT);
 const S_COUNT: u32 = (L_COUNT * N_COUNT);
 
+// FIXME(#19596) This is a workaround, we should use `F` instead of `&mut F`
 // Decompose a precomposed Hangul syllable
 #[inline(always)]
-fn decompose_hangul(s: char, f: |char|) {
+fn decompose_hangul<F>(s: char, f: &mut F) where F: FnMut(char) {
     use core::mem::transmute;
 
     let si = s as u32 - S_BASE;
 
     let li = si / N_COUNT;
     unsafe {
-        f(transmute(L_BASE + li));
+        (*f)(transmute(L_BASE + li));
 
         let vi = (si % N_COUNT) / T_COUNT;
-        f(transmute(V_BASE + vi));
+        (*f)(transmute(V_BASE + vi));
 
         let ti = si % T_COUNT;
         if ti > 0 {
-            f(transmute(T_BASE + ti));
+            (*f)(transmute(T_BASE + ti));
         }
     }
 }