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