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 //! Character manipulation (`char` type, Unicode Scalar Value)
13 //! This module provides the `CharExt` trait, as well as its
14 //! implementation for the primitive `char` type, in order to allow
15 //! basic character manipulation.
17 //! A `char` actually represents a
19 //! Value](http://www.unicode.org/glossary/#unicode_scalar_value)*, as it can
20 //! contain any Unicode code point except high-surrogate and low-surrogate code
23 //! As such, only values in the ranges \[0x0,0xD7FF\] and \[0xE000,0x10FFFF\]
24 //! (inclusive) are allowed. A `char` can always be safely cast to a `u32`;
25 //! however the converse is not always true due to the above range limits
26 //! and, as such, should be performed via the `from_u32` function.
28 #![stable(feature = "rust1", since = "1.0.0")]
29 #![doc(primitive = "char")]
31 use core
::char::CharExt
as C
;
32 use core
::option
::Option
::{self, Some, None}
;
33 use core
::iter
::Iterator
;
34 use tables
::{derived_property, property, general_category, conversions, charwidth}
;
37 pub use core
::char::{MAX, from_u32, from_digit, EscapeUnicode, EscapeDefault}
;
41 pub use normalize
::{decompose_canonical, decompose_compatible, compose}
;
43 pub use tables
::normalization
::canonical_combining_class
;
44 pub use tables
::UNICODE_VERSION
;
46 /// An iterator over the lowercase mapping of a given character, returned from
47 /// the [`to_lowercase` method](../primitive.char.html#method.to_lowercase) on
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub struct ToLowercase(CaseMappingIter
);
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl Iterator
for ToLowercase
{
55 fn next(&mut self) -> Option
<char> { self.0.next() }
58 /// An iterator over the uppercase mapping of a given character, returned from
59 /// the [`to_uppercase` method](../primitive.char.html#method.to_uppercase) on
61 #[stable(feature = "rust1", since = "1.0.0")]
62 pub struct ToUppercase(CaseMappingIter
);
64 #[stable(feature = "rust1", since = "1.0.0")]
65 impl Iterator
for ToUppercase
{
67 fn next(&mut self) -> Option
<char> { self.0.next() }
70 /// An iterator over the titlecase mapping of a given character, returned from
71 /// the [`to_titlecase` method](../primitive.char.html#method.to_titlecase) on
73 #[unstable(feature = "unicode", reason = "recently added")]
74 pub struct ToTitlecase(CaseMappingIter
);
76 #[stable(feature = "unicode_case_mapping", since = "1.2.0")]
77 impl Iterator
for ToTitlecase
{
79 fn next(&mut self) -> Option
<char> { self.0.next() }
83 enum CaseMappingIter
{
84 Three(char, char, char),
90 impl CaseMappingIter
{
91 fn new(chars
: [char; 3]) -> CaseMappingIter
{
94 CaseMappingIter
::One(chars
[0]) // Including if chars[0] == '\0'
96 CaseMappingIter
::Two(chars
[0], chars
[1])
99 CaseMappingIter
::Three(chars
[0], chars
[1], chars
[2])
104 impl Iterator
for CaseMappingIter
{
106 fn next(&mut self) -> Option
<char> {
108 CaseMappingIter
::Three(a
, b
, c
) => {
109 *self = CaseMappingIter
::Two(b
, c
);
112 CaseMappingIter
::Two(b
, c
) => {
113 *self = CaseMappingIter
::One(c
);
116 CaseMappingIter
::One(c
) => {
117 *self = CaseMappingIter
::Zero
;
120 CaseMappingIter
::Zero
=> None
,
125 #[stable(feature = "rust1", since = "1.0.0")]
128 /// Checks if a `char` parses as a numeric digit in the given radix.
130 /// Compared to `is_numeric()`, this function only recognizes the characters
131 /// `0-9`, `a-z` and `A-Z`.
135 /// Returns `true` if `c` is a valid digit under `radix`, and `false`
140 /// Panics if given a radix > 36.
147 /// assert!(c.is_digit(10));
149 /// assert!('f'.is_digit(16));
151 #[stable(feature = "rust1", since = "1.0.0")]
153 pub fn is_digit(self, radix
: u32) -> bool { C::is_digit(self, radix) }
155 /// Converts a character to the corresponding digit.
159 /// If `c` is between '0' and '9', the corresponding value between 0 and
160 /// 9. If `c` is 'a' or 'A', 10. If `c` is 'b' or 'B', 11, etc. Returns
161 /// none if the character does not refer to a digit in the given radix.
165 /// Panics if given a radix outside the range [0..36].
172 /// assert_eq!(c.to_digit(10), Some(1));
174 /// assert_eq!('f'.to_digit(16), Some(15));
176 #[stable(feature = "rust1", since = "1.0.0")]
178 pub fn to_digit(self, radix
: u32) -> Option
<u32> { C::to_digit(self, radix) }
180 /// Returns an iterator that yields the hexadecimal Unicode escape of a
181 /// character, as `char`s.
183 /// All characters are escaped with Rust syntax of the form `\\u{NNNN}`
184 /// where `NNNN` is the shortest hexadecimal representation of the code
190 /// for c in '❤'.escape_unicode() {
202 /// Collecting into a `String`:
205 /// let heart: String = '❤'.escape_unicode().collect();
207 /// assert_eq!(heart, r"\u{2764}");
209 #[stable(feature = "rust1", since = "1.0.0")]
211 pub fn escape_unicode(self) -> EscapeUnicode { C::escape_unicode(self) }
213 /// Returns an iterator that yields the 'default' ASCII and
214 /// C++11-like literal escape of a character, as `char`s.
216 /// The default is chosen with a bias toward producing literals that are
217 /// legal in a variety of languages, including C++11 and similar C-family
218 /// languages. The exact rules are:
220 /// * Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
221 /// * Single-quote, double-quote and backslash chars are backslash-
223 /// * Any other chars in the range [0x20,0x7e] are not escaped.
224 /// * Any other chars are given hex Unicode escapes; see `escape_unicode`.
229 /// for i in '"'.escape_default() {
230 /// println!("{}", i);
241 /// Collecting into a `String`:
244 /// let quote: String = '"'.escape_default().collect();
246 /// assert_eq!(quote, "\\\"");
248 #[stable(feature = "rust1", since = "1.0.0")]
250 pub fn escape_default(self) -> EscapeDefault { C::escape_default(self) }
252 /// Returns the number of bytes this character would need if encoded in
258 /// let n = 'ß'.len_utf8();
260 /// assert_eq!(n, 2);
262 #[stable(feature = "rust1", since = "1.0.0")]
264 pub fn len_utf8(self) -> usize { C::len_utf8(self) }
266 /// Returns the number of 16-bit code units this character would need if
267 /// encoded in UTF-16.
272 /// let n = 'ß'.len_utf16();
274 /// assert_eq!(n, 1);
276 #[stable(feature = "rust1", since = "1.0.0")]
278 pub fn len_utf16(self) -> usize { C::len_utf16(self) }
280 /// Encodes this character as UTF-8 into the provided byte buffer, and then
281 /// returns the number of bytes written.
283 /// If the buffer is not large enough, nothing will be written into it and a
284 /// `None` will be returned. A buffer of length four is large enough to
285 /// encode any `char`.
289 /// In both of these examples, 'ß' takes two bytes to encode.
292 /// # #![feature(unicode)]
293 /// let mut b = [0; 2];
295 /// let result = 'ß'.encode_utf8(&mut b);
297 /// assert_eq!(result, Some(2));
300 /// A buffer that's too small:
303 /// # #![feature(unicode)]
304 /// let mut b = [0; 1];
306 /// let result = 'ß'.encode_utf8(&mut b);
308 /// assert_eq!(result, None);
310 #[unstable(feature = "unicode",
311 reason
= "pending decision about Iterator/Writer/Reader")]
313 pub fn encode_utf8(self, dst
: &mut [u8]) -> Option
<usize> {
314 C
::encode_utf8(self, dst
)
317 /// Encodes this character as UTF-16 into the provided `u16` buffer, and
318 /// then returns the number of `u16`s written.
320 /// If the buffer is not large enough, nothing will be written into it and a
321 /// `None` will be returned. A buffer of length 2 is large enough to encode
326 /// In both of these examples, 'ß' takes one `u16` to encode.
329 /// # #![feature(unicode)]
330 /// let mut b = [0; 1];
332 /// let result = 'ß'.encode_utf16(&mut b);
334 /// assert_eq!(result, Some(1));
337 /// A buffer that's too small:
340 /// # #![feature(unicode)]
341 /// let mut b = [0; 0];
343 /// let result = 'ß'.encode_utf8(&mut b);
345 /// assert_eq!(result, None);
347 #[unstable(feature = "unicode",
348 reason
= "pending decision about Iterator/Writer/Reader")]
350 pub fn encode_utf16(self, dst
: &mut [u16]) -> Option
<usize> {
351 C
::encode_utf16(self, dst
)
354 /// Returns whether the specified character is considered a Unicode
355 /// alphabetic code point.
356 #[stable(feature = "rust1", since = "1.0.0")]
358 pub fn is_alphabetic(self) -> bool
{
360 'a'
... 'z'
| 'A'
... 'Z'
=> true,
361 c
if c
> '
\x7f'
=> derived_property
::Alphabetic(c
),
366 /// Returns whether the specified character satisfies the 'XID_Start'
367 /// Unicode property.
369 /// 'XID_Start' is a Unicode Derived Property specified in
370 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
371 /// mostly similar to ID_Start but modified for closure under NFKx.
372 #[unstable(feature = "unicode",
373 reason
= "mainly needed for compiler internals")]
375 pub fn is_xid_start(self) -> bool { derived_property::XID_Start(self) }
377 /// Returns whether the specified `char` satisfies the 'XID_Continue'
378 /// Unicode property.
380 /// 'XID_Continue' is a Unicode Derived Property specified in
381 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
382 /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
383 #[unstable(feature = "unicode",
384 reason
= "mainly needed for compiler internals")]
386 pub fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) }
388 /// Indicates whether a character is in lowercase.
390 /// This is defined according to the terms of the Unicode Derived Core
391 /// Property `Lowercase`.
392 #[stable(feature = "rust1", since = "1.0.0")]
394 pub fn is_lowercase(self) -> bool
{
397 c
if c
> '
\x7f'
=> derived_property
::Lowercase(c
),
402 /// Indicates whether a character is in uppercase.
404 /// This is defined according to the terms of the Unicode Derived Core
405 /// Property `Uppercase`.
406 #[stable(feature = "rust1", since = "1.0.0")]
408 pub fn is_uppercase(self) -> bool
{
411 c
if c
> '
\x7f'
=> derived_property
::Uppercase(c
),
416 /// Indicates whether a character is whitespace.
418 /// Whitespace is defined in terms of the Unicode Property `White_Space`.
419 #[stable(feature = "rust1", since = "1.0.0")]
421 pub fn is_whitespace(self) -> bool
{
423 ' '
| '
\x09'
... '
\x0d'
=> true,
424 c
if c
> '
\x7f'
=> property
::White_Space(c
),
429 /// Indicates whether a character is alphanumeric.
431 /// Alphanumericness is defined in terms of the Unicode General Categories
432 /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
433 #[stable(feature = "rust1", since = "1.0.0")]
435 pub fn is_alphanumeric(self) -> bool
{
436 self.is_alphabetic() || self.is_numeric()
439 /// Indicates whether a character is a control code point.
441 /// Control code points are defined in terms of the Unicode General
443 #[stable(feature = "rust1", since = "1.0.0")]
445 pub fn is_control(self) -> bool { general_category::Cc(self) }
447 /// Indicates whether the character is numeric (Nd, Nl, or No).
448 #[stable(feature = "rust1", since = "1.0.0")]
450 pub fn is_numeric(self) -> bool
{
453 c
if c
> '
\x7f'
=> general_category
::N(c
),
458 /// Converts a character to its lowercase equivalent.
460 /// This performs complex unconditional mappings with no tailoring.
461 /// See `to_uppercase()` for references and more information.
465 /// Returns an iterator which yields the characters corresponding to the
466 /// lowercase equivalent of the character. If no conversion is possible then
467 /// an iterator with just the input character is returned.
472 /// assert_eq!(Some('c'), 'C'.to_lowercase().next());
474 #[stable(feature = "rust1", since = "1.0.0")]
476 pub fn to_lowercase(self) -> ToLowercase
{
477 ToLowercase(CaseMappingIter
::new(conversions
::to_lower(self)))
480 /// Converts a character to its titlecase equivalent.
482 /// This performs complex unconditional mappings with no tailoring.
483 /// See `to_uppercase()` for references and more information.
485 /// This differs from `to_uppercase()` since Unicode contains
486 /// digraphs and ligature characters.
487 /// For example, U+01F3 “dz” and U+FB01 “fi”
488 /// map to U+01F1 “DZ” and U+0046 U+0069 “Fi”, respectively.
492 /// Returns an iterator which yields the characters corresponding to the
493 /// titlecase equivalent of the character. If no conversion is possible then
494 /// an iterator with just the input character is returned.
495 #[unstable(feature = "unicode", reason = "recently added")]
497 pub fn to_titlecase(self) -> ToTitlecase
{
498 ToTitlecase(CaseMappingIter
::new(conversions
::to_title(self)))
501 /// Converts a character to its uppercase equivalent.
503 /// This performs complex unconditional mappings with no tailoring:
504 /// it maps one Unicode character to its uppercase equivalent
505 /// according to the Unicode database [1]
506 /// and the additional complex mappings [`SpecialCasing.txt`].
507 /// Conditional mappings (based on context or language) are not considerd here.
509 /// A full reference can be found here [2].
513 /// Returns an iterator which yields the characters corresponding to the
514 /// uppercase equivalent of the character. If no conversion is possible then
515 /// an iterator with just the input character is returned.
517 /// [1]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
519 /// [`SpecialCasing.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
521 /// [2]: http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
526 /// assert_eq!(Some('C'), 'c'.to_uppercase().next());
528 #[stable(feature = "rust1", since = "1.0.0")]
530 pub fn to_uppercase(self) -> ToUppercase
{
531 ToUppercase(CaseMappingIter
::new(conversions
::to_upper(self)))
534 /// Returns this character's displayed width in columns, or `None` if it is a
535 /// control character other than `'\x00'`.
537 /// `is_cjk` determines behavior for characters in the Ambiguous category:
538 /// if `is_cjk` is `true`, these are 2 columns wide; otherwise, they are 1.
539 /// In CJK contexts, `is_cjk` should be `true`, else it should be `false`.
540 /// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
541 /// recommends that these characters be treated as 1 column (i.e.,
542 /// `is_cjk` = `false`) if the context cannot be reliably determined.
543 #[deprecated(reason = "use the crates.io `unicode-width` library instead",
545 #[unstable(feature = "unicode",
546 reason
= "needs expert opinion. is_cjk flag stands out as ugly")]
548 pub fn width(self, is_cjk
: bool
) -> Option
<usize> {
549 charwidth
::width(self, is_cjk
)