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.
13 //! The `char` type represents a single character. More specifically, since
14 //! 'character' isn't a well-defined concept in Unicode, `char` is a '[Unicode
15 //! scalar value]', which is similar to, but not the same as, a '[Unicode code
18 //! [Unicode scalar value]: http://www.unicode.org/glossary/#unicode_scalar_value
19 //! [Unicode code point]: http://www.unicode.org/glossary/#code_point
21 //! This module exists for technical reasons, the primary documentation for
22 //! `char` is directly on [the `char` primitive type](../primitive.char.html)
25 //! This module is the home of the iterator implementations for the iterators
26 //! implemented on `char`, as well as some useful constants and conversion
27 //! functions that convert various types to `char`.
29 #![stable(feature = "rust1", since = "1.0.0")]
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};
37 #[stable(feature = "rust1", since = "1.0.0")]
38 pub use core::char::{MAX, from_u32, from_u32_unchecked, from_digit, EscapeUnicode, EscapeDefault};
41 #[unstable(feature = "unicode", issue = "27783")]
42 pub use tables::UNICODE_VERSION;
44 /// Returns an iterator that yields the lowercase equivalent of a `char`.
46 /// This `struct` is created by the [`to_lowercase()`] method on [`char`]. See
47 /// its documentation for more.
49 /// [`to_lowercase()`]: ../primitive.char.html#method.to_lowercase
50 /// [`char`]: ../primitive.char.html
51 #[stable(feature = "rust1", since = "1.0.0")]
52 pub struct ToLowercase(CaseMappingIter);
54 #[stable(feature = "rust1", since = "1.0.0")]
55 impl Iterator for ToLowercase {
57 fn next(&mut self) -> Option<char> {
62 /// Returns an iterator that yields the uppercase equivalent of a `char`.
64 /// This `struct` is created by the [`to_uppercase()`] method on [`char`]. See
65 /// its documentation for more.
67 /// [`to_uppercase()`]: ../primitive.char.html#method.to_uppercase
68 /// [`char`]: ../primitive.char.html
69 #[stable(feature = "rust1", since = "1.0.0")]
70 pub struct ToUppercase(CaseMappingIter);
72 #[stable(feature = "rust1", since = "1.0.0")]
73 impl Iterator for ToUppercase {
75 fn next(&mut self) -> Option<char> {
81 enum CaseMappingIter {
82 Three(char, char, char),
88 impl CaseMappingIter {
89 fn new(chars: [char; 3]) -> CaseMappingIter {
92 CaseMappingIter::One(chars[0]) // Including if chars[0] == '\0'
94 CaseMappingIter::Two(chars[0], chars[1])
97 CaseMappingIter::Three(chars[0], chars[1], chars[2])
102 impl Iterator for CaseMappingIter {
104 fn next(&mut self) -> Option<char> {
106 CaseMappingIter::Three(a, b, c) => {
107 *self = CaseMappingIter::Two(b, c);
110 CaseMappingIter::Two(b, c) => {
111 *self = CaseMappingIter::One(c);
114 CaseMappingIter::One(c) => {
115 *self = CaseMappingIter::Zero;
118 CaseMappingIter::Zero => None,
125 /// Checks if a `char` is a digit in the given radix.
127 /// A 'radix' here is sometimes also called a 'base'. A radix of two
128 /// indicates a binary number, a radix of ten, decimal, and a radix of
129 /// sixteen, hexadecimal, to give some common values. Arbitrary
130 /// radicum are supported.
132 /// Compared to `is_numeric()`, this function only recognizes the characters
133 /// `0-9`, `a-z` and `A-Z`.
135 /// 'Digit' is defined to be only the following characters:
141 /// For a more comprehensive understanding of 'digit', see [`is_numeric()`][is_numeric].
143 /// [is_numeric]: #method.is_numeric
147 /// Panics if given a radix larger than 36.
156 /// assert!(d.is_digit(10));
160 /// assert!(d.is_digit(16));
161 /// assert!(!d.is_digit(10));
164 /// Passing a large radix, causing a panic:
169 /// let result = thread::spawn(|| {
176 /// assert!(result.is_err());
178 #[stable(feature = "rust1", since = "1.0.0")]
180 pub fn is_digit(self, radix: u32) -> bool {
181 C::is_digit(self, radix)
184 /// Converts a `char` to a digit in the given radix.
186 /// A 'radix' here is sometimes also called a 'base'. A radix of two
187 /// indicates a binary number, a radix of ten, decimal, and a radix of
188 /// sixteen, hexadecimal, to give some common values. Arbitrary
189 /// radicum are supported.
191 /// 'Digit' is defined to be only the following characters:
199 /// Returns `None` if the `char` does not refer to a digit in the given radix.
203 /// Panics if given a radix larger than 36.
212 /// assert_eq!(d.to_digit(10), Some(1));
216 /// assert_eq!(d.to_digit(16), Some(15));
219 /// Passing a non-digit results in failure:
224 /// assert_eq!(d.to_digit(10), None);
228 /// assert_eq!(d.to_digit(16), None);
231 /// Passing a large radix, causing a panic:
236 /// let result = thread::spawn(|| {
242 /// assert!(result.is_err());
244 #[stable(feature = "rust1", since = "1.0.0")]
246 pub fn to_digit(self, radix: u32) -> Option<u32> {
247 C::to_digit(self, radix)
250 /// Returns an iterator that yields the hexadecimal Unicode escape of a
251 /// character, as `char`s.
253 /// All characters are escaped with Rust syntax of the form `\\u{NNNN}`
254 /// where `NNNN` is the shortest hexadecimal representation.
261 /// for c in '❤'.escape_unicode() {
273 /// Collecting into a `String`:
276 /// let heart: String = '❤'.escape_unicode().collect();
278 /// assert_eq!(heart, r"\u{2764}");
280 #[stable(feature = "rust1", since = "1.0.0")]
282 pub fn escape_unicode(self) -> EscapeUnicode {
283 C::escape_unicode(self)
286 /// Returns an iterator that yields the literal escape code of a `char`.
288 /// The default is chosen with a bias toward producing literals that are
289 /// legal in a variety of languages, including C++11 and similar C-family
290 /// languages. The exact rules are:
292 /// * Tab is escaped as `\t`.
293 /// * Carriage return is escaped as `\r`.
294 /// * Line feed is escaped as `\n`.
295 /// * Single quote is escaped as `\'`.
296 /// * Double quote is escaped as `\"`.
297 /// * Backslash is escaped as `\\`.
298 /// * Any character in the 'printable ASCII' range `0x20` .. `0x7e`
299 /// inclusive is not escaped.
300 /// * All other characters are given hexadecimal Unicode escapes; see
301 /// [`escape_unicode`][escape_unicode].
303 /// [escape_unicode]: #method.escape_unicode
310 /// for i in '"'.escape_default() {
311 /// println!("{}", i);
322 /// Collecting into a `String`:
325 /// let quote: String = '"'.escape_default().collect();
327 /// assert_eq!(quote, "\\\"");
329 #[stable(feature = "rust1", since = "1.0.0")]
331 pub fn escape_default(self) -> EscapeDefault {
332 C::escape_default(self)
335 /// Returns the number of bytes this `char` would need if encoded in UTF-8.
337 /// That number of bytes is always between 1 and 4, inclusive.
344 /// let len = 'A'.len_utf8();
345 /// assert_eq!(len, 1);
347 /// let len = 'ß'.len_utf8();
348 /// assert_eq!(len, 2);
350 /// let len = 'ℝ'.len_utf8();
351 /// assert_eq!(len, 3);
353 /// let len = '💣'.len_utf8();
354 /// assert_eq!(len, 4);
357 /// The `&str` type guarantees that its contents are UTF-8, and so we can compare the length it
358 /// would take if each code point was represented as a `char` vs in the `&str` itself:
362 /// let eastern = '東';
363 /// let capitol = '京';
365 /// // both can be represented as three bytes
366 /// assert_eq!(3, eastern.len_utf8());
367 /// assert_eq!(3, capitol.len_utf8());
369 /// // as a &str, these two are encoded in UTF-8
370 /// let tokyo = "東京";
372 /// let len = eastern.len_utf8() + capitol.len_utf8();
374 /// // we can see that they take six bytes total...
375 /// assert_eq!(6, tokyo.len());
377 /// // ... just like the &str
378 /// assert_eq!(len, tokyo.len());
380 #[stable(feature = "rust1", since = "1.0.0")]
382 pub fn len_utf8(self) -> usize {
386 /// Returns the number of 16-bit code units this `char` would need if
387 /// encoded in UTF-16.
389 /// See the documentation for [`len_utf8()`] for more explanation of this
390 /// concept. This function is a mirror, but for UTF-16 instead of UTF-8.
392 /// [`len_utf8()`]: #method.len_utf8
399 /// let n = 'ß'.len_utf16();
400 /// assert_eq!(n, 1);
402 /// let len = '💣'.len_utf16();
403 /// assert_eq!(len, 2);
405 #[stable(feature = "rust1", since = "1.0.0")]
407 pub fn len_utf16(self) -> usize {
411 /// Encodes this character as UTF-8 into the provided byte buffer, and then
412 /// returns the number of bytes written.
414 /// If the buffer is not large enough, nothing will be written into it and a
415 /// `None` will be returned. A buffer of length four is large enough to
416 /// encode any `char`.
420 /// In both of these examples, 'ß' takes two bytes to encode.
423 /// #![feature(unicode)]
425 /// let mut b = [0; 2];
427 /// let result = 'ß'.encode_utf8(&mut b);
429 /// assert_eq!(result, Some(2));
432 /// A buffer that's too small:
435 /// #![feature(unicode)]
437 /// let mut b = [0; 1];
439 /// let result = 'ß'.encode_utf8(&mut b);
441 /// assert_eq!(result, None);
443 #[unstable(feature = "unicode",
444 reason = "pending decision about Iterator/Writer/Reader",
447 pub fn encode_utf8(self, dst: &mut [u8]) -> Option<usize> {
448 C::encode_utf8(self, dst)
451 /// Encodes this character as UTF-16 into the provided `u16` buffer, and
452 /// then returns the number of `u16`s written.
454 /// If the buffer is not large enough, nothing will be written into it and a
455 /// `None` will be returned. A buffer of length 2 is large enough to encode
460 /// In both of these examples, 'ß' takes one `u16` to encode.
463 /// #![feature(unicode)]
465 /// let mut b = [0; 1];
467 /// let result = 'ß'.encode_utf16(&mut b);
469 /// assert_eq!(result, Some(1));
472 /// A buffer that's too small:
475 /// #![feature(unicode)]
477 /// let mut b = [0; 0];
479 /// let result = 'ß'.encode_utf8(&mut b);
481 /// assert_eq!(result, None);
483 #[unstable(feature = "unicode",
484 reason = "pending decision about Iterator/Writer/Reader",
487 pub fn encode_utf16(self, dst: &mut [u16]) -> Option<usize> {
488 C::encode_utf16(self, dst)
491 /// Returns true if this `char` is an alphabetic code point, and false if not.
500 /// assert!(c.is_alphabetic());
503 /// assert!(c.is_alphabetic());
506 /// // love is many things, but it is not alphabetic
507 /// assert!(!c.is_alphabetic());
509 #[stable(feature = "rust1", since = "1.0.0")]
511 pub fn is_alphabetic(self) -> bool {
513 'a'...'z' | 'A'...'Z' => true,
514 c if c > '\x7f' => derived_property::Alphabetic(c),
519 /// Returns true if this `char` satisfies the 'XID_Start' Unicode property, and false
522 /// 'XID_Start' is a Unicode Derived Property specified in
523 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
524 /// mostly similar to `ID_Start` but modified for closure under `NFKx`.
525 #[unstable(feature = "unicode",
526 reason = "mainly needed for compiler internals",
529 pub fn is_xid_start(self) -> bool {
530 derived_property::XID_Start(self)
533 /// Returns true if this `char` satisfies the 'XID_Continue' Unicode property, and false
536 /// 'XID_Continue' is a Unicode Derived Property specified in
537 /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
538 /// mostly similar to 'ID_Continue' but modified for closure under NFKx.
539 #[unstable(feature = "unicode",
540 reason = "mainly needed for compiler internals",
543 pub fn is_xid_continue(self) -> bool {
544 derived_property::XID_Continue(self)
547 /// Returns true if this `char` is lowercase, and false otherwise.
549 /// 'Lowercase' is defined according to the terms of the Unicode Derived Core
550 /// Property `Lowercase`.
558 /// assert!(c.is_lowercase());
561 /// assert!(c.is_lowercase());
564 /// assert!(!c.is_lowercase());
567 /// assert!(!c.is_lowercase());
569 /// // The various Chinese scripts do not have case, and so:
571 /// assert!(!c.is_lowercase());
573 #[stable(feature = "rust1", since = "1.0.0")]
575 pub fn is_lowercase(self) -> bool {
578 c if c > '\x7f' => derived_property::Lowercase(c),
583 /// Returns true if this `char` is uppercase, and false otherwise.
585 /// 'Uppercase' is defined according to the terms of the Unicode Derived Core
586 /// Property `Uppercase`.
594 /// assert!(!c.is_uppercase());
597 /// assert!(!c.is_uppercase());
600 /// assert!(c.is_uppercase());
603 /// assert!(c.is_uppercase());
605 /// // The various Chinese scripts do not have case, and so:
607 /// assert!(!c.is_uppercase());
609 #[stable(feature = "rust1", since = "1.0.0")]
611 pub fn is_uppercase(self) -> bool {
614 c if c > '\x7f' => derived_property::Uppercase(c),
619 /// Returns true if this `char` is whitespace, and false otherwise.
621 /// 'Whitespace' is defined according to the terms of the Unicode Derived Core
622 /// Property `White_Space`.
630 /// assert!(c.is_whitespace());
632 /// // a non-breaking space
633 /// let c = '\u{A0}';
634 /// assert!(c.is_whitespace());
637 /// assert!(!c.is_whitespace());
639 #[stable(feature = "rust1", since = "1.0.0")]
641 pub fn is_whitespace(self) -> bool {
643 ' ' | '\x09'...'\x0d' => true,
644 c if c > '\x7f' => property::White_Space(c),
649 /// Returns true if this `char` is alphanumeric, and false otherwise.
651 /// 'Alphanumeric'-ness is defined in terms of the Unicode General Categories
652 /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
660 /// assert!(c.is_alphanumeric());
663 /// assert!(c.is_alphanumeric());
666 /// assert!(c.is_alphanumeric());
669 /// assert!(c.is_alphanumeric());
672 /// assert!(c.is_alphanumeric());
675 /// assert!(c.is_alphanumeric());
678 /// assert!(!c.is_alphanumeric());
681 /// assert!(!c.is_alphanumeric());
683 #[stable(feature = "rust1", since = "1.0.0")]
685 pub fn is_alphanumeric(self) -> bool {
686 self.is_alphabetic() || self.is_numeric()
689 /// Returns true if this `char` is a control code point, and false otherwise.
691 /// 'Control code point' is defined in terms of the Unicode General
699 /// // U+009C, STRING TERMINATOR
701 /// assert!(c.is_control());
704 /// assert!(!c.is_control());
706 #[stable(feature = "rust1", since = "1.0.0")]
708 pub fn is_control(self) -> bool {
709 general_category::Cc(self)
712 /// Returns true if this `char` is numeric, and false otherwise.
714 /// 'Numeric'-ness is defined in terms of the Unicode General Categories
715 /// 'Nd', 'Nl', 'No'.
723 /// assert!(c.is_numeric());
726 /// assert!(c.is_numeric());
729 /// assert!(c.is_numeric());
732 /// assert!(!c.is_numeric());
735 /// assert!(!c.is_numeric());
738 /// assert!(!c.is_numeric());
741 /// assert!(!c.is_numeric());
744 /// assert!(!c.is_numeric());
746 #[stable(feature = "rust1", since = "1.0.0")]
748 pub fn is_numeric(self) -> bool {
751 c if c > '\x7f' => general_category::N(c),
756 /// Returns an iterator that yields the lowercase equivalent of a `char`.
758 /// If no conversion is possible then an iterator with just the input character is returned.
760 /// This performs complex unconditional mappings with no tailoring: it maps
761 /// one Unicode character to its lowercase equivalent according to the
762 /// [Unicode database] and the additional complex mappings
763 /// [`SpecialCasing.txt`]. Conditional mappings (based on context or
764 /// language) are not considered here.
766 /// For a full reference, see [here][reference].
768 /// [Unicode database]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
770 /// [`SpecialCasing.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
772 /// [reference]: http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
781 /// assert_eq!(c.to_uppercase().next(), Some('C'));
783 /// // Japanese scripts do not have case, and so:
785 /// assert_eq!(c.to_uppercase().next(), Some('山'));
787 #[stable(feature = "rust1", since = "1.0.0")]
789 pub fn to_lowercase(self) -> ToLowercase {
790 ToLowercase(CaseMappingIter::new(conversions::to_lower(self)))
793 /// Returns an iterator that yields the uppercase equivalent of a `char`.
795 /// If no conversion is possible then an iterator with just the input character is returned.
797 /// This performs complex unconditional mappings with no tailoring: it maps
798 /// one Unicode character to its uppercase equivalent according to the
799 /// [Unicode database] and the additional complex mappings
800 /// [`SpecialCasing.txt`]. Conditional mappings (based on context or
801 /// language) are not considered here.
803 /// For a full reference, see [here][reference].
805 /// [Unicode database]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
807 /// [`SpecialCasing.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
809 /// [reference]: http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
817 /// assert_eq!(c.to_uppercase().next(), Some('C'));
819 /// // Japanese does not have case, and so:
821 /// assert_eq!(c.to_uppercase().next(), Some('山'));
824 /// In Turkish, the equivalent of 'i' in Latin has five forms instead of two:
826 /// * 'Dotless': I / ı, sometimes written ï
827 /// * 'Dotted': İ / i
829 /// Note that the lowercase dotted 'i' is the same as the Latin. Therefore:
834 /// let upper_i = i.to_uppercase().next();
837 /// The value of `upper_i` here relies on the language of the text: if we're
838 /// in `en-US`, it should be `Some('I')`, but if we're in `tr_TR`, it should
839 /// be `Some('İ')`. `to_uppercase()` does not take this into account, and so:
844 /// let upper_i = i.to_uppercase().next();
846 /// assert_eq!(Some('I'), upper_i);
849 /// holds across languages.
850 #[stable(feature = "rust1", since = "1.0.0")]
852 pub fn to_uppercase(self) -> ToUppercase {
853 ToUppercase(CaseMappingIter::new(conversions::to_upper(self)))
857 /// An iterator that decodes UTF-16 encoded code points from an iterator of `u16`s.
858 #[unstable(feature = "decode_utf16", reason = "recently exposed", issue = "27830")]
860 pub struct DecodeUtf16<I>
861 where I: Iterator<Item = u16>
867 /// Create an iterator over the UTF-16 encoded code points in `iterable`,
868 /// returning unpaired surrogates as `Err`s.
875 /// #![feature(decode_utf16)]
877 /// use std::char::decode_utf16;
880 /// // 𝄞mus<invalid>ic<invalid>
881 /// let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
882 /// 0x0073, 0xDD1E, 0x0069, 0x0063,
885 /// assert_eq!(decode_utf16(v.iter().cloned()).collect::<Vec<_>>(),
887 /// Ok('m'), Ok('u'), Ok('s'),
889 /// Ok('i'), Ok('c'),
894 /// A lossy decoder can be obtained by replacing `Err` results with the replacement character:
897 /// #![feature(decode_utf16)]
899 /// use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
902 /// // 𝄞mus<invalid>ic<invalid>
903 /// let v = [0xD834, 0xDD1E, 0x006d, 0x0075,
904 /// 0x0073, 0xDD1E, 0x0069, 0x0063,
907 /// assert_eq!(decode_utf16(v.iter().cloned())
908 /// .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
909 /// .collect::<String>(),
913 #[unstable(feature = "decode_utf16", reason = "recently exposed", issue = "27830")]
915 pub fn decode_utf16<I: IntoIterator<Item = u16>>(iterable: I) -> DecodeUtf16<I::IntoIter> {
917 iter: iterable.into_iter(),
922 #[unstable(feature = "decode_utf16", reason = "recently exposed", issue = "27830")]
923 impl<I: Iterator<Item=u16>> Iterator for DecodeUtf16<I> {
924 type Item = Result<char, u16>;
926 fn next(&mut self) -> Option<Result<char, u16>> {
927 let u = match self.buf.take() {
929 None => match self.iter.next() {
935 if u < 0xD800 || 0xDFFF < u {
937 Some(Ok(unsafe { from_u32_unchecked(u as u32) }))
938 } else if u >= 0xDC00 {
939 // a trailing surrogate
942 let u2 = match self.iter.next() {
945 None => return Some(Err(u)),
947 if u2 < 0xDC00 || u2 > 0xDFFF {
948 // not a trailing surrogate so we're not a valid
949 // surrogate pair, so rewind to redecode u2 next time.
954 // all ok, so lets decode it.
955 let c = (((u - 0xD800) as u32) << 10 | (u2 - 0xDC00) as u32) + 0x1_0000;
956 Some(Ok(unsafe { from_u32_unchecked(c) }))
961 fn size_hint(&self) -> (usize, Option<usize>) {
962 let (low, high) = self.iter.size_hint();
963 // we could be entirely valid surrogates (2 elements per
964 // char), or entirely non-surrogates (1 element per char)
969 /// `U+FFFD REPLACEMENT CHARACTER` (�) is used in Unicode to represent a decoding error.
970 /// It can occur, for example, when giving ill-formed UTF-8 bytes to
971 /// [`String::from_utf8_lossy`](../string/struct.String.html#method.from_utf8_lossy).
972 #[unstable(feature = "decode_utf16", reason = "recently added", issue = "27830")]
973 pub const REPLACEMENT_CHARACTER: char = '\u{FFFD}';