std: convert {vec,str}::to_owned to methods.
This commit is contained in:
parent
e06579bc09
commit
96cd61ad03
31 changed files with 95 additions and 99 deletions
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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]]);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue