about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/librustc_unicode/char.rs467
1 files changed, 392 insertions, 75 deletions
diff --git a/src/librustc_unicode/char.rs b/src/librustc_unicode/char.rs
index e08b3244109..207dc7aacbf 100644
--- a/src/librustc_unicode/char.rs
+++ b/src/librustc_unicode/char.rs
@@ -110,53 +110,122 @@ impl Iterator for CaseMappingIter {
 #[stable(feature = "rust1", since = "1.0.0")]
 #[lang = "char"]
 impl char {
-    /// Checks if a `char` parses as a numeric digit in the given radix.
+    /// Checks if a `char` is a digit in the given radix.
+    ///
+    /// A 'radix' here is sometimes also called a 'base'. A radix of two
+    /// indicates a binary number, a radix of ten, decimal, and a radix of
+    /// sixteen, hexicdecimal, to give some common values. Arbitrary
+    /// radicum are supported.
     ///
     /// Compared to `is_numeric()`, this function only recognizes the characters
     /// `0-9`, `a-z` and `A-Z`.
     ///
-    /// # Return value
+    /// 'Digit' is defined to be only the following characters:
     ///
-    /// Returns `true` if `c` is a valid digit under `radix`, and `false`
-    /// otherwise.
+    /// * `0-9`
+    /// * `a-z`
+    /// * `A-Z`
+    ///
+    /// For a more comprehensive understanding of 'digit', see [`is_numeric()`][is_numeric].
+    ///
+    /// [is_numeric]: #method.is_numeric
     ///
     /// # Panics
     ///
-    /// Panics if given a radix > 36.
+    /// Panics if given a radix larger than 36.
     ///
     /// # Examples
     ///
+    /// Basic usage:
+    ///
     /// ```
-    /// let c = '1';
+    /// let d = '1';
     ///
-    /// assert!(c.is_digit(10));
+    /// assert!(d.is_digit(10));
     ///
-    /// assert!('f'.is_digit(16));
+    /// let d = 'f';
+    ///
+    /// assert!(d.is_digit(16));
+    /// assert!(!d.is_digit(10));
+    /// ```
+    ///
+    /// Passing a large radix, causing a panic:
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let result = thread::spawn(|| {
+    ///     let d = '1';
+    ///
+    ///     // this panics
+    ///     d.is_digit(37);
+    /// }).join();
+    ///
+    /// assert!(result.is_err());
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_digit(self, radix: u32) -> bool { C::is_digit(self, radix) }
 
-    /// Converts a character to the corresponding digit.
+    /// Converts a `char` to a digit in the given radix.
+    ///
+    /// A 'radix' here is sometimes also called a 'base'. A radix of two
+    /// indicates a binary number, a radix of ten, decimal, and a radix of
+    /// sixteen, hexicdecimal, to give some common values. Arbitrary
+    /// radicum are supported.
+    ///
+    /// 'Digit' is defined to be only the following characters:
     ///
-    /// # Return value
+    /// * `0-9`
+    /// * `a-z`
+    /// * `A-Z`
     ///
-    /// If `c` is between '0' and '9', the corresponding value between 0 and
-    /// 9. If `c` is 'a' or 'A', 10. If `c` is 'b' or 'B', 11, etc. Returns
-    /// none if the character does not refer to a digit in the given radix.
+    /// # Failure
+    ///
+    /// Returns `None` if the `char` does not refer to a digit in the given radix.
     ///
     /// # Panics
     ///
-    /// Panics if given a radix outside the range [0..36].
+    /// Panics if given a radix larger than 36.
     ///
     /// # Examples
     ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let d = '1';
+    ///
+    /// assert_eq!(d.to_digit(10), Some(1));
+    ///
+    /// let d = 'f';
+    ///
+    /// assert_eq!(d.to_digit(16), Some(15));
+    /// ```
+    ///
+    /// Passing a non-digit results in failure:
+    ///
     /// ```
-    /// let c = '1';
+    /// let d = 'f';
     ///
-    /// assert_eq!(c.to_digit(10), Some(1));
+    /// assert_eq!(d.to_digit(10), None);
     ///
-    /// assert_eq!('f'.to_digit(16), Some(15));
+    /// let d = 'z';
+    ///
+    /// assert_eq!(d.to_digit(16), None);
+    /// ```
+    ///
+    /// Passing a large radix, causing a panic:
+    ///
+    /// ```
+    /// use std::thread;
+    ///
+    /// let result = thread::spawn(|| {
+    ///   let d = '1';
+    ///
+    ///   d.to_digit(37);
+    /// }).join();
+    ///
+    /// assert!(result.is_err());
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
@@ -195,21 +264,29 @@ impl char {
     #[inline]
     pub fn escape_unicode(self) -> EscapeUnicode { C::escape_unicode(self) }
 
-    /// Returns an iterator that yields the 'default' ASCII and
-    /// C++11-like literal escape of a character, as `char`s.
+    /// Returns an iterator that yields the literal escape code of a `char`.
     ///
     /// The default is chosen with a bias toward producing literals that are
     /// legal in a variety of languages, including C++11 and similar C-family
     /// languages. The exact rules are:
     ///
-    /// * Tab, CR and LF are escaped as '\t', '\r' and '\n' respectively.
-    /// * Single-quote, double-quote and backslash chars are backslash-
-    ///   escaped.
-    /// * Any other chars in the range [0x20,0x7e] are not escaped.
-    /// * Any other chars are given hex Unicode escapes; see `escape_unicode`.
+    /// * Tab is escaped as `\t`.
+    /// * Carriage return is escaped as `\r`.
+    /// * Line feed is escaped as `\n`.
+    /// * Single quote is escaped as `\'`.
+    /// * Double quote is escaped as `\"`.
+    /// * Backslash is escaped as `\\`.
+    /// * Any character in the 'printable ASCII' range `0x20` .. `0x7e`
+    ///   inclusive is not escaped.
+    /// * All other characters are given hexadecimal Unicode escapes; see
+    ///   [`escape_unicode`][escape_unicode].
+    ///
+    /// [escape_unicode]: #method.escape_unicode
     ///
     /// # Examples
     ///
+    /// Basic usage:
+    ///
     /// ```
     /// for i in '"'.escape_default() {
     ///     println!("{}", i);
@@ -234,29 +311,70 @@ impl char {
     #[inline]
     pub fn escape_default(self) -> EscapeDefault { C::escape_default(self) }
 
-    /// Returns the number of bytes this character would need if encoded in
-    /// UTF-8.
+    /// Returns the number of bytes this `char` would need if encoded in UTF-8.
+    ///
+    /// That number of bytes is always between 1 and 4, inclusive.
     ///
     /// # Examples
     ///
+    /// Basic usage:
+    ///
     /// ```
-    /// let n = 'ß'.len_utf8();
+    /// let len = 'A'.len_utf8();
+    /// assert_eq!(len, 1);
+    ///
+    /// let len = 'ß'.len_utf8();
+    /// assert_eq!(len, 2);
     ///
-    /// assert_eq!(n, 2);
+    /// let len = 'ℝ'.len_utf8();
+    /// assert_eq!(len, 3);
+    ///
+    /// let len = '💣'.len_utf8();
+    /// assert_eq!(len, 4);
+    /// ```
+    ///
+    /// The `&str` type guarantees that its contents are UTF-8, and so we can compare the length it
+    /// would take if each code point was represented as a `char` vs in the `&str` itself:
+    ///
+    /// ```
+    /// // as chars
+    /// let eastern = '東';
+    /// let capitol = '京';
+    ///
+    /// // both can be represented as three bytes
+    /// assert_eq!(3, eastern.len_utf8());
+    /// assert_eq!(3, capitol.len_utf8());
+    ///
+    /// // as a &str, these two are encoded in UTF-8
+    /// let tokyo = "東京";
+    ///
+    /// let len = eastern.len_utf8() + capitol.len_utf8();
+    ///
+    /// // we can see that they take six bytes total...
+    /// assert_eq!(6, tokyo.len());
+    ///
+    /// // ... just like the &str
+    /// assert_eq!(len, tokyo.len());
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn len_utf8(self) -> usize { C::len_utf8(self) }
 
-    /// Returns the number of 16-bit code units this character would need if
+    /// Returns the number of 16-bit code units this `char` would need if
     /// encoded in UTF-16.
     ///
+    /// See the documentation for [`len_utf8()`][len_utf8] for more explanation
+    /// of this concept. This function is a mirror, but for UTF-16 instead of
+    /// UTF-8.
+    ///
     /// # Examples
     ///
     /// ```
     /// let n = 'ß'.len_utf16();
-    ///
     /// assert_eq!(n, 1);
+    ///
+    /// let len = '💣'.len_utf16();
+    /// assert_eq!(len, 2);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
@@ -342,8 +460,24 @@ impl char {
         C::encode_utf16(self, dst)
     }
 
-    /// Returns whether the specified character is considered a Unicode
-    /// alphabetic code point.
+    /// Returns true if this `char` is an alphabetic code point, and false if not.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let c = 'a';
+    ///
+    /// assert!(c.is_alphabetic());
+    ///
+    /// let c = '京';
+    /// assert!(c.is_alphabetic());
+    ///
+    /// let c = '💝';
+    /// // love is many things, but it is not alphabetic
+    /// assert!(!c.is_alphabetic());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_alphabetic(self) -> bool {
@@ -354,20 +488,20 @@ impl char {
         }
     }
 
-    /// Returns whether the specified character satisfies the 'XID_Start'
-    /// Unicode property.
+    /// Returns true if this `char` satisfies the 'XID_Start' Unicode property, and false
+    /// otherwise.
     ///
     /// 'XID_Start' is a Unicode Derived Property specified in
     /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
-    /// mostly similar to ID_Start but modified for closure under NFKx.
+    /// mostly similar to `ID_Start` but modified for closure under `NFKx`.
     #[unstable(feature = "unicode",
                reason = "mainly needed for compiler internals",
                issue = "0")]
     #[inline]
     pub fn is_xid_start(self) -> bool { derived_property::XID_Start(self) }
 
-    /// Returns whether the specified `char` satisfies the 'XID_Continue'
-    /// Unicode property.
+    /// Returns true if this `char` satisfies the 'XID_Continue' Unicode property, and false
+    /// otherwise.
     ///
     /// 'XID_Continue' is a Unicode Derived Property specified in
     /// [UAX #31](http://unicode.org/reports/tr31/#NFKC_Modifications),
@@ -378,10 +512,32 @@ impl char {
     #[inline]
     pub fn is_xid_continue(self) -> bool { derived_property::XID_Continue(self) }
 
-    /// Indicates whether a character is in lowercase.
+    /// Returns true if this `char` is lowercase, and false otherwise.
     ///
-    /// This is defined according to the terms of the Unicode Derived Core
+    /// 'Lowercase' is defined according to the terms of the Unicode Derived Core
     /// Property `Lowercase`.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let c = 'a';
+    /// assert!(c.is_lowercase());
+    ///
+    /// let c = 'δ';
+    /// assert!(c.is_lowercase());
+    ///
+    /// let c = 'A';
+    /// assert!(!c.is_lowercase());
+    ///
+    /// let c = 'Δ';
+    /// assert!(!c.is_lowercase());
+    ///
+    /// // The various Chinese scripts do not have case, and so:
+    /// let c = '中';
+    /// assert!(!c.is_lowercase());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_lowercase(self) -> bool {
@@ -392,10 +548,32 @@ impl char {
         }
     }
 
-    /// Indicates whether a character is in uppercase.
+    /// Returns true if this `char` is uppercase, and false otherwise.
     ///
-    /// This is defined according to the terms of the Unicode Derived Core
+    /// 'Uppercase' is defined according to the terms of the Unicode Derived Core
     /// Property `Uppercase`.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let c = 'a';
+    /// assert!(!c.is_uppercase());
+    ///
+    /// let c = 'δ';
+    /// assert!(!c.is_uppercase());
+    ///
+    /// let c = 'A';
+    /// assert!(c.is_uppercase());
+    ///
+    /// let c = 'Δ';
+    /// assert!(c.is_uppercase());
+    ///
+    /// // The various Chinese scripts do not have case, and so:
+    /// let c = '中';
+    /// assert!(!c.is_uppercase());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_uppercase(self) -> bool {
@@ -406,9 +584,26 @@ impl char {
         }
     }
 
-    /// Indicates whether a character is whitespace.
+    /// Returns true if this `char` is whitespace, and false otherwise.
     ///
-    /// Whitespace is defined in terms of the Unicode Property `White_Space`.
+    /// 'Whitespace' is defined according to the terms of the Unicode Derived Core
+    /// Property `White_Space`.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let c = ' ';
+    /// assert!(c.is_whitespace());
+    ///
+    /// // a non-breaking space
+    /// let c = '\u{A0}';
+    /// assert!(c.is_whitespace());
+    ///
+    /// let c = '越';
+    /// assert!(!c.is_whitespace());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_whitespace(self) -> bool {
@@ -419,25 +614,101 @@ impl char {
         }
     }
 
-    /// Indicates whether a character is alphanumeric.
+    /// Returns true if this `char` is alphanumeric, and false otherwise.
     ///
-    /// Alphanumericness is defined in terms of the Unicode General Categories
+    /// 'Alphanumeric'-ness is defined in terms of the Unicode General Categories
     /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let c = '٣';
+    /// assert!(c.is_alphanumeric());
+    ///
+    /// let c = '7';
+    /// assert!(c.is_alphanumeric());
+    ///
+    /// let c = '৬';
+    /// assert!(c.is_alphanumeric());
+    ///
+    /// let c = 'K';
+    /// assert!(c.is_alphanumeric());
+    ///
+    /// let c = 'و';
+    /// assert!(c.is_alphanumeric());
+    ///
+    /// let c = '藏';
+    /// assert!(c.is_alphanumeric());
+    ///
+    /// let c = '¾';
+    /// assert!(!c.is_alphanumeric());
+    ///
+    /// let c = '①';
+    /// assert!(!c.is_alphanumeric());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_alphanumeric(self) -> bool {
         self.is_alphabetic() || self.is_numeric()
     }
 
-    /// Indicates whether a character is a control code point.
+    /// Returns true if this `char` is a control code point, and false otherwise.
     ///
-    /// Control code points are defined in terms of the Unicode General
+    /// 'Control code point' is defined in terms of the Unicode General
     /// Category `Cc`.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// // U+009C, STRING TERMINATOR
+    /// let c = 'œ';
+    /// assert!(c.is_control());
+    ///
+    /// let c = 'q';
+    /// assert!(!c.is_control());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_control(self) -> bool { general_category::Cc(self) }
 
-    /// Indicates whether the character is numeric (Nd, Nl, or No).
+    /// Returns true if this `char` is numeric, and false otherwise.
+    ///
+    /// 'Numeric'-ness is defined in terms of the Unicode General Categories
+    /// 'Nd', 'Nl', 'No'.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let c = '٣';
+    /// assert!(c.is_numeric());
+    ///
+    /// let c = '7';
+    /// assert!(c.is_numeric());
+    ///
+    /// let c = '৬';
+    /// assert!(c.is_numeric());
+    ///
+    /// let c = 'K';
+    /// assert!(!c.is_numeric());
+    ///
+    /// let c = 'و';
+    /// assert!(!c.is_numeric());
+    ///
+    /// let c = '藏';
+    /// assert!(!c.is_numeric());
+    ///
+    /// let c = '¾';
+    /// assert!(!c.is_numeric());
+    ///
+    /// let c = '①';
+    /// assert!(!c.is_numeric());
+    /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn is_numeric(self) -> bool {
@@ -448,21 +719,36 @@ impl char {
         }
     }
 
-    /// Converts a character to its lowercase equivalent.
+    /// Returns an iterator that yields the lowercase equivalent of a `char`.
     ///
-    /// This performs complex unconditional mappings with no tailoring.
-    /// See `to_uppercase()` for references and more information.
+    /// If no conversion is possible then an iterator with just the input character is returned.
     ///
-    /// # Return value
+    /// This performs complex unconditional mappings with no tailoring: it maps
+    /// one Unicode character to its lowercase equivalent according to the
+    /// [Unicode database] and the additional complex mappings
+    /// [`SpecialCasing.txt`]. Conditional mappings (based on context or
+    /// language) are not considered here.
     ///
-    /// Returns an iterator which yields the characters corresponding to the
-    /// lowercase equivalent of the character. If no conversion is possible then
-    /// an iterator with just the input character is returned.
+    /// For a full reference, see [here][reference].
+    ///
+    /// [Unicode database]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
+    ///
+    /// [`SpecialCasing.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
+    ///
+    /// [reference]: http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
     ///
     /// # Examples
     ///
+    /// Basic usage:
+    ///
     /// ```
-    /// assert_eq!(Some('c'), 'C'.to_lowercase().next());
+    /// let c = 'c';
+    ///
+    /// assert_eq!(c.to_uppercase().next(), Some('C'));
+    ///
+    /// // Japanese scripts do not have case, and so:
+    /// let c = '山';
+    /// assert_eq!(c.to_uppercase().next(), Some('山'));
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
@@ -470,33 +756,63 @@ impl char {
         ToLowercase(CaseMappingIter::new(conversions::to_lower(self)))
     }
 
-    /// Converts a character to its uppercase equivalent.
-    ///
-    /// This performs complex unconditional mappings with no tailoring:
-    /// it maps one Unicode character to its uppercase equivalent
-    /// according to the Unicode database [1]
-    /// and the additional complex mappings [`SpecialCasing.txt`].
-    /// Conditional mappings (based on context or language) are not considered here.
+    /// Returns an iterator that yields the uppercase equivalent of a `char`.
     ///
-    /// A full reference can be found here [2].
+    /// If no conversion is possible then an iterator with just the input character is returned.
     ///
-    /// # Return value
+    /// This performs complex unconditional mappings with no tailoring: it maps
+    /// one Unicode character to its uppercase equivalent according to the
+    /// [Unicode database] and the additional complex mappings
+    /// [`SpecialCasing.txt`]. Conditional mappings (based on context or
+    /// language) are not considered here.
     ///
-    /// Returns an iterator which yields the characters corresponding to the
-    /// uppercase equivalent of the character. If no conversion is possible then
-    /// an iterator with just the input character is returned.
+    /// For a full reference, see [here][reference].
     ///
-    /// [1]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
+    /// [Unicode database]: ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
     ///
     /// [`SpecialCasing.txt`]: ftp://ftp.unicode.org/Public/UNIDATA/SpecialCasing.txt
     ///
-    /// [2]: http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
+    /// [reference]: http://www.unicode.org/versions/Unicode7.0.0/ch03.pdf#G33992
     ///
     /// # Examples
     ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// let c = 'c';
+    /// assert_eq!(c.to_uppercase().next(), Some('C'));
+    ///
+    /// // Japanese does not have case, and so:
+    /// let c = '山';
+    /// assert_eq!(c.to_uppercase().next(), Some('山'));
+    /// ```
+    ///
+    /// In Turkish, the equivalent of 'i' in Latin has five forms instead of two:
+    ///
+    /// * 'Dotless': I / ı, sometimes written ï
+    /// * 'Dotted': İ / i
+    ///
+    /// Note that the lowercase dotted 'i' is the same as the Latin. Therefore:
+    ///
+    /// ```
+    /// let i = 'i';
+    ///
+    /// let upper_i = i.to_uppercase().next();
     /// ```
-    /// assert_eq!(Some('C'), 'c'.to_uppercase().next());
+    ///
+    /// The value of `upper_i` here relies on the language of the text: if we're
+    /// in `en-US`, it should be `Some('I')`, but if we're in `tr_TR`, it should
+    /// be `Some('İ')`. `to_uppercase()` does not take this into account, and so:
+    ///
     /// ```
+    /// let i = 'i';
+    ///
+    /// let upper_i = i.to_uppercase().next();
+    ///
+    /// assert_eq!(Some('I'), upper_i);
+    /// ```
+    ///
+    /// holds across languages.
     #[stable(feature = "rust1", since = "1.0.0")]
     #[inline]
     pub fn to_uppercase(self) -> ToUppercase {
@@ -504,7 +820,7 @@ impl char {
     }
 }
 
-/// An iterator that decodes UTF-16 encoded codepoints from an iterator of `u16`s.
+/// An iterator that decodes UTF-16 encoded code points from an iterator of `u16`s.
 #[unstable(feature = "decode_utf16", reason = "recently exposed", issue = "27830")]
 #[derive(Clone)]
 pub struct DecodeUtf16<I> where I: Iterator<Item=u16> {
@@ -512,7 +828,7 @@ pub struct DecodeUtf16<I> where I: Iterator<Item=u16> {
     buf: Option<u16>,
 }
 
-/// Create an iterator over the UTF-16 encoded codepoints in `iterable`,
+/// Create an iterator over the UTF-16 encoded code points in `iterable`,
 /// returning unpaired surrogates as `Err`s.
 ///
 /// # Examples
@@ -612,7 +928,8 @@ impl<I: Iterator<Item=u16>> Iterator for DecodeUtf16<I> {
     }
 }
 
-/// U+FFFD REPLACEMENT CHARACTER (�) is used in Unicode to represent a decoding error.
-/// It can occur, for example, when giving ill-formed UTF-8 bytes to `String::from_utf8_lossy`.
+/// `U+FFFD REPLACEMENT CHARACTER` (�) is used in Unicode to represent a decoding error.
+/// It can occur, for example, when giving ill-formed UTF-8 bytes to
+/// [`String::from_utf8_lossy`](../string/struct.String.html#method.from_utf8_lossy).
 #[unstable(feature = "decode_utf16", reason = "recently added", issue = "27830")]
 pub const REPLACEMENT_CHARACTER: char = '\u{FFFD}';