File tree 3 files changed +60
-0
lines changed
3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -779,6 +779,33 @@ impl str {
779
779
core_str:: StrExt :: slice_shift_char ( & self [ ..] )
780
780
}
781
781
782
+ /// Divide one string slice into two at an index.
783
+ ///
784
+ /// The index `mid` is a byte offset from the start of the string
785
+ /// that must be on a character boundary.
786
+ ///
787
+ /// Return slices `&self[..mid]` and `&self[mid..]`.
788
+ ///
789
+ /// # Panics
790
+ ///
791
+ /// Panics if `mid` is beyond the last character of the string,
792
+ /// or if it is not on a character boundary.
793
+ ///
794
+ /// # Examples
795
+ /// ```
796
+ /// # #![feature(collections)]
797
+ /// let s = "Löwe 老虎 Léopard";
798
+ /// let first_space = s.find(' ').unwrap_or(s.len());
799
+ /// let (a, b) = s.split_at(first_space);
800
+ ///
801
+ /// assert_eq!(a, "Löwe");
802
+ /// assert_eq!(b, " 老虎 Léopard");
803
+ /// ```
804
+ #[ inline]
805
+ pub fn split_at ( & self , mid : usize ) -> ( & str , & str ) {
806
+ core_str:: StrExt :: split_at ( self , mid)
807
+ }
808
+
782
809
/// An iterator over the codepoints of `self`.
783
810
///
784
811
/// # Examples
Original file line number Diff line number Diff line change @@ -688,6 +688,26 @@ fn test_char_at_reverse() {
688
688
}
689
689
}
690
690
691
+ #[ test]
692
+ fn test_split_at ( ) {
693
+ let s = "ศไทย中华Việt Nam" ;
694
+ for ( index, _) in s. char_indices ( ) {
695
+ let ( a, b) = s. split_at ( index) ;
696
+ assert_eq ! ( & s[ ..a. len( ) ] , a) ;
697
+ assert_eq ! ( & s[ a. len( ) ..] , b) ;
698
+ }
699
+ let ( a, b) = s. split_at ( s. len ( ) ) ;
700
+ assert_eq ! ( a, s) ;
701
+ assert_eq ! ( b, "" ) ;
702
+ }
703
+
704
+ #[ test]
705
+ #[ should_panic]
706
+ fn test_split_at_boundscheck ( ) {
707
+ let s = "ศไทย中华Việt Nam" ;
708
+ let ( a, b) = s. split_at ( 1 ) ;
709
+ }
710
+
691
711
#[ test]
692
712
fn test_escape_unicode ( ) {
693
713
assert_eq ! ( "abc" . escape_unicode( ) ,
Original file line number Diff line number Diff line change @@ -1517,6 +1517,7 @@ pub trait StrExt {
1517
1517
fn rfind < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> Option < usize >
1518
1518
where P :: Searcher : ReverseSearcher < ' a > ;
1519
1519
fn find_str < ' a , P : Pattern < ' a > > ( & ' a self , pat : P ) -> Option < usize > ;
1520
+ fn split_at ( & self , mid : usize ) -> ( & str , & str ) ;
1520
1521
fn slice_shift_char < ' a > ( & ' a self ) -> Option < ( char , & ' a str ) > ;
1521
1522
fn subslice_offset ( & self , inner : & str ) -> usize ;
1522
1523
fn as_ptr ( & self ) -> * const u8 ;
@@ -1809,6 +1810,18 @@ impl StrExt for str {
1809
1810
self . find ( pat)
1810
1811
}
1811
1812
1813
+ fn split_at ( & self , mid : usize ) -> ( & str , & str ) {
1814
+ // is_char_boundary checks that the index is in [0, .len()]
1815
+ if self . is_char_boundary ( mid) {
1816
+ unsafe {
1817
+ ( self . slice_unchecked ( 0 , mid) ,
1818
+ self . slice_unchecked ( mid, self . len ( ) ) )
1819
+ }
1820
+ } else {
1821
+ slice_error_fail ( self , 0 , mid)
1822
+ }
1823
+ }
1824
+
1812
1825
#[ inline]
1813
1826
fn slice_shift_char ( & self ) -> Option < ( char , & str ) > {
1814
1827
if self . is_empty ( ) {
You can’t perform that action at this time.
0 commit comments