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