@@ -19,7 +19,7 @@ use self::pattern::{Searcher, ReverseSearcher, DoubleEndedSearcher};
19
19
20
20
use char;
21
21
use fmt;
22
- use iter:: { Map , Cloned , FusedIterator , TrustedLen } ;
22
+ use iter:: { Map , Cloned , FusedIterator , TrustedLen , Filter } ;
23
23
use iter_private:: TrustedRandomAccess ;
24
24
use slice:: { self , SliceIndex } ;
25
25
use mem;
@@ -2216,6 +2216,18 @@ pub trait StrExt {
2216
2216
fn is_empty ( & self ) -> bool ;
2217
2217
#[ stable( feature = "core" , since = "1.6.0" ) ]
2218
2218
fn parse < T : FromStr > ( & self ) -> Result < T , T :: Err > ;
2219
+ #[ stable( feature = "split_whitespace" , since = "1.1.0" ) ]
2220
+ fn split_whitespace < ' a > ( & ' a self ) -> SplitWhitespace < ' a > ;
2221
+ #[ stable( feature = "unicode_methods_on_intrinsics" , since = "1.27.0" ) ]
2222
+ fn is_whitespace ( & self ) -> bool ;
2223
+ #[ stable( feature = "unicode_methods_on_intrinsics" , since = "1.27.0" ) ]
2224
+ fn is_alphanumeric ( & self ) -> bool ;
2225
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2226
+ fn trim ( & self ) -> & str ;
2227
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2228
+ fn trim_left ( & self ) -> & str ;
2229
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2230
+ fn trim_right ( & self ) -> & str ;
2219
2231
}
2220
2232
2221
2233
// truncate `&str` to length at most equal to `max`
@@ -2536,6 +2548,36 @@ impl StrExt for str {
2536
2548
2537
2549
#[ inline]
2538
2550
fn parse < T : FromStr > ( & self ) -> Result < T , T :: Err > { FromStr :: from_str ( self ) }
2551
+
2552
+ #[ inline]
2553
+ fn split_whitespace ( & self ) -> SplitWhitespace {
2554
+ SplitWhitespace { inner : self . split ( IsWhitespace ) . filter ( IsNotEmpty ) }
2555
+ }
2556
+
2557
+ #[ inline]
2558
+ fn is_whitespace ( & self ) -> bool {
2559
+ self . chars ( ) . all ( |c| c. is_whitespace ( ) )
2560
+ }
2561
+
2562
+ #[ inline]
2563
+ fn is_alphanumeric ( & self ) -> bool {
2564
+ self . chars ( ) . all ( |c| c. is_alphanumeric ( ) )
2565
+ }
2566
+
2567
+ #[ inline]
2568
+ fn trim ( & self ) -> & str {
2569
+ self . trim_matches ( |c : char | c. is_whitespace ( ) )
2570
+ }
2571
+
2572
+ #[ inline]
2573
+ fn trim_left ( & self ) -> & str {
2574
+ self . trim_left_matches ( |c : char | c. is_whitespace ( ) )
2575
+ }
2576
+
2577
+ #[ inline]
2578
+ fn trim_right ( & self ) -> & str {
2579
+ self . trim_right_matches ( |c : char | c. is_whitespace ( ) )
2580
+ }
2539
2581
}
2540
2582
2541
2583
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -2551,3 +2593,75 @@ impl<'a> Default for &'a str {
2551
2593
/// Creates an empty str
2552
2594
fn default ( ) -> & ' a str { "" }
2553
2595
}
2596
+
2597
+ /// An iterator over the non-whitespace substrings of a string,
2598
+ /// separated by any amount of whitespace.
2599
+ ///
2600
+ /// This struct is created by the [`split_whitespace`] method on [`str`].
2601
+ /// See its documentation for more.
2602
+ ///
2603
+ /// [`split_whitespace`]: ../../std/primitive.str.html#method.split_whitespace
2604
+ /// [`str`]: ../../std/primitive.str.html
2605
+ #[ stable( feature = "split_whitespace" , since = "1.1.0" ) ]
2606
+ #[ derive( Clone , Debug ) ]
2607
+ pub struct SplitWhitespace < ' a > {
2608
+ inner : Filter < Split < ' a , IsWhitespace > , IsNotEmpty > ,
2609
+ }
2610
+
2611
+ #[ derive( Clone ) ]
2612
+ struct IsWhitespace ;
2613
+
2614
+ impl FnOnce < ( char , ) > for IsWhitespace {
2615
+ type Output = bool ;
2616
+
2617
+ #[ inline]
2618
+ extern "rust-call" fn call_once ( mut self , arg : ( char , ) ) -> bool {
2619
+ self . call_mut ( arg)
2620
+ }
2621
+ }
2622
+
2623
+ impl FnMut < ( char , ) > for IsWhitespace {
2624
+ #[ inline]
2625
+ extern "rust-call" fn call_mut ( & mut self , arg : ( char , ) ) -> bool {
2626
+ arg. 0 . is_whitespace ( )
2627
+ }
2628
+ }
2629
+
2630
+ #[ derive( Clone ) ]
2631
+ struct IsNotEmpty ;
2632
+
2633
+ impl < ' a , ' b > FnOnce < ( & ' a & ' b str , ) > for IsNotEmpty {
2634
+ type Output = bool ;
2635
+
2636
+ #[ inline]
2637
+ extern "rust-call" fn call_once ( mut self , arg : ( & & str , ) ) -> bool {
2638
+ self . call_mut ( arg)
2639
+ }
2640
+ }
2641
+
2642
+ impl < ' a , ' b > FnMut < ( & ' a & ' b str , ) > for IsNotEmpty {
2643
+ #[ inline]
2644
+ extern "rust-call" fn call_mut ( & mut self , arg : ( & & str , ) ) -> bool {
2645
+ !arg. 0 . is_empty ( )
2646
+ }
2647
+ }
2648
+
2649
+
2650
+ #[ stable( feature = "split_whitespace" , since = "1.1.0" ) ]
2651
+ impl < ' a > Iterator for SplitWhitespace < ' a > {
2652
+ type Item = & ' a str ;
2653
+
2654
+ fn next ( & mut self ) -> Option < & ' a str > {
2655
+ self . inner . next ( )
2656
+ }
2657
+ }
2658
+
2659
+ #[ stable( feature = "split_whitespace" , since = "1.1.0" ) ]
2660
+ impl < ' a > DoubleEndedIterator for SplitWhitespace < ' a > {
2661
+ fn next_back ( & mut self ) -> Option < & ' a str > {
2662
+ self . inner . next_back ( )
2663
+ }
2664
+ }
2665
+
2666
+ #[ stable( feature = "fused" , since = "1.26.0" ) ]
2667
+ impl < ' a > FusedIterator for SplitWhitespace < ' a > { }
0 commit comments