]> git.lizzy.rs Git - rust.git/blob - src/libextra/unicode.rs
auto merge of #8111 : brson/rust/no-bench, r=graydon
[rust.git] / src / libextra / unicode.rs
1 // Copyright 2012 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.
4 //
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.
10
11 #[forbid(deprecated_mode)];
12 #[allow(missing_doc)];
13
14 pub mod icu {
15     pub type UBool = u8;
16     pub type UProperty = int;
17     pub type UChar32 = char;
18
19     pub static TRUE : u8 = 1u8;
20     pub static FALSE : u8 = 0u8;
21
22     pub static UCHAR_ALPHABETIC : UProperty = 0;
23     pub static UCHAR_BINARY_START : UProperty = 0; // = UCHAR_ALPHABETIC
24     pub static UCHAR_ASCII_HEX_DIGIT : UProperty = 1;
25     pub static UCHAR_BIDI_CONTROL : UProperty = 2;
26
27     pub static UCHAR_BIDI_MIRRORED : UProperty = 3;
28     pub static UCHAR_DASH : UProperty = 4;
29     pub static UCHAR_DEFAULT_IGNORABLE_CODE_POINT : UProperty = 5;
30     pub static UCHAR_DEPRECATED : UProperty = 6;
31
32     pub static UCHAR_DIACRITIC : UProperty = 7;
33     pub static UCHAR_EXTENDER : UProperty = 8;
34     pub static UCHAR_FULL_COMPOSITION_EXCLUSION : UProperty = 9;
35     pub static UCHAR_GRAPHEME_BASE : UProperty = 10;
36
37     pub static UCHAR_GRAPHEME_EXTEND : UProperty = 11;
38     pub static UCHAR_GRAPHEME_LINK : UProperty = 12;
39     pub static UCHAR_HEX_DIGIT : UProperty = 13;
40     pub static UCHAR_HYPHEN : UProperty = 14;
41
42     pub static UCHAR_ID_CONTINUE : UProperty = 15;
43     pub static UCHAR_ID_START : UProperty = 16;
44     pub static UCHAR_IDEOGRAPHIC : UProperty = 17;
45     pub static UCHAR_IDS_BINARY_OPERATOR : UProperty = 18;
46
47     pub static UCHAR_IDS_TRINARY_OPERATOR : UProperty = 19;
48     pub static UCHAR_JOIN_CONTROL : UProperty = 20;
49     pub static UCHAR_LOGICAL_ORDER_EXCEPTION : UProperty = 21;
50     pub static UCHAR_LOWERCASE : UProperty = 22;
51
52     pub static UCHAR_MATH : UProperty = 23;
53     pub static UCHAR_NONCHARACTER_CODE_POINT : UProperty = 24;
54     pub static UCHAR_QUOTATION_MARK : UProperty = 25;
55     pub static UCHAR_RADICAL : UProperty = 26;
56
57     pub static UCHAR_SOFT_DOTTED : UProperty = 27;
58     pub static UCHAR_TERMINAL_PUNCTUATION : UProperty = 28;
59     pub static UCHAR_UNIFIED_IDEOGRAPH : UProperty = 29;
60     pub static UCHAR_UPPERCASE : UProperty = 30;
61
62     pub static UCHAR_WHITE_SPACE : UProperty = 31;
63     pub static UCHAR_XID_CONTINUE : UProperty = 32;
64     pub static UCHAR_XID_START : UProperty = 33;
65     pub static UCHAR_CASE_SENSITIVE : UProperty = 34;
66
67     pub static UCHAR_S_TERM : UProperty = 35;
68     pub static UCHAR_VARIATION_SELECTOR : UProperty = 36;
69     pub static UCHAR_NFD_INERT : UProperty = 37;
70     pub static UCHAR_NFKD_INERT : UProperty = 38;
71
72     pub static UCHAR_NFC_INERT : UProperty = 39;
73     pub static UCHAR_NFKC_INERT : UProperty = 40;
74     pub static UCHAR_SEGMENT_STARTER : UProperty = 41;
75     pub static UCHAR_PATTERN_SYNTAX : UProperty = 42;
76
77     pub static UCHAR_PATTERN_WHITE_SPACE : UProperty = 43;
78     pub static UCHAR_POSIX_ALNUM : UProperty = 44;
79     pub static UCHAR_POSIX_BLANK : UProperty = 45;
80     pub static UCHAR_POSIX_GRAPH : UProperty = 46;
81
82     pub static UCHAR_POSIX_PRINT : UProperty = 47;
83     pub static UCHAR_POSIX_XDIGIT : UProperty = 48;
84     pub static UCHAR_CASED : UProperty = 49;
85     pub static UCHAR_CASE_IGNORABLE : UProperty = 50;
86
87     pub static UCHAR_CHANGES_WHEN_LOWERCASED : UProperty = 51;
88     pub static UCHAR_CHANGES_WHEN_UPPERCASED : UProperty = 52;
89     pub static UCHAR_CHANGES_WHEN_TITLECASED : UProperty = 53;
90     pub static UCHAR_CHANGES_WHEN_CASEFOLDED : UProperty = 54;
91
92     pub static UCHAR_CHANGES_WHEN_CASEMAPPED : UProperty = 55;
93     pub static UCHAR_CHANGES_WHEN_NFKC_CASEFOLDED : UProperty = 56;
94     pub static UCHAR_BINARY_LIMIT : UProperty = 57;
95     pub static UCHAR_BIDI_CLASS : UProperty = 0x1000;
96
97     pub static UCHAR_INT_START : UProperty = 0x1000; // UCHAR_BIDI_CLASS
98     pub static UCHAR_BLOCK : UProperty = 0x1001;
99     pub static UCHAR_CANONICAL_COMBINING_CLASS : UProperty = 0x1002;
100     pub static UCHAR_DECOMPOSITION_TYPE : UProperty = 0x1003;
101
102     pub static UCHAR_EAST_ASIAN_WIDTH : UProperty = 0x1004;
103     pub static UCHAR_GENERAL_CATEGORY : UProperty = 0x1005;
104     pub static UCHAR_JOINING_GROUP : UProperty = 0x1006;
105     pub static UCHAR_JOINING_TYPE : UProperty = 0x1007;
106
107     pub static UCHAR_LINE_BREAK : UProperty = 0x1008;
108     pub static UCHAR_NUMERIC_TYPE : UProperty = 0x1009;
109     pub static UCHAR_SCRIPT : UProperty = 0x100A;
110     pub static UCHAR_HANGUL_SYLLABLE_TYPE : UProperty = 0x100B;
111
112     pub static UCHAR_NFD_QUICK_CHECK : UProperty = 0x100C;
113     pub static UCHAR_NFKD_QUICK_CHECK : UProperty = 0x100D;
114     pub static UCHAR_NFC_QUICK_CHECK : UProperty = 0x100E;
115     pub static UCHAR_NFKC_QUICK_CHECK : UProperty = 0x100F;
116
117     pub static UCHAR_LEAD_CANONICAL_COMBINING_CLASS : UProperty = 0x1010;
118     pub static UCHAR_TRAIL_CANONICAL_COMBINING_CLASS : UProperty = 0x1011;
119     pub static UCHAR_GRAPHEME_CLUSTER_BREAK : UProperty = 0x1012;
120     pub static UCHAR_SENTENCE_BREAK : UProperty = 0x1013;
121
122     pub static UCHAR_WORD_BREAK : UProperty = 0x1014;
123     pub static UCHAR_INT_LIMIT : UProperty = 0x1015;
124
125     pub static UCHAR_GENERAL_CATEGORY_MASK : UProperty = 0x2000;
126     pub static UCHAR_MASK_START : UProperty = 0x2000;
127     // = UCHAR_GENERAL_CATEGORY_MASK
128     pub static UCHAR_MASK_LIMIT : UProperty = 0x2001;
129
130     pub static UCHAR_NUMERIC_VALUE : UProperty = 0x3000;
131     pub static UCHAR_DOUBLE_START : UProperty = 0x3000;
132     // = UCHAR_NUMERIC_VALUE
133     pub static UCHAR_DOUBLE_LIMIT : UProperty = 0x3001;
134
135     pub static UCHAR_AGE : UProperty = 0x4000;
136     pub static UCHAR_STRING_START : UProperty = 0x4000; // = UCHAR_AGE
137     pub static UCHAR_BIDI_MIRRORING_GLYPH : UProperty = 0x4001;
138     pub static UCHAR_CASE_FOLDING : UProperty = 0x4002;
139
140     pub static UCHAR_ISO_COMMENT : UProperty = 0x4003;
141     pub static UCHAR_LOWERCASE_MAPPING : UProperty = 0x4004;
142     pub static UCHAR_NAME : UProperty = 0x4005;
143     pub static UCHAR_SIMPLE_CASE_FOLDING : UProperty = 0x4006;
144
145     pub static UCHAR_SIMPLE_LOWERCASE_MAPPING : UProperty = 0x4007;
146     pub static UCHAR_SIMPLE_TITLECASE_MAPPING : UProperty = 0x4008;
147     pub static UCHAR_SIMPLE_UPPERCASE_MAPPING : UProperty = 0x4009;
148     pub static UCHAR_TITLECASE_MAPPING : UProperty = 0x400A;
149
150     pub static UCHAR_UNICODE_1_NAME : UProperty = 0x400B;
151     pub static UCHAR_UPPERCASE_MAPPING : UProperty = 0x400C;
152     pub static UCHAR_STRING_LIMIT : UProperty = 0x400D;
153
154     pub static UCHAR_SCRIPT_EXTENSIONS : UProperty = 0x7000;
155     pub static UCHAR_OTHER_PROPERTY_START : UProperty = 0x7000;
156     // = UCHAR_SCRIPT_EXTENSIONS;
157     pub static UCHAR_OTHER_PROPERTY_LIMIT : UProperty = 0x7001;
158
159     pub static UCHAR_INVALID_CODE : UProperty = -1;
160
161     pub mod libicu {
162         #[link_name = "icuuc"]
163         #[abi = "cdecl"]
164         extern {
165             pub unsafe fn u_hasBinaryProperty(c: UChar32, which: UProperty)
166                                               -> UBool;
167             pub unsafe fn u_isdigit(c: UChar32) -> UBool;
168             pub unsafe fn u_islower(c: UChar32) -> UBool;
169             pub unsafe fn u_isspace(c: UChar32) -> UBool;
170             pub unsafe fn u_isupper(c: UChar32) -> UBool;
171             pub unsafe fn u_tolower(c: UChar32) -> UChar32;
172             pub unsafe fn u_toupper(c: UChar32) -> UChar32;
173         }
174     }
175 }
176
177 pub fn is_XID_start(c: char) -> bool {
178     return icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START)
179         == icu::TRUE;
180 }
181
182 pub fn is_XID_continue(c: char) -> bool {
183     return icu::libicu::u_hasBinaryProperty(c, icu::UCHAR_XID_START)
184         == icu::TRUE;
185 }
186
187 /*
188 Function: is_digit
189
190 Returns true if a character is a digit.
191 */
192 pub fn is_digit(c: char) -> bool {
193     return icu::libicu::u_isdigit(c) == icu::TRUE;
194 }
195
196 /*
197 Function: is_lower
198
199 Returns true if a character is a lowercase letter.
200 */
201 pub fn is_lower(c: char) -> bool {
202     return icu::libicu::u_islower(c) == icu::TRUE;
203 }
204
205 /*
206 Function: is_space
207
208 Returns true if a character is space.
209 */
210 pub fn is_space(c: char) -> bool {
211     return icu::libicu::u_isspace(c) == icu::TRUE;
212 }
213
214 /*
215 Function: is_upper
216
217 Returns true if a character is an uppercase letter.
218 */
219 pub fn is_upper(c: char) -> bool {
220     return icu::libicu::u_isupper(c) == icu::TRUE;
221 }
222
223 #[cfg(test)]
224 mod tests {
225
226     #[test]
227     fn test_is_digit() {
228         assert!((unicode::icu::is_digit('0')));
229         assert!((!unicode::icu::is_digit('m')));
230     }
231
232     #[test]
233     fn test_is_lower() {
234         assert!((unicode::icu::is_lower('m')));
235         assert!((!unicode::icu::is_lower('M')));
236     }
237
238     #[test]
239     fn test_is_space() {
240         assert!((unicode::icu::is_space(' ')));
241         assert!((!unicode::icu::is_space('m')));
242     }
243
244     #[test]
245     fn test_is_upper() {
246         assert!((unicode::icu::is_upper('M')));
247         assert!((!unicode::icu::is_upper('m')));
248     }
249 }