about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-03-27 10:47:50 +0200
committerGitHub <noreply@github.com>2018-03-27 10:47:50 +0200
commit1c45f6c051762d36a25c045c954a8a97130a7ad8 (patch)
tree0dfa163fb49c274a27650b98528c0bfe64c4c6be
parentdbd6c56f325daf9b2ae141f507ead35d178b2380 (diff)
parent5fc7e0a2ba84c44caebc5801f454663bafad2ca8 (diff)
downloadrust-1c45f6c051762d36a25c045c954a8a97130a7ad8.tar.gz
rust-1c45f6c051762d36a25c045c954a8a97130a7ad8.zip
Rollup merge of #49381 - withoutboats:str_unicode, r=SimonSapin
Add is_whitespace and is_alphanumeric to str.

The other methods from `UnicodeStr` are already stable inherent
methods on str, but these have not been included.

r? @SimonSapin
-rw-r--r--src/liballoc/str.rs42
-rw-r--r--src/librustdoc/test.rs2
2 files changed, 42 insertions, 2 deletions
diff --git a/src/liballoc/str.rs b/src/liballoc/str.rs
index 14d5e96d2e7..d5ef41df0d8 100644
--- a/src/liballoc/str.rs
+++ b/src/liballoc/str.rs
@@ -2122,6 +2122,48 @@ impl str {
         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
diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs
index 3ce8bd4ebb4..8ab9ca45187 100644
--- a/src/librustdoc/test.rs
+++ b/src/librustdoc/test.rs
@@ -406,8 +406,6 @@ pub fn make_test(s: &str,
 
 // FIXME(aburka): use a real parser to deal with multiline attributes
 fn partition_source(s: &str) -> (String, String) {
-    use std_unicode::str::UnicodeStr;
-
     let mut after_header = false;
     let mut before = String::new();
     let mut after = String::new();