@@ -567,7 +567,6 @@ struct CharSplitsN<'a, P: Pattern<'a>> {
567
567
iter : CharSplits < ' a , P > ,
568
568
/// The number of splits remaining
569
569
count : usize ,
570
- invert : bool ,
571
570
}
572
571
573
572
/// An iterator over the substrings of a string, separated by a
@@ -582,6 +581,13 @@ struct RCharSplits<'a, P: Pattern<'a>> {
582
581
finished : bool ,
583
582
}
584
583
584
+ /// An iterator over the substrings of a string, separated by a
585
+ /// pattern, splitting at most `count` times, in reverse order.
586
+ struct RCharSplitsN < ' a , P : Pattern < ' a > > {
587
+ iter : RCharSplits < ' a , P > ,
588
+ /// The number of splits remaining
589
+ count : usize ,
590
+ }
585
591
586
592
/// An iterator over the lines of a string, separated by `\n`.
587
593
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -661,15 +667,14 @@ where P::Searcher: DoubleEndedSearcher<'a> {
661
667
}
662
668
663
669
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
664
- impl < ' a , P : Pattern < ' a > > Iterator for CharSplitsN < ' a , P >
665
- where P :: Searcher : DoubleEndedSearcher < ' a > {
670
+ impl < ' a , P : Pattern < ' a > > Iterator for CharSplitsN < ' a , P > {
666
671
type Item = & ' a str ;
667
672
668
673
#[ inline]
669
674
fn next ( & mut self ) -> Option < & ' a str > {
670
675
if self . count != 0 {
671
676
self . count -= 1 ;
672
- if self . invert { self . iter . next_back ( ) } else { self . iter . next ( ) }
677
+ self . iter . next ( )
673
678
} else {
674
679
self . iter . get_end ( )
675
680
}
@@ -713,6 +718,23 @@ impl<'a, P: Pattern<'a>> Iterator for RCharSplits<'a, P>
713
718
}
714
719
}
715
720
721
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
722
+ impl < ' a , P : Pattern < ' a > > Iterator for RCharSplitsN < ' a , P >
723
+ where P :: Searcher : ReverseSearcher < ' a >
724
+ {
725
+ type Item = & ' a str ;
726
+
727
+ #[ inline]
728
+ fn next ( & mut self ) -> Option < & ' a str > {
729
+ if self . count != 0 {
730
+ self . count -= 1 ;
731
+ self . iter . next ( )
732
+ } else {
733
+ self . iter . get_remainder ( )
734
+ }
735
+ }
736
+ }
737
+
716
738
/// The internal state of an iterator that searches for matches of a substring
717
739
/// within a larger string using two-way search
718
740
#[ derive( Clone ) ]
@@ -1360,23 +1382,7 @@ impl<'a, S: ?Sized> Str for &'a S where S: Str {
1360
1382
/// Return type of `StrExt::split`
1361
1383
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1362
1384
pub struct Split < ' a , P : Pattern < ' a > > ( CharSplits < ' a , P > ) ;
1363
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1364
- impl < ' a , P : Pattern < ' a > > Iterator for Split < ' a , P > {
1365
- type Item = & ' a str ;
1366
-
1367
- #[ inline]
1368
- fn next ( & mut self ) -> Option < & ' a str > {
1369
- self . 0 . next ( )
1370
- }
1371
- }
1372
- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1373
- impl < ' a , P : Pattern < ' a > > DoubleEndedIterator for Split < ' a , P >
1374
- where P :: Searcher : DoubleEndedSearcher < ' a > {
1375
- #[ inline]
1376
- fn next_back ( & mut self ) -> Option < & ' a str > {
1377
- self . 0 . next_back ( )
1378
- }
1379
- }
1385
+ delegate_iter ! { pattern & ' a str : Split <' a, P >}
1380
1386
1381
1387
/// Return type of `StrExt::split_terminator`
1382
1388
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1395,8 +1401,8 @@ delegate_iter!{pattern reverse &'a str : RSplit<'a, P>}
1395
1401
1396
1402
/// Return type of `StrExt::rsplitn`
1397
1403
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1398
- pub struct RSplitN < ' a , P : Pattern < ' a > > ( CharSplitsN < ' a , P > ) ;
1399
- delegate_iter ! { pattern forward & ' a str : RSplitN <' a, P >}
1404
+ pub struct RSplitN < ' a , P : Pattern < ' a > > ( RCharSplitsN < ' a , P > ) ;
1405
+ delegate_iter ! { pattern reverse & ' a str : RSplitN <' a, P >}
1400
1406
1401
1407
/// Methods for string slices
1402
1408
#[ allow( missing_docs) ]
@@ -1414,7 +1420,8 @@ pub trait StrExt {
1414
1420
fn split_terminator < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> SplitTerminator < ' a , P > ;
1415
1421
fn rsplit < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> RSplit < ' a , P >
1416
1422
where P :: Searcher : ReverseSearcher < ' a > ;
1417
- fn rsplitn < ' a , P : Pattern < ' a > > ( & ' a self , count : usize , pat : P ) -> RSplitN < ' a , P > ;
1423
+ fn rsplitn < ' a , P : Pattern < ' a > > ( & ' a self , count : usize , pat : P ) -> RSplitN < ' a , P >
1424
+ where P :: Searcher : ReverseSearcher < ' a > ;
1418
1425
fn match_indices < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> MatchIndices < ' a , P > ;
1419
1426
#[ allow( deprecated) /* for SplitStr */ ]
1420
1427
fn split_str < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> SplitStr < ' a , P > ;
@@ -1498,7 +1505,6 @@ impl StrExt for str {
1498
1505
SplitN ( CharSplitsN {
1499
1506
iter : self . split ( pat) . 0 ,
1500
1507
count : count,
1501
- invert : false ,
1502
1508
} )
1503
1509
}
1504
1510
@@ -1524,11 +1530,12 @@ impl StrExt for str {
1524
1530
}
1525
1531
1526
1532
#[ inline]
1527
- fn rsplitn < ' a , P : Pattern < ' a > > ( & ' a self , count : usize , pat : P ) -> RSplitN < ' a , P > {
1528
- RSplitN ( CharSplitsN {
1529
- iter : self . split ( pat) . 0 ,
1533
+ fn rsplitn < ' a , P : Pattern < ' a > > ( & ' a self , count : usize , pat : P ) -> RSplitN < ' a , P >
1534
+ where P :: Searcher : ReverseSearcher < ' a >
1535
+ {
1536
+ RSplitN ( RCharSplitsN {
1537
+ iter : self . rsplit ( pat) . 0 ,
1530
1538
count : count,
1531
- invert : true ,
1532
1539
} )
1533
1540
}
1534
1541
0 commit comments