Skip to content

Commit e4808af

Browse files
committed
Add ASCII whitespace trimming functions to &str
Add `trim_ascii_start`, `trim_ascii_end`, and `trim_ascii` functions to `&str` for trimming ASCII whitespace under the `byte_slice_trim_ascii` feature gate. Add `inline` to `[u8]` `trim_ascii` functions
1 parent 9cf18e9 commit e4808af

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

library/core/src/slice/ascii.rs

+3
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl [u8] {
125125
/// assert_eq!(b"".trim_ascii_start(), b"");
126126
/// ```
127127
#[unstable(feature = "byte_slice_trim_ascii", issue = "94035")]
128+
#[inline]
128129
pub const fn trim_ascii_start(&self) -> &[u8] {
129130
let mut bytes = self;
130131
// Note: A pattern matching based approach (instead of indexing) allows
@@ -154,6 +155,7 @@ impl [u8] {
154155
/// assert_eq!(b"".trim_ascii_end(), b"");
155156
/// ```
156157
#[unstable(feature = "byte_slice_trim_ascii", issue = "94035")]
158+
#[inline]
157159
pub const fn trim_ascii_end(&self) -> &[u8] {
158160
let mut bytes = self;
159161
// Note: A pattern matching based approach (instead of indexing) allows
@@ -184,6 +186,7 @@ impl [u8] {
184186
/// assert_eq!(b"".trim_ascii(), b"");
185187
/// ```
186188
#[unstable(feature = "byte_slice_trim_ascii", issue = "94035")]
189+
#[inline]
187190
pub const fn trim_ascii(&self) -> &[u8] {
188191
self.trim_ascii_start().trim_ascii_end()
189192
}

library/core/src/str/mod.rs

+79
Original file line numberDiff line numberDiff line change
@@ -2423,6 +2423,85 @@ 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(byte_slice_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+
#[unstable(feature = "byte_slice_trim_ascii", issue = "94035")]
2443+
#[must_use = "this returns the trimmed string as a new slice, \
2444+
without modifying the original"]
2445+
#[inline]
2446+
pub const fn trim_ascii_start(&self) -> &str {
2447+
// SAFETY: Removing ASCII characters from a `&str` does not invalidate
2448+
// UTF-8.
2449+
unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_start()) }
2450+
}
2451+
2452+
/// Returns a string slice with trailing ASCII whitespace removed.
2453+
///
2454+
/// 'Whitespace' refers to the definition used by
2455+
/// [`u8::is_ascii_whitespace`].
2456+
///
2457+
/// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
2458+
///
2459+
/// # Examples
2460+
///
2461+
/// ```
2462+
/// #![feature(byte_slice_trim_ascii)]
2463+
///
2464+
/// assert_eq!("\r hello world\u{3000}\n ".trim_ascii_end(), "\r hello world\u{3000}");
2465+
/// assert_eq!(" ".trim_ascii_end(), "");
2466+
/// assert_eq!("".trim_ascii_end(), "");
2467+
/// ```
2468+
#[unstable(feature = "byte_slice_trim_ascii", issue = "94035")]
2469+
#[must_use = "this returns the trimmed string as a new slice, \
2470+
without modifying the original"]
2471+
#[inline]
2472+
pub const fn trim_ascii_end(&self) -> &str {
2473+
// SAFETY: Removing ASCII characters from a `&str` does not invalidate
2474+
// UTF-8.
2475+
unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii_end()) }
2476+
}
2477+
2478+
/// Returns a string slice with leading and trailing ASCII whitespace
2479+
/// removed.
2480+
///
2481+
/// 'Whitespace' refers to the definition used by
2482+
/// [`u8::is_ascii_whitespace`].
2483+
///
2484+
/// [`u8::is_ascii_whitespace`]: u8::is_ascii_whitespace
2485+
///
2486+
/// # Examples
2487+
///
2488+
/// ```
2489+
/// #![feature(byte_slice_trim_ascii)]
2490+
///
2491+
/// assert_eq!("\r hello world\n ".trim_ascii(), "hello world");
2492+
/// assert_eq!(" ".trim_ascii(), "");
2493+
/// assert_eq!("".trim_ascii(), "");
2494+
/// ```
2495+
#[unstable(feature = "byte_slice_trim_ascii", issue = "94035")]
2496+
#[must_use = "this returns the trimmed string as a new slice, \
2497+
without modifying the original"]
2498+
#[inline]
2499+
pub const fn trim_ascii(&self) -> &str {
2500+
// SAFETY: Removing ASCII characters from a `&str` does not invalidate
2501+
// UTF-8.
2502+
unsafe { core::str::from_utf8_unchecked(self.as_bytes().trim_ascii()) }
2503+
}
2504+
24262505
/// Return an iterator that escapes each char in `self` with [`char::escape_debug`].
24272506
///
24282507
/// Note: only extended grapheme codepoints that begin the string will be

0 commit comments

Comments
 (0)