diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-07-23 06:49:17 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-07-23 16:56:23 -0700 |
| commit | 31b77aecfc195c774852965329b5e75453eee0b2 (patch) | |
| tree | bf46d091c4150e3da7c813ab4a5df633d68a08df /src/libstd | |
| parent | cc9666f68f829c17ff3a535f714fe5dbb3f72755 (diff) | |
| download | rust-31b77aecfc195c774852965329b5e75453eee0b2.tar.gz rust-31b77aecfc195c774852965329b5e75453eee0b2.zip | |
std: remove str::to_owned and str::raw::slice_bytes_owned
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io.rs | 2 | ||||
| -rw-r--r-- | src/libstd/str.rs | 63 |
2 files changed, 23 insertions, 42 deletions
diff --git a/src/libstd/io.rs b/src/libstd/io.rs index fed4eb26dbe..05a5184ccba 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -763,7 +763,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/str.rs b/src/libstd/str.rs index 59121d6f135..42b651a8e38 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -32,7 +32,7 @@ use ptr::RawPtr; use to_str::ToStr; use uint; use vec; -use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector}; +use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector}; /* Section: Conditions @@ -120,23 +120,17 @@ pub fn from_bytes_slice<'a>(vector: &'a [u8]) -> &'a str { } } -/// Copy a slice into a new unique str -#[inline] -pub fn to_owned(s: &str) -> ~str { - unsafe { raw::slice_bytes_owned(s, 0, s.len()) } -} - impl ToStr for ~str { #[inline] - fn to_str(&self) -> ~str { to_owned(*self) } + fn to_str(&self) -> ~str { self.to_owned() } } impl<'self> ToStr for &'self str { #[inline] - fn to_str(&self) -> ~str { to_owned(*self) } + fn to_str(&self) -> ~str { self.to_owned() } } impl ToStr for @str { #[inline] - fn to_str(&self) -> ~str { to_owned(*self) } + fn to_str(&self) -> ~str { self.to_owned() } } /** @@ -867,33 +861,6 @@ pub mod raw { * If begin is greater than end. * If end is greater than the length of the string. */ - pub unsafe fn slice_bytes_owned(s: &str, begin: uint, end: uint) -> ~str { - do s.as_imm_buf |sbuf, n| { - assert!((begin <= end)); - assert!((end <= n)); - - let mut v = vec::with_capacity(end - begin + 1u); - do v.as_imm_buf |vbuf, _vlen| { - let vbuf = ::cast::transmute_mut_unsafe(vbuf); - let src = ptr::offset(sbuf, begin); - ptr::copy_memory(vbuf, src, end - begin); - } - vec::raw::set_len(&mut v, end - begin); - v.push(0u8); - ::cast::transmute(v) - } - } - - /** - * Takes a bytewise (not UTF-8) slice from a string. - * - * Returns the substring from [`begin`..`end`). - * - * # Failure - * - * If begin is greater than end. - * If end is greater than the length of the string. - */ #[inline] pub unsafe fn slice_bytes(s: &str, begin: uint, end: uint) -> &str { do s.as_imm_buf |sbuf, n| { @@ -936,7 +903,7 @@ pub mod raw { let len = s.len(); assert!((len > 0u)); let b = s[0]; - *s = raw::slice_bytes_owned(*s, 1u, len); + *s = s.slice(1, len).to_owned(); return b; } @@ -1609,7 +1576,21 @@ 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 { + do self.as_imm_buf |src, len| { + assert!(len > 0); + unsafe { + let mut v = vec::with_capacity(len); + + do v.as_mut_buf |dst, _| { + ptr::copy_memory(dst, src, len - 1); + } + vec::raw::set_len(&mut v, len - 1); + v.push(0u8); + ::cast::transmute(v) + } + } + } #[inline] fn to_managed(&self) -> @str { @@ -2177,7 +2158,7 @@ impl OwnedStr for ~str { */ fn shift_char(&mut self) -> char { let CharRange {ch, next} = self.char_range_at(0u); - *self = unsafe { raw::slice_bytes_owned(*self, next, self.len()) }; + *self = self.slice(next, self.len()).to_owned(); return ch; } @@ -2270,7 +2251,7 @@ impl OwnedStr for ~str { impl Clone for ~str { #[inline] fn clone(&self) -> ~str { - to_owned(*self) + self.to_owned() } } |
