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.
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.
12 * Unicode-intensive `char` methods.
14 * These methods implement functionality for `char` that requires knowledge of
15 * Unicode definitions, including normalization, categorization, and display information.
18 use core::option::Option;
19 use tables::{derived_property, property, general_category, conversions, charwidth};
21 /// Returns whether the specified `char` is considered a Unicode alphabetic
23 #[deprecated = "use UnicodeChar::is_alphabetic"]
24 pub fn is_alphabetic(c: char) -> bool {
28 /// Returns whether the specified `char` satisfies the 'XID_Start' Unicode property
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) }
37 /// Returns whether the specified `char` satisfies the 'XID_Continue' Unicode property
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) }
47 /// Indicates whether a `char` is in lower case
49 /// This is defined according to the terms of the Unicode Derived Core Property 'Lowercase'.
52 #[deprecated = "use UnicodeChar::is_lowercase"]
53 pub fn is_lowercase(c: char) -> bool {
58 /// Indicates whether a `char` is in upper case
60 /// This is defined according to the terms of the Unicode Derived Core Property 'Uppercase'.
63 #[deprecated = "use UnicodeChar::is_uppercase"]
64 pub fn is_uppercase(c: char) -> bool {
69 /// Indicates whether a `char` is whitespace
71 /// Whitespace is defined in terms of the Unicode Property 'White_Space'.
74 #[deprecated = "use UnicodeChar::is_whitespace"]
75 pub fn is_whitespace(c: char) -> bool {
80 /// Indicates whether a `char` is alphanumeric
82 /// Alphanumericness is defined in terms of the Unicode General Categories
83 /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
86 #[deprecated = "use UnicodeChar::is_alphanumeric"]
87 pub fn is_alphanumeric(c: char) -> bool {
92 /// Indicates whether a `char` is a control code point
94 /// Control code points are defined in terms of the Unicode General Category
98 #[deprecated = "use UnicodeChar::is_control"]
99 pub fn is_control(c: char) -> bool { general_category::Cc(c) }
101 /// Indicates whether the `char` is numeric (Nd, Nl, or No)
103 #[deprecated = "use UnicodeChar::is_numeric"]
104 pub fn is_digit(c: char) -> bool {
108 /// Convert a char to its uppercase equivalent
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.
116 /// A full reference can be found here
117 /// http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
121 /// Returns the char itself if no conversion was made
123 #[deprecated = "use UnicodeChar::to_uppercase"]
124 pub fn to_uppercase(c: char) -> char {
125 conversions::to_upper(c)
128 /// Convert a char to its lowercase equivalent
130 /// The case-folding performed is the common or simple mapping
131 /// see `to_uppercase` for references and more information
135 /// Returns the char itself if no conversion if possible
137 #[deprecated = "use UnicodeChar::to_lowercase"]
138 pub fn to_lowercase(c: char) -> char {
139 conversions::to_lower(c)
142 /// Returns this character's displayed width in columns, or `None` if it is a
143 /// control character other than `'\x00'`.
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)
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;
163 /// Returns whether the specified character satisfies the 'XID_Start'
164 /// Unicode property.
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;
173 /// Returns whether the specified character satisfies the 'XID_Start'
174 /// Unicode property.
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;
181 /// Returns whether the specified `char` satisfies the 'XID_Continue'
182 /// Unicode property.
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;
191 /// Returns whether the specified `char` satisfies the 'XID_Continue'
192 /// Unicode property.
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;
199 /// Indicates whether a character is in lowercase.
201 /// This is defined according to the terms of the Unicode Derived Core
202 /// Property `Lowercase`.
203 fn is_lowercase(self) -> bool;
205 /// Indicates whether a character is in uppercase.
207 /// This is defined according to the terms of the Unicode Derived Core
208 /// Property `Uppercase`.
209 fn is_uppercase(self) -> bool;
211 /// Indicates whether a character is whitespace.
213 /// Whitespace is defined in terms of the Unicode Property `White_Space`.
214 fn is_whitespace(self) -> bool;
216 /// Indicates whether a character is alphanumeric.
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;
222 /// Indicates whether a character is a control code point.
224 /// Control code points are defined in terms of the Unicode General
226 fn is_control(self) -> bool;
228 /// Indicates whether the character is numeric (Nd, Nl, or No).
229 fn is_numeric(self) -> bool;
231 /// Converts a character to its lowercase equivalent.
233 /// The case-folding performed is the common or simple mapping. See
234 /// `to_uppercase()` for references and more information.
238 /// Returns the lowercase equivalent of the character, or the character
239 /// itself if no conversion is possible.
240 fn to_lowercase(self) -> char;
242 /// Converts a character to its uppercase equivalent.
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.
250 /// A full reference can be found here [2].
254 /// Returns the uppercase equivalent of the character, or the character
255 /// itself if no conversion was made.
257 /// [1]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
259 /// [`SpecialCasing`.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
261 /// [2]: http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
262 fn to_uppercase(self) -> char;
264 /// Returns this character's displayed width in columns, or `None` if it is a
265 /// control character other than `'\x00'`.
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>;
277 #[experimental = "pending prelude organization"]
278 impl UnicodeChar for char {
279 fn is_alphabetic(self) -> bool {
281 'a' ... 'z' | 'A' ... 'Z' => true,
282 c if c > '\x7f' => derived_property::Alphabetic(c),
287 #[deprecated = "use is_xid_start"]
288 fn is_XID_start(self) -> bool { derived_property::XID_Start(self) }
290 #[deprecated = "use is_xid_continue"]
291 fn is_XID_continue(self) -> bool { derived_property::XID_Continue(self) }
293 fn is_xid_start(self) -> bool { derived_property::XID_Start(self) }
295 fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) }
297 fn is_lowercase(self) -> bool {
300 c if c > '\x7f' => derived_property::Lowercase(c),
305 fn is_uppercase(self) -> bool {
308 c if c > '\x7f' => derived_property::Uppercase(c),
313 fn is_whitespace(self) -> bool {
315 ' ' | '\x09' ... '\x0d' => true,
316 c if c > '\x7f' => property::White_Space(c),
321 fn is_alphanumeric(self) -> bool {
322 self.is_alphabetic() || self.is_numeric()
325 fn is_control(self) -> bool { general_category::Cc(self) }
327 fn is_numeric(self) -> bool {
330 c if c > '\x7f' => general_category::N(c),
335 fn to_lowercase(self) -> char { conversions::to_lower(self) }
337 fn to_uppercase(self) -> char { conversions::to_upper(self) }
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) }