Skip to content

Commit 938f94d

Browse files
committed
Add ASCII whitespace trimming functions to &str
Add `str_trim_ascii` unstable feature. Add `trim_ascii_start`, `trim_ascii_end`, and `trim_ascii` functions to `&str` for trimming ASCII whitespace.
1 parent 9cf18e9 commit 938f94d

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

library/core/src/str/mod.rs

+76
Original file line numberDiff line numberDiff line change
@@ -2423,6 +2423,82 @@ impl str {
24232423
me.make_ascii_lowercase()
24242424
}
24252425

2426+
/// Returns a string slice with leading ASCII whitespace removed.
2427+
///
2428+
/// 'Whitespace' refers to the definition used by
2429+
/// [`u8::is_ascii_whitespace`].
2430+
///
2431+
/// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
2432+
///
2433+
/// # Examples
2434+
///
2435+
/// ```
2436+
/// #![feature(str_trim_ascii)]
2437+
///
2438+
/// assert_eq!(" \t \u{3000}hello world\n".trim_ascii_start(), "\u{3000}hello world\n");
2439+
/// assert_eq!(" ".trim_ascii_start(), "");
2440+
/// assert_eq!("".trim_ascii_start(), "");
2441+
/// ```
2442+
#[must_use = "this returns the trimmed string as a new slice, \
2443+
without modifying the original"]
2444+
#[unstable(feature = "str_trim_ascii", issue = "none")]
2445+
pub const fn trim_ascii_start(&self) -> &str {
2446+
// SAFETY: Removing ASCII characters from a `&str` does not invalidate
2447+
// UTF-8.
2448+
unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_start()) }
2449+
}
2450+
2451+
/// Returns a string slice with trailing ASCII whitespace removed.
2452+
///
2453+
/// 'Whitespace' refers to the definition used by
2454+
/// [`u8::is_ascii_whitespace`].
2455+
///
2456+
/// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
2457+
///
2458+
/// # Examples
2459+
///
2460+
/// ```
2461+
/// #![feature(str_trim_ascii)]
2462+
///
2463+
/// assert_eq!(b"\r hello world\u{3000}\n ".trim_ascii_end(), b"\r hello world\u{3000}");
2464+
/// assert_eq!(b" ".trim_ascii_end(), b"");
2465+
/// assert_eq!(b"".trim_ascii_end(), b"");
2466+
/// ```
2467+
#[must_use = "this returns the trimmed string as a new slice, \
2468+
without modifying the original"]
2469+
#[unstable(feature = "str_trim_ascii", issue = "none")]
2470+
pub const fn trim_ascii_end(&self) -> &str {
2471+
// SAFETY: Removing ASCII characters from a `&str` does not invalidate
2472+
// UTF-8.
2473+
unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_end()) }
2474+
}
2475+
2476+
/// Returns a string slice with leading and trailing ASCII whitespace
2477+
/// removed.
2478+
///
2479+
/// 'Whitespace' refers to the definition used by
2480+
/// [`u8::is_ascii_whitespace`].
2481+
///
2482+
/// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
2483+
///
2484+
/// # Examples
2485+
///
2486+
/// ```
2487+
/// #![feature(str_trim_ascii)]
2488+
///
2489+
/// assert_eq!(b"\r hello world\n ".trim_ascii(), b"hello world");
2490+
/// assert_eq!(b" ".trim_ascii(), b"");
2491+
/// assert_eq!(b"".trim_ascii(), b"");
2492+
/// ```
2493+
#[must_use = "this returns the trimmed string as a new slice, \
2494+
without modifying the original"]
2495+
#[unstable(feature = "str_trim_ascii", issue = "none")]
2496+
pub const fn trim_ascii(&self) -> &str {
2497+
// SAFETY: Removing ASCII characters from a `&str` does not invalidate
2498+
// UTF-8.
2499+
unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii()) }
2500+
}
2501+
24262502
/// Return an iterator that escapes each char in `self` with [`char::escape_debug`].
24272503
///
24282504
/// Note: only extended grapheme codepoints that begin the string will be

0 commit comments

Comments
 (0)