| 1 | pub mod general; |
| 2 | pub use self::general::*; |
| 3 | |
| 4 | pub mod bidi; |
| 5 | pub use self::bidi::*; |
| 6 | |
| 7 | pub mod misc; |
| 8 | pub use self::misc::*; |
| 9 | |
| 10 | pub mod scripts; |
| 11 | pub use self::scripts::*; |
| 12 | |
| 13 | pub mod function; |
| 14 | pub use self::function::*; |
| 15 | |
| 16 | pub mod bool; |
| 17 | pub use self::bool::*; |
| 18 | |
| 19 | pub mod rem; |
| 20 | pub use self::rem::*; |
| 21 | |
| 22 | pub mod decomp; |
| 23 | pub use self::decomp::*; |
| 24 | |
| 25 | use core::cmp::Ordering::{Equal, Less, Greater}; |
| 26 | use core::char; |
| 27 | |
| 28 | pub trait Search { |
| 29 | type T: Clone; |
| 30 | fn search(&self, cp: char) -> Option<Self::T>; |
| 31 | fn includes(&self, cp: char) -> bool { |
| 32 | match self.search(cp) { |
| 33 | Some(_) => true, |
| 34 | None => false |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<S: Clone> Search for [((u8,u8,u8),(u8,u8,u8),S)] { |
| 40 | type T = S; |
| 41 | fn search(&self, cp: char) -> Option<S> { |
| 42 | let cp = cp as u32; |
| 43 | match self.binary_search_by(|&((rb1,rb2,rb3), (re1,re2,re3), _)| { |
| 44 | let rb: u32 = (rb1 as u32)*65536 + (rb2 as u32)*256 + (rb3 as u32); |
| 45 | let re: u32 = (re1 as u32)*65536 + (re2 as u32)*256 + (re3 as u32); |
| 46 | if rb <= cp && cp <= re { Equal } |
| 47 | else if re < cp { Less } |
| 48 | else { Greater } |
| 49 | }) { |
| 50 | Ok(idx) => { |
| 51 | let (_, _, ref v) = self[idx]; |
| 52 | Some(v.clone()) |
| 53 | }, |
| 54 | _ => None |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<S: Clone> Search for [((u8,u8,u8),S)] { |
| 60 | type T = S; |
| 61 | fn search(&self, cp: char) -> Option<S> { |
| 62 | let ca = cp as u32; |
| 63 | match self.binary_search_by(|&((cb1,cb2,cb3), _)| { |
| 64 | let cb: u32 = (cb1 as u32)*65536 + (cb2 as u32)*256 + (cb3 as u32); |
| 65 | cb.cmp(&ca) |
| 66 | }) { |
| 67 | Ok(idx) => { |
| 68 | let (_, ref v) = self[idx]; |
| 69 | Some(v.clone()) |
| 70 | }, |
| 71 | _ => None |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl Search for [(u16,u16)] { |
| 77 | type T = char; |
| 78 | fn search(&self, cp: char) -> Option<char> { |
| 79 | let ca = cp as u32; |
| 80 | let cb = ca as u16; |
| 81 | |
| 82 | if ca > 65536 { return None; } |
| 83 | |
| 84 | match self.binary_search_by(|&(cc,_)| cc.cmp(&cb)) { |
| 85 | Ok(idx) => { |
| 86 | let (_, v) = self[idx]; |
| 87 | char::from_u32(v as u32) |
| 88 | }, |
| 89 | _ => None |
| 90 | } |
| 91 | } |
| 92 | } |