@@ -658,7 +658,8 @@ impl str {
658
658
#[ inline]
659
659
#[ must_use]
660
660
#[ stable( feature = "str_split_at" , since = "1.4.0" ) ]
661
- pub fn split_at ( & self , mid : usize ) -> ( & str , & str ) {
661
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "none" ) ]
662
+ pub const fn split_at ( & self , mid : usize ) -> ( & str , & str ) {
662
663
match self . split_at_checked ( mid) {
663
664
None => slice_error_fail ( self , 0 , mid) ,
664
665
Some ( pair) => pair,
@@ -698,7 +699,8 @@ impl str {
698
699
#[ inline]
699
700
#[ must_use]
700
701
#[ stable( feature = "str_split_at" , since = "1.4.0" ) ]
701
- pub fn split_at_mut ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
702
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "none" ) ]
703
+ pub const fn split_at_mut ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
702
704
// is_char_boundary checks that the index is in [0, .len()]
703
705
if self . is_char_boundary ( mid) {
704
706
// SAFETY: just checked that `mid` is on a char boundary.
@@ -737,11 +739,12 @@ impl str {
737
739
#[ inline]
738
740
#[ must_use]
739
741
#[ stable( feature = "split_at_checked" , since = "1.80.0" ) ]
740
- pub fn split_at_checked ( & self , mid : usize ) -> Option < ( & str , & str ) > {
742
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "none" ) ]
743
+ pub const fn split_at_checked ( & self , mid : usize ) -> Option < ( & str , & str ) > {
741
744
// is_char_boundary checks that the index is in [0, .len()]
742
745
if self . is_char_boundary ( mid) {
743
746
// SAFETY: just checked that `mid` is on a char boundary.
744
- Some ( unsafe { ( self . get_unchecked ( 0 .. mid) , self . get_unchecked ( mid.. self . len ( ) ) ) } )
747
+ Some ( unsafe { self . split_at_unchecked ( mid) } )
745
748
} else {
746
749
None
747
750
}
@@ -777,7 +780,9 @@ impl str {
777
780
#[ inline]
778
781
#[ must_use]
779
782
#[ stable( feature = "split_at_checked" , since = "1.80.0" ) ]
780
- pub fn split_at_mut_checked ( & mut self , mid : usize ) -> Option < ( & mut str , & mut str ) > {
783
+ #[ rustc_const_unstable( feature = "const_str_split_at" , issue = "none" ) ]
784
+ #[ rustc_allow_const_fn_unstable( const_is_char_boundary) ]
785
+ pub const fn split_at_mut_checked ( & mut self , mid : usize ) -> Option < ( & mut str , & mut str ) > {
781
786
// is_char_boundary checks that the index is in [0, .len()]
782
787
if self . is_char_boundary ( mid) {
783
788
// SAFETY: just checked that `mid` is on a char boundary.
@@ -793,7 +798,25 @@ impl str {
793
798
///
794
799
/// The caller must ensure that `mid` is a valid byte offset from the start
795
800
/// of the string and falls on the boundary of a UTF-8 code point.
796
- unsafe fn split_at_mut_unchecked ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
801
+ const unsafe fn split_at_unchecked ( & self , mid : usize ) -> ( & str , & str ) {
802
+ let len = self . len ( ) ;
803
+ let ptr = self . as_ptr ( ) ;
804
+ // SAFETY: caller guarantees `mid` is on a char boundary.
805
+ unsafe {
806
+ (
807
+ from_utf8_unchecked ( slice:: from_raw_parts ( ptr, mid) ) ,
808
+ from_utf8_unchecked ( slice:: from_raw_parts ( ptr. add ( mid) , len - mid) ) ,
809
+ )
810
+ }
811
+ }
812
+
813
+ /// Divides one string slice into two at an index.
814
+ ///
815
+ /// # Safety
816
+ ///
817
+ /// The caller must ensure that `mid` is a valid byte offset from the start
818
+ /// of the string and falls on the boundary of a UTF-8 code point.
819
+ const unsafe fn split_at_mut_unchecked ( & mut self , mid : usize ) -> ( & mut str , & mut str ) {
797
820
let len = self . len ( ) ;
798
821
let ptr = self . as_mut_ptr ( ) ;
799
822
// SAFETY: caller guarantees `mid` is on a char boundary.
0 commit comments