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
/// 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 unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
::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 @@ fn sum_len<S: Container>(v: &[S]) -> uint {
#[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 @@ fn is_utf8_100_ascii(bh: &mut BenchHarness) {
assert_eq!(100, s.len());
do bh.iter {
str::is_utf8(s);
is_utf8(s);
}
}
@ -3719,7 +3738,7 @@ fn is_utf8_100_multibyte(bh: &mut BenchHarness) {
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
assert_eq!(100, s.len());
do bh.iter {
str::is_utf8(s);
is_utf8(s);
}
}
@ -3742,4 +3761,11 @@ fn map_chars_100_multibytes(bh: &mut BenchHarness) {
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 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()

View File

@ -561,7 +561,7 @@ fn idx(&self, index: uint) -> Option<&'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 @@ fn gt(&self, other: &@[T]) -> bool { self.as_slice() > other.as_slice() }
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 @@ fn mut_iterator(bh: &mut BenchHarness) {
}
}
}
#[bench]
fn add(b: &mut BenchHarness) {
let xs: &[int] = [5, ..10];
let ys: &[int] = [5, ..10];
do b.iter() {
xs + ys;
}
}
}