Skip to content

Commit 1c45f6c

Browse files
authored
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
2 parents dbd6c56 + 5fc7e0a commit 1c45f6c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Diff for: src/liballoc/str.rs

+42
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,48 @@ impl str {
21222122
unsafe { String::from_utf8_unchecked(buf) }
21232123
}
21242124

2125+
/// Returns true if this `str` is entirely whitespace, and false otherwise.
2126+
///
2127+
/// 'Whitespace' is defined according to the terms of the Unicode Derived Core
2128+
/// Property `White_Space`.
2129+
///
2130+
/// # Examples
2131+
///
2132+
/// Basic usage:
2133+
///
2134+
/// ```
2135+
/// assert!(" \t ".is_whitespace());
2136+
///
2137+
/// // a non-breaking space
2138+
/// assert!("\u{A0}".is_whitespace());
2139+
///
2140+
/// assert!(!" 越".is_whitespace());
2141+
/// ```
2142+
#[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
2143+
#[inline]
2144+
pub fn is_whitespace(&self) -> bool {
2145+
UnicodeStr::is_whitespace(self)
2146+
}
2147+
2148+
/// Returns true if this `str` is entirely alphanumeric, and false otherwise.
2149+
///
2150+
/// 'Alphanumeric'-ness is defined in terms of the Unicode General Categories
2151+
/// 'Nd', 'Nl', 'No' and the Derived Core Property 'Alphabetic'.
2152+
///
2153+
/// # Examples
2154+
///
2155+
/// Basic usage:
2156+
///
2157+
/// ```
2158+
/// assert!("٣7৬Kو藏".is_alphanumeric());
2159+
/// assert!(!"¾①".is_alphanumeric());
2160+
/// ```
2161+
#[stable(feature = "unicode_methods_on_intrinsics", since = "1.27.0")]
2162+
#[inline]
2163+
pub fn is_alphanumeric(&self) -> bool {
2164+
UnicodeStr::is_alphanumeric(self)
2165+
}
2166+
21252167
/// Checks if all characters in this string are within the ASCII range.
21262168
///
21272169
/// # Examples

Diff for: src/librustdoc/test.rs

-2
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,6 @@ pub fn make_test(s: &str,
406406

407407
// FIXME(aburka): use a real parser to deal with multiline attributes
408408
fn partition_source(s: &str) -> (String, String) {
409-
use std_unicode::str::UnicodeStr;
410-
411409
let mut after_header = false;
412410
let mut before = String::new();
413411
let mut after = String::new();

0 commit comments

Comments
 (0)