@@ -2423,6 +2423,85 @@ impl str {
2423
2423
me. make_ascii_lowercase ( )
2424
2424
}
2425
2425
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
+
2426
2505
/// Return an iterator that escapes each char in `self` with [`char::escape_debug`].
2427
2506
///
2428
2507
/// Note: only extended grapheme codepoints that begin the string will be
0 commit comments