@@ -2423,6 +2423,82 @@ 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(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!("\r hello world\u{3000}\n ".trim_ascii_end(), "\r hello world\u{3000}");
2464
+ /// assert_eq!(" ".trim_ascii_end(), "");
2465
+ /// assert_eq!("".trim_ascii_end(), "");
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!("\r hello world\n ".trim_ascii(), "hello world");
2490
+ /// assert_eq!(" ".trim_ascii(), "");
2491
+ /// assert_eq!("".trim_ascii(), "");
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
+
2426
2502
/// Return an iterator that escapes each char in `self` with [`char::escape_debug`].
2427
2503
///
2428
2504
/// Note: only extended grapheme codepoints that begin the string will be
0 commit comments