]> git.lizzy.rs Git - rust.git/commitdiff
Add str::split_at_mut
authorSimon Sapin <simon.sapin@exyr.org>
Mon, 15 Jun 2015 17:24:52 +0000 (19:24 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Mon, 13 Jul 2015 14:21:43 +0000 (16:21 +0200)
src/libcollections/str.rs
src/libcollectionstest/lib.rs
src/libcollectionstest/str.rs
src/libcore/str/mod.rs

index 711e2887865d9ccf882d08ccc80e6c2adee1e598..306a8ba3e090026aaf6c068559cbd1855d669af4 100644 (file)
@@ -793,6 +793,12 @@ pub fn split_at(&self, mid: usize) -> (&str, &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
index 6bf7380fdeb500138cc923157607184a17f7c39d..233623b8a5e4eaa11b64d04f49a074aaea976a9e 100644 (file)
@@ -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)]
index 4eee99f2bc91284137d97f77499af643718bbdba..66d9569710259b80e841f8e3116d9601f055e8bb 100644 (file)
@@ -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() {
index c8237720b0e90d35e91f6a04cb85e586aa20a89b..7e4c2ba3be87590256d63b8ae45b02e4b4baa1df 100644 (file)
@@ -1279,6 +1279,7 @@ fn rfind<'a, P: Pattern<'a>>(&'a self, pat: P) -> Option<usize>
         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 @@ fn split_at(&self, mid: usize) -> (&str, &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() {