]> git.lizzy.rs Git - rust.git/blob - src/libcore/unicode/mod.rs
2a41685a4809683dcc2262fef7a79049927bf73b
[rust.git] / src / libcore / unicode / mod.rs
1 #![unstable(feature = "unicode_internals", issue = "none")]
2 #![allow(missing_docs)]
3
4 pub(crate) mod printable;
5 mod unicode_data;
6 pub(crate) mod version;
7
8 use version::UnicodeVersion;
9
10 /// The version of [Unicode](http://www.unicode.org/) that the Unicode parts of
11 /// `char` and `str` methods are based on.
12 #[unstable(feature = "unicode_version", issue = "49726")]
13 pub const UNICODE_VERSION: UnicodeVersion = UnicodeVersion {
14     major: unicode_data::UNICODE_VERSION.0,
15     minor: unicode_data::UNICODE_VERSION.1,
16     micro: unicode_data::UNICODE_VERSION.2,
17     _priv: (),
18 };
19
20 // For use in liballoc, not re-exported in libstd.
21 pub mod derived_property {
22     pub use super::{Case_Ignorable, Cased};
23 }
24
25 pub use unicode_data::alphabetic::lookup as Alphabetic;
26 pub use unicode_data::case_ignorable::lookup as Case_Ignorable;
27 pub use unicode_data::cased::lookup as Cased;
28 pub use unicode_data::cc::lookup as Cc;
29 pub use unicode_data::conversions;
30 pub use unicode_data::grapheme_extend::lookup as Grapheme_Extend;
31 pub use unicode_data::lowercase::lookup as Lowercase;
32 pub use unicode_data::n::lookup as N;
33 pub use unicode_data::uppercase::lookup as Uppercase;
34 pub use unicode_data::white_space::lookup as White_Space;
35
36 #[inline(always)]
37 fn range_search<
38     const N: usize,
39     const CHUNK_SIZE: usize,
40     const N1: usize,
41     const CANONICAL: usize,
42     const CANONICALIZED: usize,
43 >(
44     needle: u32,
45     chunk_idx_map: &[u8; N],
46     (last_chunk_idx, last_chunk_mapping): (u16, u8),
47     bitset_chunk_idx: &[[u8; CHUNK_SIZE]; N1],
48     bitset_canonical: &[u64; CANONICAL],
49     bitset_canonicalized: &[(u8, u8); CANONICALIZED],
50 ) -> bool {
51     let bucket_idx = (needle / 64) as usize;
52     let chunk_map_idx = bucket_idx / CHUNK_SIZE;
53     let chunk_piece = bucket_idx % CHUNK_SIZE;
54     let chunk_idx = if chunk_map_idx >= N {
55         if chunk_map_idx == last_chunk_idx as usize {
56             last_chunk_mapping
57         } else {
58             return false;
59         }
60     } else {
61         chunk_idx_map[chunk_map_idx]
62     };
63     let idx = bitset_chunk_idx[(chunk_idx as usize)][chunk_piece] as usize;
64     let word = if idx < CANONICAL {
65         bitset_canonical[idx]
66     } else {
67         let (real_idx, mapping) = bitset_canonicalized[idx - CANONICAL];
68         let mut word = bitset_canonical[real_idx as usize];
69         let should_invert = mapping & (1 << 7) != 0;
70         if should_invert {
71             word = !word;
72         }
73         // Unset the inversion bit
74         let rotate_by = mapping & !(1 << 7);
75         word = word.rotate_left(rotate_by as u32);
76         word
77     };
78     (word & (1 << (needle % 64) as u64)) != 0
79 }