]> git.lizzy.rs Git - rust.git/blob - src/libunicode/u_char.rs
369336639a7fe465b617b8c552cc0e42cffda939
[rust.git] / src / libunicode / u_char.rs
1 // Copyright 2012-2014 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 /*!
12  * Unicode-intensive `char` methods.
13  *
14  * These methods implement functionality for `char` that requires knowledge of
15  * Unicode definitions, including normalization, categorization, and display information.
16  */
17
18 use core::option::Option;
19 use tables::{derived_property, property, general_category, conversions, charwidth};
20
21 /// Returns whether the specified `char` is considered a Unicode alphabetic
22 /// code point
23 #[deprecated = "use UnicodeChar::is_alphabetic"]
24 pub fn is_alphabetic(c: char) -> bool {
25     c.is_alphabetic()
26 }
27
28 /// Returns whether the specified `char` satisfies the 'XID_Start' Unicode property
29 ///
30 /// 'XID_Start' is a Unicode Derived Property specified in
31 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
32 /// mostly similar to ID_Start but modified for closure under NFKx.
33 #[allow(non_snake_case)]
34 #[deprecated = "use UnicodeChar::is_XID_start"]
35 pub fn is_XID_start(c: char) -> bool    { derived_property::XID_Start(c) }
36
37 /// Returns whether the specified `char` satisfies the 'XID_Continue' Unicode property
38 ///
39 /// 'XID_Continue' is a Unicode Derived Property specified in
40 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
41 /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
42 #[allow(non_snake_case)]
43 #[deprecated = "use UnicodeChar::is_XID_continue"]
44 pub fn is_XID_continue(c: char) -> bool { derived_property::XID_Continue(c) }
45
46 ///
47 /// Indicates whether a `char` is in lower case
48 ///
49 /// This is defined according to the terms of the Unicode Derived Core Property 'Lowercase'.
50 ///
51 #[inline]
52 #[deprecated = "use UnicodeChar::is_lowercase"]
53 pub fn is_lowercase(c: char) -> bool {
54     c.is_lowercase()
55 }
56
57 ///
58 /// Indicates whether a `char` is in upper case
59 ///
60 /// This is defined according to the terms of the Unicode Derived Core Property 'Uppercase'.
61 ///
62 #[inline]
63 #[deprecated = "use UnicodeChar::is_uppercase"]
64 pub fn is_uppercase(c: char) -> bool {
65     c.is_uppercase()
66 }
67
68 ///
69 /// Indicates whether a `char` is whitespace
70 ///
71 /// Whitespace is defined in terms of the Unicode Property 'White_Space'.
72 ///
73 #[inline]
74 #[deprecated = "use UnicodeChar::is_whitespace"]
75 pub fn is_whitespace(c: char) -> bool {
76     c.is_whitespace()
77 }
78
79 ///
80 /// Indicates whether a `char` is alphanumeric
81 ///
82 /// Alphanumericness is defined in terms of the Unicode General Categories
83 /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
84 ///
85 #[inline]
86 #[deprecated = "use UnicodeChar::is_alphanumeric"]
87 pub fn is_alphanumeric(c: char) -> bool {
88     c.is_alphanumeric()
89 }
90
91 ///
92 /// Indicates whether a `char` is a control code point
93 ///
94 /// Control code points are defined in terms of the Unicode General Category
95 /// 'Cc'.
96 ///
97 #[inline]
98 #[deprecated = "use UnicodeChar::is_control"]
99 pub fn is_control(c: char) -> bool { general_category::Cc(c) }
100
101 /// Indicates whether the `char` is numeric (Nd, Nl, or No)
102 #[inline]
103 #[deprecated = "use UnicodeChar::is_numeric"]
104 pub fn is_digit(c: char) -> bool {
105     c.is_numeric()
106 }
107
108 /// Convert a char to its uppercase equivalent
109 ///
110 /// The case-folding performed is the common or simple mapping:
111 /// it maps one Unicode codepoint (one char in Rust) to its uppercase equivalent according
112 /// to the Unicode database at ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
113 /// The additional SpecialCasing.txt is not considered here, as it expands to multiple
114 /// codepoints in some cases.
115 ///
116 /// A full reference can be found here
117 /// http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
118 ///
119 /// # Return value
120 ///
121 /// Returns the char itself if no conversion was made
122 #[inline]
123 #[deprecated = "use UnicodeChar::to_uppercase"]
124 pub fn to_uppercase(c: char) -> char {
125     conversions::to_upper(c)
126 }
127
128 /// Convert a char to its lowercase equivalent
129 ///
130 /// The case-folding performed is the common or simple mapping
131 /// see `to_uppercase` for references and more information
132 ///
133 /// # Return value
134 ///
135 /// Returns the char itself if no conversion if possible
136 #[inline]
137 #[deprecated = "use UnicodeChar::to_lowercase"]
138 pub fn to_lowercase(c: char) -> char {
139     conversions::to_lower(c)
140 }
141
142 /// Returns this character's displayed width in columns, or `None` if it is a
143 /// control character other than `'\x00'`.
144 ///
145 /// `is_cjk` determines behavior for characters in the Ambiguous category:
146 /// if `is_cjk` is `true`, these are 2 columns wide; otherwise, they are 1.
147 /// In CJK contexts, `is_cjk` should be `true`, else it should be `false`.
148 /// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
149 /// recommends that these characters be treated as 1 column (i.e.,
150 /// `is_cjk` = `false`) if the context cannot be reliably determined.
151 #[deprecated = "use UnicodeChar::width"]
152 pub fn width(c: char, is_cjk: bool) -> Option<uint> {
153     charwidth::width(c, is_cjk)
154 }
155
156 /// Useful functions for Unicode characters.
157 #[experimental = "pending prelude organization"]
158 pub trait UnicodeChar {
159     /// Returns whether the specified character is considered a Unicode
160     /// alphabetic code point.
161     fn is_alphabetic(self) -> bool;
162
163     /// Returns whether the specified character satisfies the 'XID_Start'
164     /// Unicode property.
165     ///
166     /// 'XID_Start' is a Unicode Derived Property specified in
167     /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
168     /// mostly similar to ID_Start but modified for closure under NFKx.
169     #[allow(non_snake_case)]
170     #[deprecated = "use is_xid_start"]
171     fn is_XID_start(self) -> bool;
172
173     /// Returns whether the specified character satisfies the 'XID_Start'
174     /// Unicode property.
175     ///
176     /// 'XID_Start' is a Unicode Derived Property specified in
177     /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
178     /// mostly similar to ID_Start but modified for closure under NFKx.
179     fn is_xid_start(self) -> bool;
180
181     /// Returns whether the specified `char` satisfies the 'XID_Continue'
182     /// Unicode property.
183     ///
184     /// 'XID_Continue' is a Unicode Derived Property specified in
185     /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
186     /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
187     #[allow(non_snake_case)]
188     #[deprecated = "use is_xid_continue"]
189     fn is_XID_continue(self) -> bool;
190
191     /// Returns whether the specified `char` satisfies the 'XID_Continue'
192     /// Unicode property.
193     ///
194     /// 'XID_Continue' is a Unicode Derived Property specified in
195     /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
196     /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
197     fn is_xid_continue(self) -> bool;
198
199     /// Indicates whether a character is in lowercase.
200     ///
201     /// This is defined according to the terms of the Unicode Derived Core
202     /// Property `Lowercase`.
203     fn is_lowercase(self) -> bool;
204
205     /// Indicates whether a character is in uppercase.
206     ///
207     /// This is defined according to the terms of the Unicode Derived Core
208     /// Property `Uppercase`.
209     fn is_uppercase(self) -> bool;
210
211     /// Indicates whether a character is whitespace.
212     ///
213     /// Whitespace is defined in terms of the Unicode Property `White_Space`.
214     fn is_whitespace(self) -> bool;
215
216     /// Indicates whether a character is alphanumeric.
217     ///
218     /// Alphanumericness is defined in terms of the Unicode General Categories
219     /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
220     fn is_alphanumeric(self) -> bool;
221
222     /// Indicates whether a character is a control code point.
223     ///
224     /// Control code points are defined in terms of the Unicode General
225     /// Category `Cc`.
226     fn is_control(self) -> bool;
227
228     /// Indicates whether the character is numeric (Nd, Nl, or No).
229     fn is_numeric(self) -> bool;
230
231     /// Converts a character to its lowercase equivalent.
232     ///
233     /// The case-folding performed is the common or simple mapping. See
234     /// `to_uppercase()` for references and more information.
235     ///
236     /// # Return value
237     ///
238     /// Returns the lowercase equivalent of the character, or the character
239     /// itself if no conversion is possible.
240     fn to_lowercase(self) -> char;
241
242     /// Converts a character to its uppercase equivalent.
243     ///
244     /// The case-folding performed is the common or simple mapping: it maps
245     /// one Unicode codepoint (one character in Rust) to its uppercase
246     /// equivalent according to the Unicode database [1]. The additional
247     /// [`SpecialCasing.txt`] is not considered here, as it expands to multiple
248     /// codepoints in some cases.
249     ///
250     /// A full reference can be found here [2].
251     ///
252     /// # Return value
253     ///
254     /// Returns the uppercase equivalent of the character, or the character
255     /// itself if no conversion was made.
256     ///
257     /// [1]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
258     ///
259     /// [`SpecialCasing`.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
260     ///
261     /// [2]: http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
262     fn to_uppercase(self) -> char;
263
264     /// Returns this character's displayed width in columns, or `None` if it is a
265     /// control character other than `'\x00'`.
266     ///
267     /// `is_cjk` determines behavior for characters in the Ambiguous category:
268     /// if `is_cjk` is `true`, these are 2 columns wide; otherwise, they are 1.
269     /// In CJK contexts, `is_cjk` should be `true`, else it should be `false`.
270     /// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
271     /// recommends that these characters be treated as 1 column (i.e.,
272     /// `is_cjk` = `false`) if the context cannot be reliably determined.
273     #[experimental = "needs expert opinion. is_cjk flag stands out as ugly"]
274     fn width(self, is_cjk: bool) -> Option<uint>;
275 }
276
277 #[experimental = "pending prelude organization"]
278 impl UnicodeChar for char {
279     fn is_alphabetic(self) -> bool {
280         match self {
281             'a' ... 'z' | 'A' ... 'Z' => true,
282             c if c > '\x7f' => derived_property::Alphabetic(c),
283             _ => false
284         }
285     }
286
287     #[deprecated = "use is_xid_start"]
288     fn is_XID_start(self) -> bool { derived_property::XID_Start(self) }
289
290     #[deprecated = "use is_xid_continue"]
291     fn is_XID_continue(self) -> bool { derived_property::XID_Continue(self) }
292
293     fn is_xid_start(self) -> bool { derived_property::XID_Start(self) }
294
295     fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) }
296
297     fn is_lowercase(self) -> bool {
298         match self {
299             'a' ... 'z' => true,
300             c if c > '\x7f' => derived_property::Lowercase(c),
301             _ => false
302         }
303     }
304
305     fn is_uppercase(self) -> bool {
306         match self {
307             'A' ... 'Z' => true,
308             c if c > '\x7f' => derived_property::Uppercase(c),
309             _ => false
310         }
311     }
312
313     fn is_whitespace(self) -> bool {
314         match self {
315             ' ' | '\x09' ... '\x0d' => true,
316             c if c > '\x7f' => property::White_Space(c),
317             _ => false
318         }
319     }
320
321     fn is_alphanumeric(self) -> bool {
322         self.is_alphabetic() || self.is_numeric()
323     }
324
325     fn is_control(self) -> bool { general_category::Cc(self) }
326
327     fn is_numeric(self) -> bool {
328         match self {
329             '0' ... '9' => true,
330             c if c > '\x7f' => general_category::N(c),
331             _ => false
332         }
333     }
334
335     fn to_lowercase(self) -> char { conversions::to_lower(self) }
336
337     fn to_uppercase(self) -> char { conversions::to_upper(self) }
338
339     #[experimental = "needs expert opinion. is_cjk flag stands out as ugly"]
340     fn width(self, is_cjk: bool) -> Option<uint> { charwidth::width(self, is_cjk) }
341 }