@@ -585,16 +585,17 @@ where
585
585
impl < ' a , P : Pattern < ' a > > SplitInternal < ' a , P > {
586
586
#[ inline]
587
587
fn get_end ( & mut self ) -> Option < & ' a str > {
588
- if !self . finished && ( self . allow_trailing_empty || self . end - self . start > 0 ) {
588
+ if !self . finished {
589
589
self . finished = true ;
590
- // SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
591
- unsafe {
592
- let string = self . matcher . haystack ( ) . get_unchecked ( self . start ..self . end ) ;
593
- Some ( string)
590
+
591
+ if self . allow_trailing_empty || self . end - self . start > 0 {
592
+ // SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
593
+ let string = unsafe { self . matcher . haystack ( ) . get_unchecked ( self . start ..self . end ) } ;
594
+ return Some ( string) ;
594
595
}
595
- } else {
596
- None
597
596
}
597
+
598
+ None
598
599
}
599
600
600
601
#[ inline]
@@ -716,14 +717,14 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
716
717
}
717
718
718
719
#[ inline]
719
- fn as_str ( & self ) -> & ' a str {
720
+ fn remainder ( & self ) -> Option < & ' a str > {
720
721
// `Self::get_end` doesn't change `self.start`
721
722
if self . finished {
722
- return "" ;
723
+ return None ;
723
724
}
724
725
725
726
// SAFETY: `self.start` and `self.end` always lie on unicode boundaries.
726
- unsafe { self . matcher . haystack ( ) . get_unchecked ( self . start ..self . end ) }
727
+ Some ( unsafe { self . matcher . haystack ( ) . get_unchecked ( self . start ..self . end ) } )
727
728
}
728
729
}
729
730
@@ -746,44 +747,48 @@ generate_pattern_iterators! {
746
747
}
747
748
748
749
impl < ' a , P : Pattern < ' a > > Split < ' a , P > {
749
- /// Returns remainder of the split string
750
+ /// Returns remainder of the split string.
751
+ ///
752
+ /// If the iterator is empty, returns `None`.
750
753
///
751
754
/// # Examples
752
755
///
753
756
/// ```
754
- /// #![feature(str_split_as_str )]
757
+ /// #![feature(str_split_remainder )]
755
758
/// let mut split = "Mary had a little lamb".split(' ');
756
- /// assert_eq!(split.as_str (), "Mary had a little lamb");
759
+ /// assert_eq!(split.remainder (), Some( "Mary had a little lamb") );
757
760
/// split.next();
758
- /// assert_eq!(split.as_str (), "had a little lamb");
761
+ /// assert_eq!(split.remainder (), Some( "had a little lamb") );
759
762
/// split.by_ref().for_each(drop);
760
- /// assert_eq!(split.as_str (), "" );
763
+ /// assert_eq!(split.remainder (), None );
761
764
/// ```
762
765
#[ inline]
763
- #[ unstable( feature = "str_split_as_str " , issue = "77998" ) ]
764
- pub fn as_str ( & self ) -> & ' a str {
765
- self . 0 . as_str ( )
766
+ #[ unstable( feature = "str_split_remainder " , issue = "77998" ) ]
767
+ pub fn remainder ( & self ) -> Option < & ' a str > {
768
+ self . 0 . remainder ( )
766
769
}
767
770
}
768
771
769
772
impl < ' a , P : Pattern < ' a > > RSplit < ' a , P > {
770
- /// Returns remainder of the split string
773
+ /// Returns remainder of the split string.
774
+ ///
775
+ /// If the iterator is empty, returns `None`.
771
776
///
772
777
/// # Examples
773
778
///
774
779
/// ```
775
- /// #![feature(str_split_as_str )]
780
+ /// #![feature(str_split_remainder )]
776
781
/// let mut split = "Mary had a little lamb".rsplit(' ');
777
- /// assert_eq!(split.as_str (), "Mary had a little lamb");
782
+ /// assert_eq!(split.remainder (), Some( "Mary had a little lamb") );
778
783
/// split.next();
779
- /// assert_eq!(split.as_str (), "Mary had a little");
784
+ /// assert_eq!(split.remainder (), Some( "Mary had a little") );
780
785
/// split.by_ref().for_each(drop);
781
- /// assert_eq!(split.as_str (), "" );
786
+ /// assert_eq!(split.remainder (), None );
782
787
/// ```
783
788
#[ inline]
784
- #[ unstable( feature = "str_split_as_str " , issue = "77998" ) ]
785
- pub fn as_str ( & self ) -> & ' a str {
786
- self . 0 . as_str ( )
789
+ #[ unstable( feature = "str_split_remainder " , issue = "77998" ) ]
790
+ pub fn remainder ( & self ) -> Option < & ' a str > {
791
+ self . 0 . remainder ( )
787
792
}
788
793
}
789
794
@@ -806,44 +811,48 @@ generate_pattern_iterators! {
806
811
}
807
812
808
813
impl < ' a , P : Pattern < ' a > > SplitTerminator < ' a , P > {
809
- /// Returns remainder of the split string
814
+ /// Returns remainder of the split string.
815
+ ///
816
+ /// If the iterator is empty, returns `None`.
810
817
///
811
818
/// # Examples
812
819
///
813
820
/// ```
814
- /// #![feature(str_split_as_str )]
821
+ /// #![feature(str_split_remainder )]
815
822
/// let mut split = "A..B..".split_terminator('.');
816
- /// assert_eq!(split.as_str (), "A..B..");
823
+ /// assert_eq!(split.remainder (), Some( "A..B..") );
817
824
/// split.next();
818
- /// assert_eq!(split.as_str (), ".B..");
825
+ /// assert_eq!(split.remainder (), Some( ".B..") );
819
826
/// split.by_ref().for_each(drop);
820
- /// assert_eq!(split.as_str (), "" );
827
+ /// assert_eq!(split.remainder (), None );
821
828
/// ```
822
829
#[ inline]
823
- #[ unstable( feature = "str_split_as_str " , issue = "77998" ) ]
824
- pub fn as_str ( & self ) -> & ' a str {
825
- self . 0 . as_str ( )
830
+ #[ unstable( feature = "str_split_remainder " , issue = "77998" ) ]
831
+ pub fn remainder ( & self ) -> Option < & ' a str > {
832
+ self . 0 . remainder ( )
826
833
}
827
834
}
828
835
829
836
impl < ' a , P : Pattern < ' a > > RSplitTerminator < ' a , P > {
830
- /// Returns remainder of the split string
837
+ /// Returns remainder of the split string.
838
+ ///
839
+ /// If the iterator is empty, returns `None`.
831
840
///
832
841
/// # Examples
833
842
///
834
843
/// ```
835
- /// #![feature(str_split_as_str )]
844
+ /// #![feature(str_split_remainder )]
836
845
/// let mut split = "A..B..".rsplit_terminator('.');
837
- /// assert_eq!(split.as_str (), "A..B..");
846
+ /// assert_eq!(split.remainder (), Some( "A..B..") );
838
847
/// split.next();
839
- /// assert_eq!(split.as_str (), "A..B");
848
+ /// assert_eq!(split.remainder (), Some( "A..B") );
840
849
/// split.by_ref().for_each(drop);
841
- /// assert_eq!(split.as_str (), "" );
850
+ /// assert_eq!(split.remainder (), None );
842
851
/// ```
843
852
#[ inline]
844
- #[ unstable( feature = "str_split_as_str " , issue = "77998" ) ]
845
- pub fn as_str ( & self ) -> & ' a str {
846
- self . 0 . as_str ( )
853
+ #[ unstable( feature = "str_split_remainder " , issue = "77998" ) ]
854
+ pub fn remainder ( & self ) -> Option < & ' a str > {
855
+ self . 0 . remainder ( )
847
856
}
848
857
}
849
858
@@ -905,8 +914,8 @@ impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
905
914
}
906
915
907
916
#[ inline]
908
- fn as_str ( & self ) -> & ' a str {
909
- self . iter . as_str ( )
917
+ fn remainder ( & self ) -> Option < & ' a str > {
918
+ self . iter . remainder ( )
910
919
}
911
920
}
912
921
@@ -929,44 +938,48 @@ generate_pattern_iterators! {
929
938
}
930
939
931
940
impl < ' a , P : Pattern < ' a > > SplitN < ' a , P > {
932
- /// Returns remainder of the split string
941
+ /// Returns remainder of the split string.
942
+ ///
943
+ /// If the iterator is empty, returns `None`.
933
944
///
934
945
/// # Examples
935
946
///
936
947
/// ```
937
- /// #![feature(str_split_as_str )]
948
+ /// #![feature(str_split_remainder )]
938
949
/// let mut split = "Mary had a little lamb".splitn(3, ' ');
939
- /// assert_eq!(split.as_str (), "Mary had a little lamb");
950
+ /// assert_eq!(split.remainder (), Some( "Mary had a little lamb") );
940
951
/// split.next();
941
- /// assert_eq!(split.as_str (), "had a little lamb");
952
+ /// assert_eq!(split.remainder (), Some( "had a little lamb") );
942
953
/// split.by_ref().for_each(drop);
943
- /// assert_eq!(split.as_str (), "" );
954
+ /// assert_eq!(split.remainder (), None );
944
955
/// ```
945
956
#[ inline]
946
- #[ unstable( feature = "str_split_as_str " , issue = "77998" ) ]
947
- pub fn as_str ( & self ) -> & ' a str {
948
- self . 0 . as_str ( )
957
+ #[ unstable( feature = "str_split_remainder " , issue = "77998" ) ]
958
+ pub fn remainder ( & self ) -> Option < & ' a str > {
959
+ self . 0 . remainder ( )
949
960
}
950
961
}
951
962
952
963
impl < ' a , P : Pattern < ' a > > RSplitN < ' a , P > {
953
- /// Returns remainder of the split string
964
+ /// Returns remainder of the split string.
965
+ ///
966
+ /// If the iterator is empty, returns `None`.
954
967
///
955
968
/// # Examples
956
969
///
957
970
/// ```
958
- /// #![feature(str_split_as_str )]
971
+ /// #![feature(str_split_remainder )]
959
972
/// let mut split = "Mary had a little lamb".rsplitn(3, ' ');
960
- /// assert_eq!(split.as_str (), "Mary had a little lamb");
973
+ /// assert_eq!(split.remainder (), Some( "Mary had a little lamb") );
961
974
/// split.next();
962
- /// assert_eq!(split.as_str (), "Mary had a little");
975
+ /// assert_eq!(split.remainder (), Some( "Mary had a little") );
963
976
/// split.by_ref().for_each(drop);
964
- /// assert_eq!(split.as_str (), "" );
977
+ /// assert_eq!(split.remainder (), None );
965
978
/// ```
966
979
#[ inline]
967
- #[ unstable( feature = "str_split_as_str " , issue = "77998" ) ]
968
- pub fn as_str ( & self ) -> & ' a str {
969
- self . 0 . as_str ( )
980
+ #[ unstable( feature = "str_split_remainder " , issue = "77998" ) ]
981
+ pub fn remainder ( & self ) -> Option < & ' a str > {
982
+ self . 0 . remainder ( )
970
983
}
971
984
}
972
985
@@ -1239,22 +1252,22 @@ impl<'a> SplitWhitespace<'a> {
1239
1252
/// # Examples
1240
1253
///
1241
1254
/// ```
1242
- /// #![feature(str_split_whitespace_as_str )]
1255
+ /// #![feature(str_split_whitespace_remainder )]
1243
1256
///
1244
1257
/// let mut split = "Mary had a little lamb".split_whitespace();
1245
- /// assert_eq!(split.as_str (), "Mary had a little lamb");
1258
+ /// assert_eq!(split.remainder (), Some( "Mary had a little lamb") );
1246
1259
///
1247
1260
/// split.next();
1248
- /// assert_eq!(split.as_str (), "had a little lamb");
1261
+ /// assert_eq!(split.remainder (), Some( "had a little lamb") );
1249
1262
///
1250
1263
/// split.by_ref().for_each(drop);
1251
- /// assert_eq!(split.as_str (), "" );
1264
+ /// assert_eq!(split.remainder (), None );
1252
1265
/// ```
1253
1266
#[ inline]
1254
1267
#[ must_use]
1255
- #[ unstable( feature = "str_split_whitespace_as_str " , issue = "77998" ) ]
1256
- pub fn as_str ( & self ) -> & ' a str {
1257
- self . inner . iter . as_str ( )
1268
+ #[ unstable( feature = "str_split_whitespace_remainder " , issue = "77998" ) ]
1269
+ pub fn remainder ( & self ) -> Option < & ' a str > {
1270
+ self . inner . iter . remainder ( )
1258
1271
}
1259
1272
}
1260
1273
@@ -1290,32 +1303,34 @@ impl<'a> DoubleEndedIterator for SplitAsciiWhitespace<'a> {
1290
1303
impl FusedIterator for SplitAsciiWhitespace < ' _ > { }
1291
1304
1292
1305
impl < ' a > SplitAsciiWhitespace < ' a > {
1293
- /// Returns remainder of the split string
1306
+ /// Returns remainder of the split string.
1307
+ ///
1308
+ /// If the iterator is empty, returns `None`.
1294
1309
///
1295
1310
/// # Examples
1296
1311
///
1297
1312
/// ```
1298
- /// #![feature(str_split_whitespace_as_str )]
1313
+ /// #![feature(str_split_whitespace_remainder )]
1299
1314
///
1300
1315
/// let mut split = "Mary had a little lamb".split_ascii_whitespace();
1301
- /// assert_eq!(split.as_str (), "Mary had a little lamb");
1316
+ /// assert_eq!(split.remainder (), Some( "Mary had a little lamb") );
1302
1317
///
1303
1318
/// split.next();
1304
- /// assert_eq!(split.as_str (), "had a little lamb");
1319
+ /// assert_eq!(split.remainder (), Some( "had a little lamb") );
1305
1320
///
1306
1321
/// split.by_ref().for_each(drop);
1307
- /// assert_eq!(split.as_str (), "" );
1322
+ /// assert_eq!(split.remainder (), None );
1308
1323
/// ```
1309
1324
#[ inline]
1310
1325
#[ must_use]
1311
- #[ unstable( feature = "str_split_whitespace_as_str " , issue = "77998" ) ]
1312
- pub fn as_str ( & self ) -> & ' a str {
1326
+ #[ unstable( feature = "str_split_whitespace_remainder " , issue = "77998" ) ]
1327
+ pub fn remainder ( & self ) -> Option < & ' a str > {
1313
1328
if self . inner . iter . iter . finished {
1314
- return "" ;
1329
+ return None ;
1315
1330
}
1316
1331
1317
1332
// SAFETY: Slice is created from str.
1318
- unsafe { crate :: str:: from_utf8_unchecked ( & self . inner . iter . iter . v ) }
1333
+ Some ( unsafe { crate :: str:: from_utf8_unchecked ( & self . inner . iter . iter . v ) } )
1319
1334
}
1320
1335
}
1321
1336
@@ -1358,23 +1373,25 @@ impl<'a, P: Pattern<'a, Searcher: ReverseSearcher<'a>>> DoubleEndedIterator
1358
1373
impl < ' a , P : Pattern < ' a > > FusedIterator for SplitInclusive < ' a , P > { }
1359
1374
1360
1375
impl < ' a , P : Pattern < ' a > > SplitInclusive < ' a , P > {
1361
- /// Returns remainder of the split string
1376
+ /// Returns remainder of the split string.
1377
+ ///
1378
+ /// If the iterator is empty, returns `None`.
1362
1379
///
1363
1380
/// # Examples
1364
1381
///
1365
1382
/// ```
1366
- /// #![feature(str_split_inclusive_as_str )]
1383
+ /// #![feature(str_split_inclusive_remainder )]
1367
1384
/// let mut split = "Mary had a little lamb".split_inclusive(' ');
1368
- /// assert_eq!(split.as_str (), "Mary had a little lamb");
1385
+ /// assert_eq!(split.remainder (), Some( "Mary had a little lamb") );
1369
1386
/// split.next();
1370
- /// assert_eq!(split.as_str (), "had a little lamb");
1387
+ /// assert_eq!(split.remainder (), Some( "had a little lamb") );
1371
1388
/// split.by_ref().for_each(drop);
1372
- /// assert_eq!(split.as_str (), "" );
1389
+ /// assert_eq!(split.remainder (), None );
1373
1390
/// ```
1374
1391
#[ inline]
1375
- #[ unstable( feature = "str_split_inclusive_as_str " , issue = "77998" ) ]
1376
- pub fn as_str ( & self ) -> & ' a str {
1377
- self . 0 . as_str ( )
1392
+ #[ unstable( feature = "str_split_inclusive_remainder " , issue = "77998" ) ]
1393
+ pub fn remainder ( & self ) -> Option < & ' a str > {
1394
+ self . 0 . remainder ( )
1378
1395
}
1379
1396
}
1380
1397
0 commit comments