Skip to content

Commit 57f4d7a

Browse files
Jules BertholetJules-Bertholet
Jules Bertholet
authored andcommitted
Implement split_initiator, split_ends, and split_once variants for string slices
1 parent a76b09c commit 57f4d7a

File tree

4 files changed

+372
-11
lines changed

4 files changed

+372
-11
lines changed

library/alloc/src/str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub use core::str::{Matches, RMatches};
6969
pub use core::str::{RSplit, Split};
7070
#[unstable(feature = "split_inclusive_variants", issue = "none")]
7171
pub use core::str::{
72-
RSplitInclusive, RSplitLeftInclusive, RSplitNInclusive, RSplitNLeftInclusive,
73-
SplitLeftInclusive, SplitNInclusive, SplitNLeftInclusive,
72+
RSplitInclusive, RSplitLeftInclusive, RSplitNInclusive, RSplitNLeftInclusive, SplitEnds,
73+
SplitInitiator, SplitLeftInclusive, SplitNInclusive, SplitNLeftInclusive,
7474
};
7575
#[stable(feature = "rust1", since = "1.0.0")]
7676
pub use core::str::{RSplitN, SplitN};

library/core/src/slice/iter/macros.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,7 @@ macro_rules! iter_n {
776776
#[$stability]
777777
impl<'a, T: 'a, P> $clone for $iter_n<'a, T, P>
778778
where
779-
P: FnMut(&T) -> bool,
780-
$inner<'a, T, P>: Clone,
779+
P: Clone + FnMut(&T) -> bool,
781780
{
782781
fn clone(&self) -> Self {
783782
Self { inner: self.inner.clone() }
@@ -788,7 +787,7 @@ macro_rules! iter_n {
788787
#[$stability]
789788
impl<T: fmt::Debug, P> fmt::Debug for $iter_n<'_, T, P>
790789
where
791-
P: Clone + FnMut(&T) -> bool,
790+
P: FnMut(&T) -> bool,
792791
{
793792
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
794793
f.debug_struct($str).field("inner", &self.inner).finish()

library/core/src/str/iter.rs

+59
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,35 @@ generate_pattern_iterators! {
871871
delegate double ended;
872872
}
873873

874+
split_internal! {
875+
SplitInitiatorInternal {
876+
debug: "SplitInitiatorInternal",
877+
skip_leading_empty: skip_leading_empty,
878+
include_leading: false,
879+
include_trailing: false,
880+
}
881+
}
882+
883+
generate_pattern_iterators! {
884+
forward:
885+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
886+
#[fused(unstable(feature = "split_inclusive_variants", issue = "none"))]
887+
/// Created with the method [`split_initiator`].
888+
///
889+
/// [`split_initiator`]: str::split_initiator
890+
struct SplitInitiator;
891+
reverse:
892+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
893+
#[fused(unstable(feature = "split_inclusive_variants", issue = "none"))]
894+
/// Created with the method [`rsplit_initiator`].
895+
///
896+
/// [`rsplit_initiator`]: str::rsplit_initiator
897+
struct RSplitInitiator;
898+
internal:
899+
SplitInitiatorInternal yielding (&'a str);
900+
delegate double ended;
901+
}
902+
874903
split_internal! {
875904
SplitTerminatorInternal {
876905
debug: "SplitTerminatorInternal",
@@ -900,6 +929,36 @@ generate_pattern_iterators! {
900929
delegate double ended;
901930
}
902931

932+
split_internal! {
933+
SplitEndsInternal {
934+
debug: "SplitEndsInternal",
935+
skip_leading_empty: skip_leading_empty,
936+
skip_trailing_empty: skip_trailing_empty,
937+
include_leading: false,
938+
include_trailing: false,
939+
}
940+
}
941+
942+
generate_pattern_iterators! {
943+
forward:
944+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
945+
#[fused(unstable(feature = "split_inclusive_variants", issue = "none"))]
946+
/// Created with the method [`split_ends`].
947+
///
948+
/// [`split_ends`]: str::split_ends
949+
struct SplitEnds;
950+
reverse:
951+
#[unstable(feature = "split_inclusive_variants", issue = "none")]
952+
#[fused(unstable(feature = "split_inclusive_variants", issue = "none"))]
953+
/// Created with the method [`rsplit_ends`].
954+
///
955+
/// [`rsplit_ends`]: str::rsplit_ends
956+
struct RSplitEnds;
957+
internal:
958+
SplitEndsInternal yielding (&'a str);
959+
delegate double ended;
960+
}
961+
903962
impl<'a, P: Pattern<'a>> SplitTerminator<'a, P> {
904963
/// Returns remainder of the split string
905964
///

0 commit comments

Comments
 (0)