Skip to content

Commit 45da2a5

Browse files
committed
auto merge of #8412 : thestinger/rust/vector-add, r=alexcrichton
2 parents cac9aff + 83b3a0e commit 45da2a5

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

src/libstd/str.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -807,13 +807,24 @@ pub fn from_utf16(v: &[u16]) -> ~str {
807807

808808
/// Allocates a new string with the specified capacity. The string returned is
809809
/// the empty string, but has capacity for much more.
810+
#[cfg(stage0)]
810811
#[inline]
811812
pub fn with_capacity(capacity: uint) -> ~str {
812813
let mut buf = ~"";
813814
buf.reserve(capacity);
814815
buf
815816
}
816817

818+
/// Allocates a new string with the specified capacity. The string returned is
819+
/// the empty string, but has capacity for much more.
820+
#[cfg(not(stage0))]
821+
#[inline]
822+
pub fn with_capacity(capacity: uint) -> ~str {
823+
unsafe {
824+
cast::transmute(vec::with_capacity::<~[u8]>(capacity))
825+
}
826+
}
827+
817828
/// As char_len but for a slice of a string
818829
///
819830
/// # Arguments
@@ -948,6 +959,14 @@ pub mod raw {
948959
::cast::transmute(v)
949960
}
950961

962+
#[lang="strdup_uniq"]
963+
#[cfg(not(test))]
964+
#[allow(missing_doc)]
965+
#[inline]
966+
pub unsafe fn strdup_uniq(ptr: *u8, len: uint) -> ~str {
967+
from_buf_len(ptr, len)
968+
}
969+
951970
/// Create a Rust string from a null-terminated C string
952971
pub unsafe fn from_c_str(buf: *libc::c_char) -> ~str {
953972
let mut curr = buf;
@@ -3700,7 +3719,7 @@ mod tests {
37003719
#[cfg(test)]
37013720
mod bench {
37023721
use extra::test::BenchHarness;
3703-
use str;
3722+
use super::*;
37043723
37053724
#[bench]
37063725
fn is_utf8_100_ascii(bh: &mut BenchHarness) {
@@ -3710,7 +3729,7 @@ mod bench {
37103729
37113730
assert_eq!(100, s.len());
37123731
do bh.iter {
3713-
str::is_utf8(s);
3732+
is_utf8(s);
37143733
}
37153734
}
37163735
@@ -3719,7 +3738,7 @@ mod bench {
37193738
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
37203739
assert_eq!(100, s.len());
37213740
do bh.iter {
3722-
str::is_utf8(s);
3741+
is_utf8(s);
37233742
}
37243743
}
37253744
@@ -3742,4 +3761,11 @@ mod bench {
37423761
s.map_chars(|c| ((c as uint) + 1) as char);
37433762
}
37443763
}
3764+
3765+
#[bench]
3766+
fn bench_with_capacity(bh: &mut BenchHarness) {
3767+
do bh.iter {
3768+
with_capacity(100);
3769+
}
3770+
}
37453771
}

src/libstd/unstable/lang.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
1313
use c_str::ToCStr;
1414
use cast::transmute;
15-
use libc::{c_char, c_uchar, c_void, size_t, uintptr_t};
16-
use str;
15+
use libc::{c_char, c_void, size_t, uintptr_t};
1716
use sys;
1817
use rt::task::Task;
1918
use rt::local::Local;
@@ -93,12 +92,6 @@ pub unsafe fn check_not_borrowed(a: *u8,
9392
borrowck::check_not_borrowed(a, file, line)
9493
}
9594

96-
#[lang="strdup_uniq"]
97-
#[inline]
98-
pub unsafe fn strdup_uniq(ptr: *c_uchar, len: uint) -> ~str {
99-
str::raw::from_buf_len(ptr, len)
100-
}
101-
10295
#[lang="annihilate"]
10396
pub unsafe fn annihilate() {
10497
::cleanup::annihilate()

src/libstd/vec.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'self, T> RandomAccessIterator<&'self [T]> for ChunkIter<'self, T> {
561561

562562
#[cfg(not(test))]
563563
pub mod traits {
564-
use super::Vector;
564+
use super::*;
565565

566566
use clone::Clone;
567567
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Equal, Equiv};
@@ -686,17 +686,17 @@ pub mod traits {
686686
impl<'self,T:Clone, V: Vector<T>> Add<V, ~[T]> for &'self [T] {
687687
#[inline]
688688
fn add(&self, rhs: &V) -> ~[T] {
689-
let mut res = self.to_owned();
689+
let mut res = with_capacity(self.len() + rhs.as_slice().len());
690+
res.push_all(*self);
690691
res.push_all(rhs.as_slice());
691692
res
692693
}
693694
}
695+
694696
impl<T:Clone, V: Vector<T>> Add<V, ~[T]> for ~[T] {
695697
#[inline]
696698
fn add(&self, rhs: &V) -> ~[T] {
697-
let mut res = self.to_owned();
698-
res.push_all(rhs.as_slice());
699-
res
699+
self.as_slice() + rhs.as_slice()
700700
}
701701
}
702702
}
@@ -3662,4 +3662,13 @@ mod bench {
36623662
}
36633663
}
36643664
}
3665+
3666+
#[bench]
3667+
fn add(b: &mut BenchHarness) {
3668+
let xs: &[int] = [5, ..10];
3669+
let ys: &[int] = [5, ..10];
3670+
do b.iter() {
3671+
xs + ys;
3672+
}
3673+
}
36653674
}

0 commit comments

Comments
 (0)