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.
11 //! Unicode-intensive `char` methods.
13 //! These methods implement functionality for `char` that requires knowledge of
14 //! Unicode definitions, including normalization, categorization, and display information.
16 use core::option::Option;
17 use tables::{derived_property, property, general_category, conversions, charwidth};
19 /// Useful functions for Unicode characters.
20 #[experimental = "pending prelude organization"]
21 pub trait UnicodeChar {
22 /// Returns whether the specified character is considered a Unicode
23 /// alphabetic code point.
24 fn is_alphabetic(self) -> bool;
26 /// Returns whether the specified character satisfies the 'XID_Start'
29 /// 'XID_Start' is a Unicode Derived Property specified in
30 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
31 /// mostly similar to ID_Start but modified for closure under NFKx.
32 fn is_xid_start(self) -> bool;
34 /// Returns whether the specified `char` satisfies the 'XID_Continue'
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 fn is_xid_continue(self) -> bool;
42 /// Indicates whether a character is in lowercase.
44 /// This is defined according to the terms of the Unicode Derived Core
45 /// Property `Lowercase`.
46 fn is_lowercase(self) -> bool;
48 /// Indicates whether a character is in uppercase.
50 /// This is defined according to the terms of the Unicode Derived Core
51 /// Property `Uppercase`.
52 fn is_uppercase(self) -> bool;
54 /// Indicates whether a character is whitespace.
56 /// Whitespace is defined in terms of the Unicode Property `White_Space`.
57 fn is_whitespace(self) -> bool;
59 /// Indicates whether a character is alphanumeric.
61 /// Alphanumericness is defined in terms of the Unicode General Categories
62 /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
63 fn is_alphanumeric(self) -> bool;
65 /// Indicates whether a character is a control code point.
67 /// Control code points are defined in terms of the Unicode General
69 fn is_control(self) -> bool;
71 /// Indicates whether the character is numeric (Nd, Nl, or No).
72 fn is_numeric(self) -> bool;
74 /// Converts a character to its lowercase equivalent.
76 /// The case-folding performed is the common or simple mapping. See
77 /// `to_uppercase()` for references and more information.
81 /// Returns the lowercase equivalent of the character, or the character
82 /// itself if no conversion is possible.
83 fn to_lowercase(self) -> char;
85 /// Converts a character to its uppercase equivalent.
87 /// The case-folding performed is the common or simple mapping: it maps
88 /// one Unicode codepoint (one character in Rust) to its uppercase
89 /// equivalent according to the Unicode database [1]. The additional
90 /// [`SpecialCasing.txt`] is not considered here, as it expands to multiple
91 /// codepoints in some cases.
93 /// A full reference can be found here [2].
97 /// Returns the uppercase equivalent of the character, or the character
98 /// itself if no conversion was made.
100 /// [1]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
102 /// [`SpecialCasing`.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
104 /// [2]: http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
105 fn to_uppercase(self) -> char;
107 /// Returns this character's displayed width in columns, or `None` if it is a
108 /// control character other than `'\x00'`.
110 /// `is_cjk` determines behavior for characters in the Ambiguous category:
111 /// if `is_cjk` is `true`, these are 2 columns wide; otherwise, they are 1.
112 /// In CJK contexts, `is_cjk` should be `true`, else it should be `false`.
113 /// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
114 /// recommends that these characters be treated as 1 column (i.e.,
115 /// `is_cjk` = `false`) if the context cannot be reliably determined.
116 #[experimental = "needs expert opinion. is_cjk flag stands out as ugly"]
117 fn width(self, is_cjk: bool) -> Option<uint>;
120 #[experimental = "pending prelude organization"]
121 impl UnicodeChar for char {
122 fn is_alphabetic(self) -> bool {
124 'a' ... 'z' | 'A' ... 'Z' => true,
125 c if c > '\x7f' => derived_property::Alphabetic(c),
130 fn is_xid_start(self) -> bool { derived_property::XID_Start(self) }
132 fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) }
134 fn is_lowercase(self) -> bool {
137 c if c > '\x7f' => derived_property::Lowercase(c),
142 fn is_uppercase(self) -> bool {
145 c if c > '\x7f' => derived_property::Uppercase(c),
150 fn is_whitespace(self) -> bool {
152 ' ' | '\x09' ... '\x0d' => true,
153 c if c > '\x7f' => property::White_Space(c),
158 fn is_alphanumeric(self) -> bool {
159 self.is_alphabetic() || self.is_numeric()
162 fn is_control(self) -> bool { general_category::Cc(self) }
164 fn is_numeric(self) -> bool {
167 c if c > '\x7f' => general_category::N(c),
172 fn to_lowercase(self) -> char { conversions::to_lower(self) }
174 fn to_uppercase(self) -> char { conversions::to_upper(self) }
176 #[experimental = "needs expert opinion. is_cjk flag stands out as ugly"]
177 fn width(self, is_cjk: bool) -> Option<uint> { charwidth::width(self, is_cjk) }