Add str::split_at_mut
This commit is contained in:
parent
f9005512a9
commit
7469914e96
@ -793,6 +793,12 @@ impl str {
|
||||
core_str::StrExt::split_at(self, mid)
|
||||
}
|
||||
|
||||
/// Divide one mutable string slice into two at an index.
|
||||
#[inline]
|
||||
pub fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
|
||||
core_str::StrExt::split_at_mut(self, mid)
|
||||
}
|
||||
|
||||
/// An iterator over the codepoints of `self`.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -8,6 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![feature(ascii)]
|
||||
#![feature(append)]
|
||||
#![feature(bitset)]
|
||||
#![feature(bitvec)]
|
||||
|
@ -701,6 +701,18 @@ fn test_split_at() {
|
||||
assert_eq!(b, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_split_at_mut() {
|
||||
use std::ascii::AsciiExt;
|
||||
let mut s = "Hello World".to_string();
|
||||
{
|
||||
let (a, b) = s.split_at_mut(5);
|
||||
a.make_ascii_uppercase();
|
||||
b.make_ascii_lowercase();
|
||||
}
|
||||
assert_eq!(s, "HELLO world");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn test_split_at_boundscheck() {
|
||||
|
@ -1279,6 +1279,7 @@ pub trait StrExt {
|
||||
where P::Searcher: ReverseSearcher<'a>;
|
||||
fn find_str<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>;
|
||||
fn split_at(&self, mid: usize) -> (&str, &str);
|
||||
fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str);
|
||||
fn slice_shift_char<'a>(&'a self) -> Option<(char, &'a str)>;
|
||||
fn subslice_offset(&self, inner: &str) -> usize;
|
||||
fn as_ptr(&self) -> *const u8;
|
||||
@ -1591,6 +1592,20 @@ impl StrExt for str {
|
||||
}
|
||||
}
|
||||
|
||||
fn split_at_mut(&mut self, mid: usize) -> (&mut str, &mut str) {
|
||||
// is_char_boundary checks that the index is in [0, .len()]
|
||||
if self.is_char_boundary(mid) {
|
||||
let len = self.len();
|
||||
unsafe {
|
||||
let self2: &mut str = mem::transmute_copy(&self);
|
||||
(self.slice_mut_unchecked(0, mid),
|
||||
self2.slice_mut_unchecked(mid, len))
|
||||
}
|
||||
} else {
|
||||
slice_error_fail(self, 0, mid)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn slice_shift_char(&self) -> Option<(char, &str)> {
|
||||
if self.is_empty() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user