core: Inherit possible string functionality

This moves as much allocation as possible from teh std::str module into
core::str. This includes essentially all non-allocating functionality, mostly
iterators and slicing and such.

This primarily splits the Str trait into only having the as_slice() method,
adding a new StrAllocating trait to std::str which contains the relevant new
allocation methods. This is a breaking change if any of the methods of "trait
Str" were overriden. The old functionality can be restored by implementing both
the Str and StrAllocating traits.

[breaking-change]
This commit is contained in:
Alex Crichton 2014-04-30 23:06:36 -07:00
parent 544d909401
commit 9bae6ec828
18 changed files with 1986 additions and 1952 deletions

View File

@ -71,4 +71,5 @@ pub mod option;
pub mod raw;
pub mod char;
pub mod slice;
pub mod str;
pub mod tuple;

1861
src/libcore/str.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -493,11 +493,11 @@ use io;
use iter;
use iter::{Iterator, range};
use num::Signed;
use option::{Option,Some,None};
use option::{Option, Some, None};
use owned::Box;
use repr;
use result::{Ok, Err};
use str::StrSlice;
use result::{Ok, Err, ResultUnwrap};
use str::{StrSlice, StrAllocating, UTF16Item, ScalarValue, LoneSurrogate};
use str;
use slice::{Vector, ImmutableVector};
use slice;
@ -1359,5 +1359,20 @@ impl Show for cmp::Ordering {
}
}
impl<T: Copy + Show> Show for Cell<T> {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f.buf, r"Cell \{ value: {} \}", self.get())
}
}
impl Show for UTF16Item {
fn fmt(&self, f: &mut Formatter) -> Result {
match *self {
ScalarValue(c) => write!(f.buf, "ScalarValue({})", c),
LoneSurrogate(u) => write!(f.buf, "LoneSurrogate({})", u),
}
}
}
// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
// it's a lot easier than creating all of the rt::Piece structures here.

View File

@ -194,7 +194,7 @@ mod tests {
use fmt::radix;
use super::{Binary, Octal, Decimal, LowerHex, UpperHex};
use super::{GenericRadix, Radix};
use str::StrSlice;
use str::StrAllocating;
#[test]
fn test_radix_base() {

View File

@ -362,7 +362,7 @@ mod tests {
use prelude::*;
use num::ToStrRadix;
use option::{Some, None};
use str::{Str,StrSlice};
use str::Str;
use strbuf::StrBuf;
use slice::{Vector, ImmutableVector};
use self::test::Bencher;

View File

@ -230,7 +230,7 @@ use option::{Option, Some, None};
use owned::Box;
use path::Path;
use result::{Ok, Err, Result};
use str::StrSlice;
use str::{StrSlice, StrAllocating};
use str;
use uint;
use unstable::finally::try_finally;

View File

@ -428,7 +428,6 @@ impl Drop for Process {
mod tests {
use io::process::{ProcessConfig, Process};
use prelude::*;
use str::StrSlice;
// FIXME(#10380) these tests should not all be ignored on android.

View File

@ -357,7 +357,6 @@ mod tests {
use super::*;
use owned::Box;
use task;
use str::StrSlice;
#[test]
fn test_tls_multitask() {

View File

@ -38,7 +38,7 @@ use ops::Drop;
use result::{Err, Ok, Result};
use ptr;
use str;
use str::{Str, StrSlice};
use str::{Str, StrSlice, StrAllocating};
use fmt;
use sync::atomics::{AtomicInt, INIT_ATOMIC_INT, SeqCst};
use path::{Path, GenericPath};

View File

@ -21,7 +21,7 @@ use io::Writer;
use iter::{AdditiveIterator, DoubleEndedIterator, Extendable, Rev, Iterator, Map};
use option::{Option, Some, None};
use slice::{Vector, OwnedVector, ImmutableVector};
use str::{CharSplits, Str, StrVector, StrSlice};
use str::{CharSplits, Str, StrAllocating, StrVector, StrSlice};
use strbuf::StrBuf;
use vec::Vec;
@ -684,7 +684,7 @@ impl Path {
}
}
fn normalize_<S: Str>(s: S) -> (Option<PathPrefix>, StrBuf) {
fn normalize_<S: StrAllocating>(s: S) -> (Option<PathPrefix>, StrBuf) {
// make borrowck happy
let (prefix, val) = {
let prefix = parse_prefix(s.as_slice());
@ -842,7 +842,7 @@ impl Path {
}
fn update_normalized<S: Str>(&mut self, s: S) {
let (prefix, path) = Path::normalize_(s);
let (prefix, path) = Path::normalize_(s.as_slice());
self.repr = path;
self.prefix = prefix;
self.update_sepidx();

View File

@ -70,6 +70,7 @@ pub use path::{GenericPath, Path, PosixPath, WindowsPath};
pub use ptr::RawPtr;
pub use io::{Buffer, Writer, Reader, Seek};
pub use str::{Str, StrVector, StrSlice, OwnedStr, IntoMaybeOwned};
pub use str::{StrAllocating};
pub use to_str::{ToStr, IntoStr};
pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4};
pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8};

View File

@ -606,6 +606,7 @@ pub fn write_repr<T>(writer: &mut io::Writer, object: &T) -> io::IoResult<()> {
pub fn repr_to_str<T>(t: &T) -> ~str {
use str;
use str::StrAllocating;
use io;
let mut result = io::MemWriter::new();

View File

@ -70,7 +70,6 @@ mod imp {
use owned::Box;
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
use mem;
#[cfg(not(test))] use str::StrSlice;
#[cfg(not(test))] use ptr::RawPtr;
static mut global_args_ptr: uint = 0;

File diff suppressed because it is too large Load Diff

View File

@ -20,8 +20,8 @@ use iter::{Extendable, FromIterator, Iterator, range};
use option::{None, Option, Some};
use ptr::RawPtr;
use slice::{OwnedVector, Vector};
use str::{OwnedStr, Str, StrSlice, StrAllocating};
use str;
use str::{OwnedStr, Str, StrSlice};
use vec::Vec;
/// A growable string stored as a UTF-8 encoded buffer.
@ -268,7 +268,9 @@ impl Str for StrBuf {
cast::transmute(self.vec.as_slice())
}
}
}
impl StrAllocating for StrBuf {
#[inline]
fn into_owned(self) -> ~str {
let StrBuf {

View File

@ -49,7 +49,7 @@ use str::{Str, SendStr, IntoMaybeOwned};
#[cfg(test)] use any::{AnyOwnExt, AnyRefExt};
#[cfg(test)] use result;
#[cfg(test)] use str::StrSlice;
#[cfg(test)] use str::StrAllocating;
/// Indicates the manner in which a task exited.
///

View File

@ -35,7 +35,7 @@ impl<T: fmt::Show> ToStr for T {
#[cfg(test)]
mod tests {
use super::*;
use str::StrSlice;
use str::StrAllocating;
#[test]
fn test_simple_types() {

View File

@ -109,11 +109,6 @@ impl Str for RcStr {
let s: &'a str = *self.string;
s
}
#[inline]
fn into_owned(self) -> ~str {
self.string.to_owned()
}
}
impl fmt::Show for RcStr {