Skip to content

Commit 4747215

Browse files
committed
add str::{SplitN, RSplitN, SplitTerminator, RSplitTerminator}::as_str methods
This commit entroduce 4 methods smililar to `Split::as_str` all under the same gate "str_split_as_str".
1 parent 0b923d3 commit 4747215

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

library/core/src/str/iter.rs

+89
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,48 @@ generate_pattern_iterators! {
783783
delegate double ended;
784784
}
785785

786+
impl<'a, P: Pattern<'a>> SplitTerminator<'a, P> {
787+
/// Returns remainder of the splitted string
788+
///
789+
/// # Examples
790+
///
791+
/// ```
792+
/// #![feature(str_split_as_str)]
793+
/// let mut split = "A..B..".split_terminator('.');
794+
/// assert_eq!(split.as_str(), "A..B..");
795+
/// split.next();
796+
/// assert_eq!(split.as_str(), ".B..");
797+
/// split.by_ref().for_each(drop);
798+
/// assert_eq!(split.as_str(), "");
799+
/// ```
800+
#[inline]
801+
#[unstable(feature = "str_split_as_str", issue = "none")]
802+
pub fn as_str(&self) -> &'a str {
803+
self.0.as_str()
804+
}
805+
}
806+
807+
impl<'a, P: Pattern<'a>> RSplitTerminator<'a, P> {
808+
/// Returns remainder of the splitted string
809+
///
810+
/// # Examples
811+
///
812+
/// ```
813+
/// #![feature(str_split_as_str)]
814+
/// let mut split = "A..B..".rsplit_terminator('.');
815+
/// assert_eq!(split.as_str(), "A..B..");
816+
/// split.next();
817+
/// assert_eq!(split.as_str(), "A..B");
818+
/// split.by_ref().for_each(drop);
819+
/// assert_eq!(split.as_str(), "");
820+
/// ```
821+
#[inline]
822+
#[unstable(feature = "str_split_as_str", issue = "none")]
823+
pub fn as_str(&self) -> &'a str {
824+
self.0.as_str()
825+
}
826+
}
827+
786828
derive_pattern_clone! {
787829
clone SplitNInternal
788830
with |s| SplitNInternal { iter: s.iter.clone(), ..*s }
@@ -839,6 +881,11 @@ impl<'a, P: Pattern<'a>> SplitNInternal<'a, P> {
839881
}
840882
}
841883
}
884+
885+
#[inline]
886+
fn as_str(&self) -> &'a str {
887+
self.iter.as_str()
888+
}
842889
}
843890

844891
generate_pattern_iterators! {
@@ -859,6 +906,48 @@ generate_pattern_iterators! {
859906
delegate single ended;
860907
}
861908

909+
impl<'a, P: Pattern<'a>> SplitN<'a, P> {
910+
/// Returns remainder of the splitted string
911+
///
912+
/// # Examples
913+
///
914+
/// ```
915+
/// #![feature(str_split_as_str)]
916+
/// let mut split = "Mary had a little lamb".splitn(3, ' ');
917+
/// assert_eq!(split.as_str(), "Mary had a little lamb");
918+
/// split.next();
919+
/// assert_eq!(split.as_str(), "had a little lamb");
920+
/// split.by_ref().for_each(drop);
921+
/// assert_eq!(split.as_str(), "");
922+
/// ```
923+
#[inline]
924+
#[unstable(feature = "str_split_as_str", issue = "none")]
925+
pub fn as_str(&self) -> &'a str {
926+
self.0.as_str()
927+
}
928+
}
929+
930+
impl<'a, P: Pattern<'a>> RSplitN<'a, P> {
931+
/// Returns remainder of the splitted string
932+
///
933+
/// # Examples
934+
///
935+
/// ```
936+
/// #![feature(str_split_as_str)]
937+
/// let mut split = "Mary had a little lamb".rsplitn(3, ' ');
938+
/// assert_eq!(split.as_str(), "Mary had a little lamb");
939+
/// split.next();
940+
/// assert_eq!(split.as_str(), "Mary had a little");
941+
/// split.by_ref().for_each(drop);
942+
/// assert_eq!(split.as_str(), "");
943+
/// ```
944+
#[inline]
945+
#[unstable(feature = "str_split_as_str", issue = "none")]
946+
pub fn as_str(&self) -> &'a str {
947+
self.0.as_str()
948+
}
949+
}
950+
862951
derive_pattern_clone! {
863952
clone MatchIndicesInternal
864953
with |s| MatchIndicesInternal(s.0.clone())

0 commit comments

Comments
 (0)