1
Fork 0

auto merge of #8412 : thestinger/rust/vector-add, r=alexcrichton

This commit is contained in:
bors 2013-08-11 04:11:08 -07:00
commit 45da2a5f48
3 changed files with 44 additions and 16 deletions

View file

@ -807,6 +807,7 @@ pub fn from_utf16(v: &[u16]) -> ~str {
/// Allocates a new string with the specified capacity. The string returned is /// Allocates a new string with the specified capacity. The string returned is
/// the empty string, but has capacity for much more. /// the empty string, but has capacity for much more.
#[cfg(stage0)]
#[inline] #[inline]
pub fn with_capacity(capacity: uint) -> ~str { pub fn with_capacity(capacity: uint) -> ~str {
let mut buf = ~""; let mut buf = ~"";
@ -814,6 +815,16 @@ pub fn with_capacity(capacity: uint) -> ~str {
buf 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 /// As char_len but for a slice of a string
/// ///
/// # Arguments /// # Arguments
@ -948,6 +959,14 @@ pub mod raw {
::cast::transmute(v) ::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 /// Create a Rust string from a null-terminated C string
pub unsafe fn from_c_str(buf: *libc::c_char) -> ~str { pub unsafe fn from_c_str(buf: *libc::c_char) -> ~str {
let mut curr = buf; let mut curr = buf;
@ -3700,7 +3719,7 @@ mod tests {
#[cfg(test)] #[cfg(test)]
mod bench { mod bench {
use extra::test::BenchHarness; use extra::test::BenchHarness;
use str; use super::*;
#[bench] #[bench]
fn is_utf8_100_ascii(bh: &mut BenchHarness) { fn is_utf8_100_ascii(bh: &mut BenchHarness) {
@ -3710,7 +3729,7 @@ mod bench {
assert_eq!(100, s.len()); assert_eq!(100, s.len());
do bh.iter { do bh.iter {
str::is_utf8(s); is_utf8(s);
} }
} }
@ -3719,7 +3738,7 @@ mod bench {
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰"); let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
assert_eq!(100, s.len()); assert_eq!(100, s.len());
do bh.iter { 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); s.map_chars(|c| ((c as uint) + 1) as char);
} }
} }
#[bench]
fn bench_with_capacity(bh: &mut BenchHarness) {
do bh.iter {
with_capacity(100);
}
}
} }

View file

@ -12,8 +12,7 @@
use c_str::ToCStr; use c_str::ToCStr;
use cast::transmute; use cast::transmute;
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t}; use libc::{c_char, c_void, size_t, uintptr_t};
use str;
use sys; use sys;
use rt::task::Task; use rt::task::Task;
use rt::local::Local; use rt::local::Local;
@ -93,12 +92,6 @@ pub unsafe fn check_not_borrowed(a: *u8,
borrowck::check_not_borrowed(a, file, line) 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"] #[lang="annihilate"]
pub unsafe fn annihilate() { pub unsafe fn annihilate() {
::cleanup::annihilate() ::cleanup::annihilate()

View file

@ -561,7 +561,7 @@ impl<'self, T> RandomAccessIterator<&'self [T]> for ChunkIter<'self, T> {
#[cfg(not(test))] #[cfg(not(test))]
pub mod traits { pub mod traits {
use super::Vector; use super::*;
use clone::Clone; use clone::Clone;
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Equal, Equiv}; 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] { impl<'self,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'self [T] {
#[inline] #[inline]
fn add(&self, rhs: &V) -> ~[T] { 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.push_all(rhs.as_slice());
res res
} }
} }
impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] { impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
#[inline] #[inline]
fn add(&self, rhs: &V) -> ~[T] { fn add(&self, rhs: &V) -> ~[T] {
let mut res = self.to_owned(); self.as_slice() + rhs.as_slice()
res.push_all(rhs.as_slice());
res
} }
} }
} }
@ -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;
}
}
} }