summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-08-11 04:11:08 -0700
committerbors <bors@rust-lang.org>2013-08-11 04:11:08 -0700
commit45da2a5f48300eff8ccfd524a0bad5c351c20298 (patch)
tree57da7bbb03fb467fcb97638c2a02119214f5bf07 /src/libstd
parentcac9affc20984a8a2d01c2ff3201a56f91b5bc6e (diff)
parent83b3a0eaf166c5dc88eb0ab9b4b793a3694f680b (diff)
downloadrust-45da2a5f48300eff8ccfd524a0bad5c351c20298.tar.gz
rust-45da2a5f48300eff8ccfd524a0bad5c351c20298.zip
auto merge of #8412 : thestinger/rust/vector-add, r=alexcrichton
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs32
-rw-r--r--src/libstd/unstable/lang.rs9
-rw-r--r--src/libstd/vec.rs19
3 files changed, 44 insertions, 16 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 26a00cca4c8..0b270edc534 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -807,6 +807,7 @@ pub fn from_utf16(v: &[u16]) -> ~str {
 
 /// Allocates a new string with the specified capacity. The string returned is
 /// the empty string, but has capacity for much more.
+#[cfg(stage0)]
 #[inline]
 pub fn with_capacity(capacity: uint) -> ~str {
     let mut buf = ~"";
@@ -814,6 +815,16 @@ pub fn with_capacity(capacity: uint) -> ~str {
     buf
 }
 
+/// Allocates a new string with the specified capacity. The string returned is
+/// the empty string, but has capacity for much more.
+#[cfg(not(stage0))]
+#[inline]
+pub fn with_capacity(capacity: uint) -> ~str {
+    unsafe {
+        cast::transmute(vec::with_capacity::<~[u8]>(capacity))
+    }
+}
+
 /// As char_len but for a slice of a string
 ///
 /// # Arguments
@@ -948,6 +959,14 @@ pub mod raw {
         ::cast::transmute(v)
     }
 
+    #[lang="strdup_uniq"]
+    #[cfg(not(test))]
+    #[allow(missing_doc)]
+    #[inline]
+    pub unsafe fn strdup_uniq(ptr: *u8, len: uint) -> ~str {
+        from_buf_len(ptr, len)
+    }
+
     /// Create a Rust string from a null-terminated C string
     pub unsafe fn from_c_str(buf: *libc::c_char) -> ~str {
         let mut curr = buf;
@@ -3700,7 +3719,7 @@ mod tests {
 #[cfg(test)]
 mod bench {
     use extra::test::BenchHarness;
-    use str;
+    use super::*;
 
     #[bench]
     fn is_utf8_100_ascii(bh: &mut BenchHarness) {
@@ -3710,7 +3729,7 @@ mod bench {
 
         assert_eq!(100, s.len());
         do bh.iter {
-            str::is_utf8(s);
+            is_utf8(s);
         }
     }
 
@@ -3719,7 +3738,7 @@ mod bench {
         let s = bytes!("πŒ€πŒ–πŒ‹πŒ„πŒ‘πŒ‰ΰΈ›ΰΈ£Ψ―ΩˆΩ„Ψ© Ψ§Ω„ΩƒΩˆΩŠΨͺΰΈ—ΰΈ¨ΰΉ„ΰΈ—ΰΈ’δΈ­εŽπ…πŒΏπŒ»π†πŒΉπŒ»πŒ°");
         assert_eq!(100, s.len());
         do bh.iter {
-            str::is_utf8(s);
+            is_utf8(s);
         }
     }
 
@@ -3742,4 +3761,11 @@ mod bench {
             s.map_chars(|c| ((c as uint) + 1) as char);
         }
     }
+
+    #[bench]
+    fn bench_with_capacity(bh: &mut BenchHarness) {
+        do bh.iter {
+            with_capacity(100);
+        }
+    }
 }
diff --git a/src/libstd/unstable/lang.rs b/src/libstd/unstable/lang.rs
index 9e7ac1fd7db..d8df967a45c 100644
--- a/src/libstd/unstable/lang.rs
+++ b/src/libstd/unstable/lang.rs
@@ -12,8 +12,7 @@
 
 use c_str::ToCStr;
 use cast::transmute;
-use libc::{c_char, c_uchar, c_void, size_t, uintptr_t};
-use str;
+use libc::{c_char, c_void, size_t, uintptr_t};
 use sys;
 use rt::task::Task;
 use rt::local::Local;
@@ -93,12 +92,6 @@ pub unsafe fn check_not_borrowed(a: *u8,
     borrowck::check_not_borrowed(a, file, line)
 }
 
-#[lang="strdup_uniq"]
-#[inline]
-pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
-    str::raw::from_buf_len(ptr, len)
-}
-
 #[lang="annihilate"]
 pub unsafe fn annihilate() {
     ::cleanup::annihilate()
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index c831dd70918..d626b8604db 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -561,7 +561,7 @@ impl<'self, T> RandomAccessIterator<&'self [T]> for ChunkIter<'self, T> {
 
 #[cfg(not(test))]
 pub mod traits {
-    use super::Vector;
+    use super::*;
 
     use clone::Clone;
     use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Equal, Equiv};
@@ -686,17 +686,17 @@ pub mod traits {
     impl<'self,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'self [T] {
         #[inline]
         fn add(&self, rhs: &V) -> ~[T] {
-            let mut res = self.to_owned();
+            let mut res = with_capacity(self.len() + rhs.as_slice().len());
+            res.push_all(*self);
             res.push_all(rhs.as_slice());
             res
         }
     }
+
     impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
         #[inline]
         fn add(&self, rhs: &V) -> ~[T] {
-            let mut res = self.to_owned();
-            res.push_all(rhs.as_slice());
-            res
+            self.as_slice() + rhs.as_slice()
         }
     }
 }
@@ -3662,4 +3662,13 @@ mod bench {
             }
         }
     }
+
+    #[bench]
+    fn add(b: &mut BenchHarness) {
+        let xs: &[int] = [5, ..10];
+        let ys: &[int] = [5, ..10];
+        do b.iter() {
+            xs + ys;
+        }
+    }
 }