Skip to content

Commit b458a49

Browse files
committed
Replace Split*::as_str with remainder
This commit - Renames `Split*::{as_str -> remainder}` as it seems less confusing - Makes `remainder` return Option<&str> to distinguish between "iterator is exhausted" and "the tail is empty"
1 parent ca4989e commit b458a49

File tree

2 files changed

+96
-77
lines changed

2 files changed

+96
-77
lines changed

library/core/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
#![feature(const_unsafecell_get_mut)]
159159
#![feature(const_waker)]
160160
#![feature(core_panic)]
161+
#![feature(char_indices_offset)]
161162
#![feature(duration_consts_float)]
162163
#![feature(maybe_uninit_uninit_array)]
163164
#![feature(ptr_alignment_type)]
@@ -166,6 +167,8 @@
166167
#![feature(slice_ptr_get)]
167168
#![feature(slice_split_at_unchecked)]
168169
#![feature(str_internals)]
170+
#![feature(str_split_remainder)]
171+
#![feature(str_split_inclusive_remainder)]
169172
#![feature(strict_provenance)]
170173
#![feature(utf16_extra)]
171174
#![feature(utf16_extra_const)]

library/core/src/str/iter.rs

+93-77
Original file line numberDiff line numberDiff line change
@@ -717,14 +717,14 @@ impl<'a, P: Pattern<'a>> SplitInternal<'a, P> {
717717
}
718718

719719
#[inline]
720-
fn as_str(&self) -> &'a str {
720+
fn remainder(&self) -> Option<&'a str> {
721721
// `Self::get_end` doesn't change `self.start`
722722
if self.finished {
723-
return "";
723+
return None;
724724
}
725725

726726
// 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) })
728728
}
729729
}
730730

@@ -747,44 +747,48 @@ generate_pattern_iterators! {
747747
}
748748

749749
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`.
751753
///
752754
/// # Examples
753755
///
754756
/// ```
755-
/// #![feature(str_split_as_str)]
757+
/// #![feature(str_split_remainder)]
756758
/// 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"));
758760
/// split.next();
759-
/// assert_eq!(split.as_str(), "had a little lamb");
761+
/// assert_eq!(split.remainder(), Some("had a little lamb"));
760762
/// split.by_ref().for_each(drop);
761-
/// assert_eq!(split.as_str(), "");
763+
/// assert_eq!(split.remainder(), None);
762764
/// ```
763765
#[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()
767769
}
768770
}
769771

770772
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`.
772776
///
773777
/// # Examples
774778
///
775779
/// ```
776-
/// #![feature(str_split_as_str)]
780+
/// #![feature(str_split_remainder)]
777781
/// 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"));
779783
/// split.next();
780-
/// assert_eq!(split.as_str(), "Mary had a little");
784+
/// assert_eq!(split.remainder(), Some("Mary had a little"));
781785
/// split.by_ref().for_each(drop);
782-
/// assert_eq!(split.as_str(), "");
786+
/// assert_eq!(split.remainder(), None);
783787
/// ```
784788
#[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()
788792
}
789793
}
790794

@@ -807,44 +811,48 @@ generate_pattern_iterators! {
807811
}
808812

809813
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`.
811817
///
812818
/// # Examples
813819
///
814820
/// ```
815-
/// #![feature(str_split_as_str)]
821+
/// #![feature(str_split_remainder)]
816822
/// let mut split = "A..B..".split_terminator('.');
817-
/// assert_eq!(split.as_str(), "A..B..");
823+
/// assert_eq!(split.remainder(), Some("A..B.."));
818824
/// split.next();
819-
/// assert_eq!(split.as_str(), ".B..");
825+
/// assert_eq!(split.remainder(), Some(".B.."));
820826
/// split.by_ref().for_each(drop);
821-
/// assert_eq!(split.as_str(), "");
827+
/// assert_eq!(split.remainder(), None);
822828
/// ```
823829
#[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()
827833
}
828834
}
829835

830836
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`.
832840
///
833841
/// # Examples
834842
///
835843
/// ```
836-
/// #![feature(str_split_as_str)]
844+
/// #![feature(str_split_remainder)]
837845
/// let mut split = "A..B..".rsplit_terminator('.');
838-
/// assert_eq!(split.as_str(), "A..B..");
846+
/// assert_eq!(split.remainder(), Some("A..B.."));
839847
/// split.next();
840-
/// assert_eq!(split.as_str(), "A..B");
848+
/// assert_eq!(split.remainder(), Some("A..B"));
841849
/// split.by_ref().for_each(drop);
842-
/// assert_eq!(split.as_str(), "");
850+
/// assert_eq!(split.remainder(), None);
843851
/// ```
844852
#[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()
848856
}
849857
}
850858

@@ -906,8 +914,8 @@ impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
906914
}
907915

908916
#[inline]
909-
fn as_str(&self) -> &'a str {
910-
self.iter.as_str()
917+
fn remainder(&self) -> Option<&'a str> {
918+
self.iter.remainder()
911919
}
912920
}
913921

@@ -930,44 +938,48 @@ generate_pattern_iterators! {
930938
}
931939

932940
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`.
934944
///
935945
/// # Examples
936946
///
937947
/// ```
938-
/// #![feature(str_split_as_str)]
948+
/// #![feature(str_split_remainder)]
939949
/// 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"));
941951
/// split.next();
942-
/// assert_eq!(split.as_str(), "had a little lamb");
952+
/// assert_eq!(split.remainder(), Some("had a little lamb"));
943953
/// split.by_ref().for_each(drop);
944-
/// assert_eq!(split.as_str(), "");
954+
/// assert_eq!(split.remainder(), None);
945955
/// ```
946956
#[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()
950960
}
951961
}
952962

953963
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`.
955967
///
956968
/// # Examples
957969
///
958970
/// ```
959-
/// #![feature(str_split_as_str)]
971+
/// #![feature(str_split_remainder)]
960972
/// 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"));
962974
/// split.next();
963-
/// assert_eq!(split.as_str(), "Mary had a little");
975+
/// assert_eq!(split.remainder(), Some("Mary had a little"));
964976
/// split.by_ref().for_each(drop);
965-
/// assert_eq!(split.as_str(), "");
977+
/// assert_eq!(split.remainder(), None);
966978
/// ```
967979
#[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()
971983
}
972984
}
973985

@@ -1240,22 +1252,22 @@ impl<'a> SplitWhitespace<'a> {
12401252
/// # Examples
12411253
///
12421254
/// ```
1243-
/// #![feature(str_split_whitespace_as_str)]
1255+
/// #![feature(str_split_whitespace_remainder)]
12441256
///
12451257
/// 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"));
12471259
///
12481260
/// split.next();
1249-
/// assert_eq!(split.as_str(), "had a little lamb");
1261+
/// assert_eq!(split.remainder(), Some("had a little lamb"));
12501262
///
12511263
/// split.by_ref().for_each(drop);
1252-
/// assert_eq!(split.as_str(), "");
1264+
/// assert_eq!(split.remainder(), None);
12531265
/// ```
12541266
#[inline]
12551267
#[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()
12591271
}
12601272
}
12611273

@@ -1291,32 +1303,34 @@ impl<'a> DoubleEndedIterator for SplitAsciiWhitespace<'a> {
12911303
impl FusedIterator for SplitAsciiWhitespace<'_> {}
12921304

12931305
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`.
12951309
///
12961310
/// # Examples
12971311
///
12981312
/// ```
1299-
/// #![feature(str_split_whitespace_as_str)]
1313+
/// #![feature(str_split_whitespace_remainder)]
13001314
///
13011315
/// 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"));
13031317
///
13041318
/// split.next();
1305-
/// assert_eq!(split.as_str(), "had a little lamb");
1319+
/// assert_eq!(split.remainder(), Some("had a little lamb"));
13061320
///
13071321
/// split.by_ref().for_each(drop);
1308-
/// assert_eq!(split.as_str(), "");
1322+
/// assert_eq!(split.remainder(), None);
13091323
/// ```
13101324
#[inline]
13111325
#[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> {
13141328
if self.inner.iter.iter.finished {
1315-
return "";
1329+
return None;
13161330
}
13171331

13181332
// 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) })
13201334
}
13211335
}
13221336

@@ -1359,23 +1373,25 @@ impl<'a, P: Pattern<'a, Searcher: ReverseSearcher<'a>>> DoubleEndedIterator
13591373
impl<'a, P: Pattern<'a>> FusedIterator for SplitInclusive<'a, P> {}
13601374

13611375
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`.
13631379
///
13641380
/// # Examples
13651381
///
13661382
/// ```
1367-
/// #![feature(str_split_inclusive_as_str)]
1383+
/// #![feature(str_split_inclusive_remainder)]
13681384
/// 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"));
13701386
/// split.next();
1371-
/// assert_eq!(split.as_str(), "had a little lamb");
1387+
/// assert_eq!(split.remainder(), Some("had a little lamb"));
13721388
/// split.by_ref().for_each(drop);
1373-
/// assert_eq!(split.as_str(), "");
1389+
/// assert_eq!(split.remainder(), None);
13741390
/// ```
13751391
#[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()
13791395
}
13801396
}
13811397

0 commit comments

Comments
 (0)