diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-11 11:49:51 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-12 12:21:03 +1000 |
| commit | 96cd61ad034cc9e88ab6a7845c3480dbc1ea62f3 (patch) | |
| tree | 02a109c7703430ff9595c631e3ba4621c0374f1c /src/libstd | |
| parent | e06579bc0935ed1dcbddef41bc1b6a8850a2059c (diff) | |
| download | rust-96cd61ad034cc9e88ab6a7845c3480dbc1ea62f3.tar.gz rust-96cd61ad034cc9e88ab6a7845c3480dbc1ea62f3.zip | |
std: convert {vec,str}::to_owned to methods.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io.rs | 2 | ||||
| -rw-r--r-- | src/libstd/path.rs | 12 | ||||
| -rw-r--r-- | src/libstd/rand.rs | 2 | ||||
| -rw-r--r-- | src/libstd/str.rs | 34 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 16 |
5 files changed, 32 insertions, 34 deletions
diff --git a/src/libstd/io.rs b/src/libstd/io.rs index 58711360c35..142489df6c1 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -761,7 +761,7 @@ impl<T:Reader> ReaderUtil for T { fn read_lines(&self) -> ~[~str] { do vec::build |push| { for self.each_line |line| { - push(str::to_owned(line)); + push(line.to_owned()); } } } diff --git a/src/libstd/path.rs b/src/libstd/path.rs index d62fc8c2cba..c8ffe007b90 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -515,7 +515,7 @@ impl GenericPath for PosixPath { fn with_filestem(&self, s: &str) -> PosixPath { match self.filetype() { None => self.with_filename(s), - Some(ref t) => self.with_filename(str::to_owned(s) + *t), + Some(ref t) => self.with_filename(s.to_owned() + *t), } } @@ -657,7 +657,7 @@ impl GenericPath for WindowsPath { (None, None) => { host = None; device = None; - rest = str::to_owned(s); + rest = s.to_owned(); } } @@ -729,7 +729,7 @@ impl GenericPath for WindowsPath { fn with_filestem(&self, s: &str) -> WindowsPath { match self.filetype() { None => self.with_filename(s), - Some(ref t) => self.with_filename(str::to_owned(s) + *t), + Some(ref t) => self.with_filename(s.to_owned() + *t), } } @@ -984,7 +984,7 @@ mod tests { fn test_posix_paths() { fn t(wp: &PosixPath, s: &str) { let ss = wp.to_str(); - let sss = str::to_owned(s); + let sss = s.to_owned(); if (ss != sss) { debug!("got %s", ss); debug!("expected %s", sss); @@ -1042,7 +1042,7 @@ mod tests { fn test_normalize() { fn t(wp: &PosixPath, s: &str) { let ss = wp.to_str(); - let sss = str::to_owned(s); + let sss = s.to_owned(); if (ss != sss) { debug!("got %s", ss); debug!("expected %s", sss); @@ -1105,7 +1105,7 @@ mod tests { fn test_windows_paths() { fn t(wp: &WindowsPath, s: &str) { let ss = wp.to_str(); - let sss = str::to_owned(s); + let sss = s.to_owned(); if (ss != sss) { debug!("got %s", ss); debug!("expected %s", sss); diff --git a/src/libstd/rand.rs b/src/libstd/rand.rs index 7946f7e4f13..f7850205930 100644 --- a/src/libstd/rand.rs +++ b/src/libstd/rand.rs @@ -577,7 +577,7 @@ impl<R: Rng> RngUtil for R { /// Shuffle a vec fn shuffle<T:Copy>(&mut self, values: &[T]) -> ~[T] { - let mut m = vec::to_owned(values); + let mut m = values.to_owned(); self.shuffle_mut(m); m } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index f270964c3b5..9d8618e5571 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -107,23 +107,17 @@ pub fn from_bytes_slice<'a>(vector: &'a [u8]) -> &'a str { } } -/// Copy a slice into a new unique str -#[inline(always)] -pub fn to_owned(s: &str) -> ~str { - unsafe { raw::slice_bytes_owned(s, 0, s.len()) } -} - impl ToStr for ~str { #[inline(always)] - fn to_str(&self) -> ~str { to_owned(*self) } + fn to_str(&self) -> ~str { self.to_owned() } } impl<'self> ToStr for &'self str { #[inline(always)] - fn to_str(&self) -> ~str { to_owned(*self) } + fn to_str(&self) -> ~str { self.to_owned() } } impl ToStr for @str { #[inline(always)] - fn to_str(&self) -> ~str { to_owned(*self) } + fn to_str(&self) -> ~str { self.to_owned() } } /** @@ -409,7 +403,7 @@ Section: Transforming strings */ pub fn to_bytes(s: &str) -> ~[u8] { unsafe { - let mut v: ~[u8] = ::cast::transmute(to_owned(s)); + let mut v: ~[u8] = ::cast::transmute(s.to_owned()); vec::raw::set_len(&mut v, s.len()); v } @@ -1237,7 +1231,7 @@ impl<'self> StrUtil for &'self str { // NB: len includes the trailing null. assert!(len > 0); if unsafe { *(ptr::offset(buf,len-1)) != 0 } { - to_owned(self).as_c_str(f) + self.to_owned().as_c_str(f) } else { f(buf as *libc::c_char) } @@ -1526,7 +1520,9 @@ pub mod traits { impl<'self> Add<&'self str,~str> for ~str { #[inline(always)] fn add(&self, rhs: & &'self str) -> ~str { - append(copy *self, (*rhs)) + let mut s = self.to_owned(); + s.push_str(*rhs); + s } } } @@ -1893,10 +1889,13 @@ impl<'self> StrSlice<'self> for &'self str { } } - + /// Copy a slice into a new unique str #[inline] - fn to_owned(&self) -> ~str { to_owned(*self) } + fn to_owned(&self) -> ~str { + unsafe { raw::slice_bytes_owned(*self, 0, self.len()) } + } + /// Copy a slice into a new @str #[inline] fn to_managed(&self) -> @str { let v = at_vec::from_fn(self.len() + 1, |i| { @@ -2252,7 +2251,7 @@ impl OwnedStr for ~str { impl Clone for ~str { #[inline(always)] fn clone(&self) -> ~str { - to_owned(*self) + self.to_owned() } } @@ -3135,6 +3134,11 @@ mod tests { assert_eq!("abc".to_managed(), @"abc"); assert_eq!("abcdef".slice(1, 5).to_managed(), @"bcde"); } + #[test] + fn test_to_owned() { + assert_eq!("abc".to_owned(), ~"abc"); + assert_eq!("abcdef".slice(1, 5).to_owned(), ~"bcde"); + } #[test] fn test_total_ord() { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 19233c53348..52cb20458ea 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -171,11 +171,6 @@ pub fn from_elem<T:Copy>(n_elts: uint, t: T) -> ~[T] { } } -/// Creates a new unique vector with the same contents as the slice -pub fn to_owned<T:Copy>(t: &[T]) -> ~[T] { - from_fn(t.len(), |i| t[i]) -} - /// Creates a new vector with a capacity of `capacity` pub fn with_capacity<T>(capacity: uint) -> ~[T] { let mut vec = ~[]; @@ -1787,7 +1782,7 @@ pub trait CopyableVector<T> { /// Extension methods for vectors impl<'self,T:Copy> CopyableVector<T> for &'self [T] { - /// Returns a copy of `v`. + /// Creates a new unique vector with the same contents as the slice #[inline] fn to_owned(&self) -> ~[T] { let mut result = ~[]; @@ -1796,7 +1791,6 @@ impl<'self,T:Copy> CopyableVector<T> for &'self [T] { result.push(copy *e); } result - } } @@ -3361,19 +3355,19 @@ mod tests { let mut results: ~[~[int]]; results = ~[]; - for each_permutation([]) |v| { results.push(to_owned(v)); } + for each_permutation([]) |v| { results.push(v.to_owned()); } assert_eq!(results, ~[~[]]); results = ~[]; - for each_permutation([7]) |v| { results.push(to_owned(v)); } + for each_permutation([7]) |v| { results.push(v.to_owned()); } assert_eq!(results, ~[~[7]]); results = ~[]; - for each_permutation([1,1]) |v| { results.push(to_owned(v)); } + for each_permutation([1,1]) |v| { results.push(v.to_owned()); } assert_eq!(results, ~[~[1,1],~[1,1]]); results = ~[]; - for each_permutation([5,2,0]) |v| { results.push(to_owned(v)); } + for each_permutation([5,2,0]) |v| { results.push(v.to_owned()); } assert!(results == ~[~[5,2,0],~[5,0,2],~[2,5,0],~[2,0,5],~[0,5,2],~[0,2,5]]); } |
