]> git.lizzy.rs Git - rust.git/blob - src/libunicode/u_char.rs
Merge pull request #20510 from tshepang/patch-6
[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 /// 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;
25
26     /// Returns whether the specified character satisfies the 'XID_Start'
27     /// Unicode property.
28     ///
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;
33
34     /// Returns whether the specified `char` satisfies the 'XID_Continue'
35     /// 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     fn is_xid_continue(self) -> bool;
41
42     /// Indicates whether a character is in lowercase.
43     ///
44     /// This is defined according to the terms of the Unicode Derived Core
45     /// Property `Lowercase`.
46     fn is_lowercase(self) -> bool;
47
48     /// Indicates whether a character is in uppercase.
49     ///
50     /// This is defined according to the terms of the Unicode Derived Core
51     /// Property `Uppercase`.
52     fn is_uppercase(self) -> bool;
53
54     /// Indicates whether a character is whitespace.
55     ///
56     /// Whitespace is defined in terms of the Unicode Property `White_Space`.
57     fn is_whitespace(self) -> bool;
58
59     /// Indicates whether a character is alphanumeric.
60     ///
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;
64
65     /// Indicates whether a character is a control code point.
66     ///
67     /// Control code points are defined in terms of the Unicode General
68     /// Category `Cc`.
69     fn is_control(self) -> bool;
70
71     /// Indicates whether the character is numeric (Nd, Nl, or No).
72     fn is_numeric(self) -> bool;
73
74     /// Converts a character to its lowercase equivalent.
75     ///
76     /// The case-folding performed is the common or simple mapping. See
77     /// `to_uppercase()` for references and more information.
78     ///
79     /// # Return value
80     ///
81     /// Returns the lowercase equivalent of the character, or the character
82     /// itself if no conversion is possible.
83     fn to_lowercase(self) -> char;
84
85     /// Converts a character to its uppercase equivalent.
86     ///
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.
92     ///
93     /// A full reference can be found here [2].
94     ///
95     /// # Return value
96     ///
97     /// Returns the uppercase equivalent of the character, or the character
98     /// itself if no conversion was made.
99     ///
100     /// [1]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
101     ///
102     /// [`SpecialCasing`.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
103     ///
104     /// [2]: http://www.unicode.org/versions/Unicode4.0.0/ch03.pdf#G33992
105     fn to_uppercase(self) -> char;
106
107     /// Returns this character's displayed width in columns, or `None` if it is a
108     /// control character other than `'\x00'`.
109     ///
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>;
118 }
119
120 #[experimental = "pending prelude organization"]
121 impl UnicodeChar for char {
122     fn is_alphabetic(self) -> bool {
123         match self {
124             'a' ... 'z' | 'A' ... 'Z' => true,
125             c if c > '\x7f' => derived_property::Alphabetic(c),
126             _ => false
127         }
128     }
129
130     fn is_xid_start(self) -> bool { derived_property::XID_Start(self) }
131
132     fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) }
133
134     fn is_lowercase(self) -> bool {
135         match self {
136             'a' ... 'z' => true,
137             c if c > '\x7f' => derived_property::Lowercase(c),
138             _ => false
139         }
140     }
141
142     fn is_uppercase(self) -> bool {
143         match self {
144             'A' ... 'Z' => true,
145             c if c > '\x7f' => derived_property::Uppercase(c),
146             _ => false
147         }
148     }
149
150     fn is_whitespace(self) -> bool {
151         match self {
152             ' ' | '\x09' ... '\x0d' => true,
153             c if c > '\x7f' => property::White_Space(c),
154             _ => false
155         }
156     }
157
158     fn is_alphanumeric(self) -> bool {
159         self.is_alphabetic() || self.is_numeric()
160     }
161
162     fn is_control(self) -> bool { general_category::Cc(self) }
163
164     fn is_numeric(self) -> bool {
165         match self {
166             '0' ... '9' => true,
167             c if c > '\x7f' => general_category::N(c),
168             _ => false
169         }
170     }
171
172     fn to_lowercase(self) -> char { conversions::to_lower(self) }
173
174     fn to_uppercase(self) -> char { conversions::to_upper(self) }
175
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) }
178 }