]> git.lizzy.rs Git - rust.git/commitdiff
Add is_whitespace and is_alphanumeric to str.
authorboats <boats@mozilla.com>
Mon, 26 Mar 2018 13:25:31 +0000 (06:25 -0700)
committerboats <boats@mozilla.com>
Mon, 26 Mar 2018 13:25:31 +0000 (06:25 -0700)
The other methods from `UnicodeStr` are already stable inherent
methods on str, but these have not been included.

src/liballoc/str.rs

index 14d5e96d2e73a87853ea103a9abe18bd768c1b40..d5ef41df0d850d5804299a328f617f411f3abdd2 100644 (file)
@@ -2122,6 +2122,48 @@ pub fn repeat(&self, n: usize) -> String {
         unsafe { String::from_utf8_unchecked(buf) }
     }
 
+    /// Returns true if this `str` is entirely whitespace, and false otherwise.
+    ///
+    /// 'Whitespace' is defined according to the terms of the Unicode Derived Core
+    /// Property `White_Space`.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// assert!("    \t ".is_whitespace());
+    ///
+    /// // a non-breaking space
+    /// assert!("\u{A0}".is_whitespace());
+    ///
+    /// assert!(!"   越".is_whitespace());
+    /// ```
+    #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
+    #[inline]
+    pub fn is_whitespace(&self) -> bool {
+        UnicodeStr::is_whitespace(self)
+    }
+
+    /// Returns true if this `str` is entirely alphanumeric, and false otherwise.
+    ///
+    /// 'Alphanumeric'-ness is defined in terms of the Unicode General Categories
+    /// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
+    ///
+    /// # Examples
+    ///
+    /// Basic usage:
+    ///
+    /// ```
+    /// assert!("٣7৬Kو藏".is_alphanumeric());
+    /// assert!(!"¾①".is_alphanumeric());
+    /// ```
+    #[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
+    #[inline]
+    pub fn is_alphanumeric(&self) -> bool {
+        UnicodeStr::is_alphanumeric(self)
+    }
+
     /// Checks if all characters in this string are within the ASCII range.
     ///
     /// # Examples