From e878721d70349e2055f0ef854085de92e9498fde Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Mon, 19 May 2014 23:19:56 -0700 Subject: libcore: Remove all uses of `~str` from `libcore`. [breaking-change] --- src/libcore/any.rs | 14 ++- src/libcore/bool.rs | 6 +- src/libcore/cell.rs | 5 +- src/libcore/char.rs | 69 +++++++----- src/libcore/cmp.rs | 2 +- src/libcore/fmt/mod.rs | 8 +- src/libcore/fmt/num.rs | 230 ++++++++++++++++++++-------------------- src/libcore/mem.rs | 3 +- src/libcore/option.rs | 24 +++-- src/libcore/ptr.rs | 5 +- src/libcore/raw.rs | 1 - src/libcore/result.rs | 113 +++++++++++--------- src/libcore/should_not_exist.rs | 112 +------------------ src/libcore/str.rs | 50 +-------- src/libcore/tuple.rs | 13 ++- 15 files changed, 273 insertions(+), 382 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/any.rs b/src/libcore/any.rs index 61c1193e515..94ac344db34 100644 --- a/src/libcore/any.rs +++ b/src/libcore/any.rs @@ -119,7 +119,7 @@ mod tests { use prelude::*; use super::*; use realstd::owned::{Box, AnyOwnExt}; - use realstd::str::StrAllocating; + use realstd::str::{Str, StrAllocating}; #[deriving(Eq, Show)] struct Test; @@ -249,13 +249,17 @@ mod tests { use realstd::to_str::ToStr; let a = box 8u as Box<::realstd::any::Any>; let b = box Test as Box<::realstd::any::Any>; - assert_eq!(a.to_str(), "Box".to_owned()); - assert_eq!(b.to_str(), "Box".to_owned()); + let a_str = a.to_str(); + let b_str = b.to_str(); + assert_eq!(a_str.as_slice(), "Box"); + assert_eq!(b_str.as_slice(), "Box"); let a = &8u as &Any; let b = &Test as &Any; - assert_eq!(format!("{}", a), "&Any".to_owned()); - assert_eq!(format!("{}", b), "&Any".to_owned()); + let s = format!("{}", a); + assert_eq!(s.as_slice(), "&Any"); + let s = format!("{}", b); + assert_eq!(s.as_slice(), "&Any"); } } diff --git a/src/libcore/bool.rs b/src/libcore/bool.rs index 2a44d141719..ddfdbca196c 100644 --- a/src/libcore/bool.rs +++ b/src/libcore/bool.rs @@ -242,8 +242,10 @@ mod tests { #[test] fn test_to_str() { - assert_eq!(false.to_str(), "false".to_owned()); - assert_eq!(true.to_str(), "true".to_owned()); + let s = false.to_str(); + assert_eq!(s.as_slice(), "false"); + let s = true.to_str(); + assert_eq!(s.as_slice(), "true"); } #[test] diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 093b3f57047..3ad2ebb9f0a 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -404,12 +404,13 @@ mod test { #[test] fn cell_has_sensible_show() { use str::StrSlice; + use realstd::str::Str; let x = Cell::new("foo bar"); - assert!(format!("{}", x).contains(x.get())); + assert!(format!("{}", x).as_slice().contains(x.get())); x.set("baz qux"); - assert!(format!("{}", x).contains(x.get())); + assert!(format!("{}", x).as_slice().contains(x.get())); } #[test] diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 6e9d4c9bafb..f3942dfd753 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -637,7 +637,7 @@ mod test { use slice::ImmutableVector; use option::{Some, None}; use realstd::strbuf::StrBuf; - use realstd::str::StrAllocating; + use realstd::str::{Str, StrAllocating}; #[test] fn test_is_lowercase() { @@ -742,46 +742,65 @@ mod test { #[test] fn test_escape_default() { - fn string(c: char) -> ~str { + fn string(c: char) -> StrBuf { let mut result = StrBuf::new(); escape_default(c, |c| { result.push_char(c); }); - return result.into_owned(); + return result; } - assert_eq!(string('\n'), "\\n".to_owned()); - assert_eq!(string('\r'), "\\r".to_owned()); - assert_eq!(string('\''), "\\'".to_owned()); - assert_eq!(string('"'), "\\\"".to_owned()); - assert_eq!(string(' '), " ".to_owned()); - assert_eq!(string('a'), "a".to_owned()); - assert_eq!(string('~'), "~".to_owned()); - assert_eq!(string('\x00'), "\\x00".to_owned()); - assert_eq!(string('\x1f'), "\\x1f".to_owned()); - assert_eq!(string('\x7f'), "\\x7f".to_owned()); - assert_eq!(string('\xff'), "\\xff".to_owned()); - assert_eq!(string('\u011b'), "\\u011b".to_owned()); - assert_eq!(string('\U0001d4b6'), "\\U0001d4b6".to_owned()); + let s = string('\n'); + assert_eq!(s.as_slice(), "\\n"); + let s = string('\r'); + assert_eq!(s.as_slice(), "\\r"); + let s = string('\''); + assert_eq!(s.as_slice(), "\\'"); + let s = string('"'); + assert_eq!(s.as_slice(), "\\\""); + let s = string(' '); + assert_eq!(s.as_slice(), " "); + let s = string('a'); + assert_eq!(s.as_slice(), "a"); + let s = string('~'); + assert_eq!(s.as_slice(), "~"); + let s = string('\x00'); + assert_eq!(s.as_slice(), "\\x00"); + let s = string('\x1f'); + assert_eq!(s.as_slice(), "\\x1f"); + let s = string('\x7f'); + assert_eq!(s.as_slice(), "\\x7f"); + let s = string('\xff'); + assert_eq!(s.as_slice(), "\\xff"); + let s = string('\u011b'); + assert_eq!(s.as_slice(), "\\u011b"); + let s = string('\U0001d4b6'); + assert_eq!(s.as_slice(), "\\U0001d4b6"); } #[test] fn test_escape_unicode() { - fn string(c: char) -> ~str { + fn string(c: char) -> StrBuf { let mut result = StrBuf::new(); escape_unicode(c, |c| { result.push_char(c); }); - return result.into_owned(); + return result; } - assert_eq!(string('\x00'), "\\x00".to_owned()); - assert_eq!(string('\n'), "\\x0a".to_owned()); - assert_eq!(string(' '), "\\x20".to_owned()); - assert_eq!(string('a'), "\\x61".to_owned()); - assert_eq!(string('\u011b'), "\\u011b".to_owned()); - assert_eq!(string('\U0001d4b6'), "\\U0001d4b6".to_owned()); + let s = string('\x00'); + assert_eq!(s.as_slice(), "\\x00"); + let s = string('\n'); + assert_eq!(s.as_slice(), "\\x0a"); + let s = string(' '); + assert_eq!(s.as_slice(), "\\x20"); + let s = string('a'); + assert_eq!(s.as_slice(), "\\x61"); + let s = string('\u011b'); + assert_eq!(s.as_slice(), "\\u011b"); + let s = string('\U0001d4b6'); + assert_eq!(s.as_slice(), "\\U0001d4b6"); } #[test] fn test_to_str() { use realstd::to_str::ToStr; let s = 't'.to_str(); - assert_eq!(s, "t".to_owned()); + assert_eq!(s.as_slice(), "t"); } #[test] diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index a50108607ce..bf1f3ee310c 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -171,7 +171,7 @@ pub trait Ord: Eq { /// The equivalence relation. Two values may be equivalent even if they are /// of different types. The most common use case for this relation is /// container types; e.g. it is often desirable to be able to use `&str` -/// values to look up entries in a container with `~str` keys. +/// values to look up entries in a container with `StrBuf` keys. pub trait Equiv { /// Implement this function to decide equivalent values. fn equiv(&self, other: &T) -> bool; diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index af492dc295a..cc965bc6eed 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -594,7 +594,7 @@ pub fn argument<'a, T>(f: extern "Rust" fn(&T, &mut Formatter) -> Result, } #[cfg(test)] -pub fn format(args: &Arguments) -> ~str { +pub fn format(args: &Arguments) -> ::realstd::strbuf::StrBuf { use str; use realstd::str::StrAllocating; use realstd::io::MemWriter; @@ -613,7 +613,10 @@ pub fn format(args: &Arguments) -> ~str { let mut i = MemWriter::new(); let _ = write(&mut i, args); - str::from_utf8(i.get_ref()).unwrap().to_owned() + + let mut result = ::realstd::strbuf::StrBuf::new(); + result.push_str(str::from_utf8(i.get_ref()).unwrap()); + result } /// When the compiler determines that the type of an argument *must* be a string @@ -761,7 +764,6 @@ macro_rules! delegate(($ty:ty to $other:ident) => { } } }) -delegate!(~str to string) delegate!(&'a str to string) delegate!(bool to bool) delegate!(char to char) diff --git a/src/libcore/fmt/num.rs b/src/libcore/fmt/num.rs index d9a32713781..75f67c3df65 100644 --- a/src/libcore/fmt/num.rs +++ b/src/libcore/fmt/num.rs @@ -194,7 +194,7 @@ mod tests { use fmt::radix; use super::{Binary, Octal, Decimal, LowerHex, UpperHex}; use super::{GenericRadix, Radix}; - use realstd::str::StrAllocating; + use realstd::str::{Str, StrAllocating}; #[test] fn test_radix_base() { @@ -246,143 +246,143 @@ mod tests { // Formatting integers should select the right implementation based off // the type of the argument. Also, hex/octal/binary should be defined // for integers, but they shouldn't emit the negative sign. - assert_eq!(format!("{}", 1i), "1".to_owned()); - assert_eq!(format!("{}", 1i8), "1".to_owned()); - assert_eq!(format!("{}", 1i16), "1".to_owned()); - assert_eq!(format!("{}", 1i32), "1".to_owned()); - assert_eq!(format!("{}", 1i64), "1".to_owned()); - assert_eq!(format!("{:d}", -1i), "-1".to_owned()); - assert_eq!(format!("{:d}", -1i8), "-1".to_owned()); - assert_eq!(format!("{:d}", -1i16), "-1".to_owned()); - assert_eq!(format!("{:d}", -1i32), "-1".to_owned()); - assert_eq!(format!("{:d}", -1i64), "-1".to_owned()); - assert_eq!(format!("{:t}", 1i), "1".to_owned()); - assert_eq!(format!("{:t}", 1i8), "1".to_owned()); - assert_eq!(format!("{:t}", 1i16), "1".to_owned()); - assert_eq!(format!("{:t}", 1i32), "1".to_owned()); - assert_eq!(format!("{:t}", 1i64), "1".to_owned()); - assert_eq!(format!("{:x}", 1i), "1".to_owned()); - assert_eq!(format!("{:x}", 1i8), "1".to_owned()); - assert_eq!(format!("{:x}", 1i16), "1".to_owned()); - assert_eq!(format!("{:x}", 1i32), "1".to_owned()); - assert_eq!(format!("{:x}", 1i64), "1".to_owned()); - assert_eq!(format!("{:X}", 1i), "1".to_owned()); - assert_eq!(format!("{:X}", 1i8), "1".to_owned()); - assert_eq!(format!("{:X}", 1i16), "1".to_owned()); - assert_eq!(format!("{:X}", 1i32), "1".to_owned()); - assert_eq!(format!("{:X}", 1i64), "1".to_owned()); - assert_eq!(format!("{:o}", 1i), "1".to_owned()); - assert_eq!(format!("{:o}", 1i8), "1".to_owned()); - assert_eq!(format!("{:o}", 1i16), "1".to_owned()); - assert_eq!(format!("{:o}", 1i32), "1".to_owned()); - assert_eq!(format!("{:o}", 1i64), "1".to_owned()); - - assert_eq!(format!("{}", 1u), "1".to_owned()); - assert_eq!(format!("{}", 1u8), "1".to_owned()); - assert_eq!(format!("{}", 1u16), "1".to_owned()); - assert_eq!(format!("{}", 1u32), "1".to_owned()); - assert_eq!(format!("{}", 1u64), "1".to_owned()); - assert_eq!(format!("{:u}", 1u), "1".to_owned()); - assert_eq!(format!("{:u}", 1u8), "1".to_owned()); - assert_eq!(format!("{:u}", 1u16), "1".to_owned()); - assert_eq!(format!("{:u}", 1u32), "1".to_owned()); - assert_eq!(format!("{:u}", 1u64), "1".to_owned()); - assert_eq!(format!("{:t}", 1u), "1".to_owned()); - assert_eq!(format!("{:t}", 1u8), "1".to_owned()); - assert_eq!(format!("{:t}", 1u16), "1".to_owned()); - assert_eq!(format!("{:t}", 1u32), "1".to_owned()); - assert_eq!(format!("{:t}", 1u64), "1".to_owned()); - assert_eq!(format!("{:x}", 1u), "1".to_owned()); - assert_eq!(format!("{:x}", 1u8), "1".to_owned()); - assert_eq!(format!("{:x}", 1u16), "1".to_owned()); - assert_eq!(format!("{:x}", 1u32), "1".to_owned()); - assert_eq!(format!("{:x}", 1u64), "1".to_owned()); - assert_eq!(format!("{:X}", 1u), "1".to_owned()); - assert_eq!(format!("{:X}", 1u8), "1".to_owned()); - assert_eq!(format!("{:X}", 1u16), "1".to_owned()); - assert_eq!(format!("{:X}", 1u32), "1".to_owned()); - assert_eq!(format!("{:X}", 1u64), "1".to_owned()); - assert_eq!(format!("{:o}", 1u), "1".to_owned()); - assert_eq!(format!("{:o}", 1u8), "1".to_owned()); - assert_eq!(format!("{:o}", 1u16), "1".to_owned()); - assert_eq!(format!("{:o}", 1u32), "1".to_owned()); - assert_eq!(format!("{:o}", 1u64), "1".to_owned()); + assert!(format!("{}", 1i).as_slice() == "1"); + assert!(format!("{}", 1i8).as_slice() == "1"); + assert!(format!("{}", 1i16).as_slice() == "1"); + assert!(format!("{}", 1i32).as_slice() == "1"); + assert!(format!("{}", 1i64).as_slice() == "1"); + assert!(format!("{:d}", -1i).as_slice() == "-1"); + assert!(format!("{:d}", -1i8).as_slice() == "-1"); + assert!(format!("{:d}", -1i16).as_slice() == "-1"); + assert!(format!("{:d}", -1i32).as_slice() == "-1"); + assert!(format!("{:d}", -1i64).as_slice() == "-1"); + assert!(format!("{:t}", 1i).as_slice() == "1"); + assert!(format!("{:t}", 1i8).as_slice() == "1"); + assert!(format!("{:t}", 1i16).as_slice() == "1"); + assert!(format!("{:t}", 1i32).as_slice() == "1"); + assert!(format!("{:t}", 1i64).as_slice() == "1"); + assert!(format!("{:x}", 1i).as_slice() == "1"); + assert!(format!("{:x}", 1i8).as_slice() == "1"); + assert!(format!("{:x}", 1i16).as_slice() == "1"); + assert!(format!("{:x}", 1i32).as_slice() == "1"); + assert!(format!("{:x}", 1i64).as_slice() == "1"); + assert!(format!("{:X}", 1i).as_slice() == "1"); + assert!(format!("{:X}", 1i8).as_slice() == "1"); + assert!(format!("{:X}", 1i16).as_slice() == "1"); + assert!(format!("{:X}", 1i32).as_slice() == "1"); + assert!(format!("{:X}", 1i64).as_slice() == "1"); + assert!(format!("{:o}", 1i).as_slice() == "1"); + assert!(format!("{:o}", 1i8).as_slice() == "1"); + assert!(format!("{:o}", 1i16).as_slice() == "1"); + assert!(format!("{:o}", 1i32).as_slice() == "1"); + assert!(format!("{:o}", 1i64).as_slice() == "1"); + + assert!(format!("{}", 1u).as_slice() == "1"); + assert!(format!("{}", 1u8).as_slice() == "1"); + assert!(format!("{}", 1u16).as_slice() == "1"); + assert!(format!("{}", 1u32).as_slice() == "1"); + assert!(format!("{}", 1u64).as_slice() == "1"); + assert!(format!("{:u}", 1u).as_slice() == "1"); + assert!(format!("{:u}", 1u8).as_slice() == "1"); + assert!(format!("{:u}", 1u16).as_slice() == "1"); + assert!(format!("{:u}", 1u32).as_slice() == "1"); + assert!(format!("{:u}", 1u64).as_slice() == "1"); + assert!(format!("{:t}", 1u).as_slice() == "1"); + assert!(format!("{:t}", 1u8).as_slice() == "1"); + assert!(format!("{:t}", 1u16).as_slice() == "1"); + assert!(format!("{:t}", 1u32).as_slice() == "1"); + assert!(format!("{:t}", 1u64).as_slice() == "1"); + assert!(format!("{:x}", 1u).as_slice() == "1"); + assert!(format!("{:x}", 1u8).as_slice() == "1"); + assert!(format!("{:x}", 1u16).as_slice() == "1"); + assert!(format!("{:x}", 1u32).as_slice() == "1"); + assert!(format!("{:x}", 1u64).as_slice() == "1"); + assert!(format!("{:X}", 1u).as_slice() == "1"); + assert!(format!("{:X}", 1u8).as_slice() == "1"); + assert!(format!("{:X}", 1u16).as_slice() == "1"); + assert!(format!("{:X}", 1u32).as_slice() == "1"); + assert!(format!("{:X}", 1u64).as_slice() == "1"); + assert!(format!("{:o}", 1u).as_slice() == "1"); + assert!(format!("{:o}", 1u8).as_slice() == "1"); + assert!(format!("{:o}", 1u16).as_slice() == "1"); + assert!(format!("{:o}", 1u32).as_slice() == "1"); + assert!(format!("{:o}", 1u64).as_slice() == "1"); // Test a larger number - assert_eq!(format!("{:t}", 55), "110111".to_owned()); - assert_eq!(format!("{:o}", 55), "67".to_owned()); - assert_eq!(format!("{:d}", 55), "55".to_owned()); - assert_eq!(format!("{:x}", 55), "37".to_owned()); - assert_eq!(format!("{:X}", 55), "37".to_owned()); + assert!(format!("{:t}", 55).as_slice() == "110111"); + assert!(format!("{:o}", 55).as_slice() == "67"); + assert!(format!("{:d}", 55).as_slice() == "55"); + assert!(format!("{:x}", 55).as_slice() == "37"); + assert!(format!("{:X}", 55).as_slice() == "37"); } #[test] fn test_format_int_zero() { - assert_eq!(format!("{}", 0i), "0".to_owned()); - assert_eq!(format!("{:d}", 0i), "0".to_owned()); - assert_eq!(format!("{:t}", 0i), "0".to_owned()); - assert_eq!(format!("{:o}", 0i), "0".to_owned()); - assert_eq!(format!("{:x}", 0i), "0".to_owned()); - assert_eq!(format!("{:X}", 0i), "0".to_owned()); - - assert_eq!(format!("{}", 0u), "0".to_owned()); - assert_eq!(format!("{:u}", 0u), "0".to_owned()); - assert_eq!(format!("{:t}", 0u), "0".to_owned()); - assert_eq!(format!("{:o}", 0u), "0".to_owned()); - assert_eq!(format!("{:x}", 0u), "0".to_owned()); - assert_eq!(format!("{:X}", 0u), "0".to_owned()); + assert!(format!("{}", 0i).as_slice() == "0"); + assert!(format!("{:d}", 0i).as_slice() == "0"); + assert!(format!("{:t}", 0i).as_slice() == "0"); + assert!(format!("{:o}", 0i).as_slice() == "0"); + assert!(format!("{:x}", 0i).as_slice() == "0"); + assert!(format!("{:X}", 0i).as_slice() == "0"); + + assert!(format!("{}", 0u).as_slice() == "0"); + assert!(format!("{:u}", 0u).as_slice() == "0"); + assert!(format!("{:t}", 0u).as_slice() == "0"); + assert!(format!("{:o}", 0u).as_slice() == "0"); + assert!(format!("{:x}", 0u).as_slice() == "0"); + assert!(format!("{:X}", 0u).as_slice() == "0"); } #[test] fn test_format_int_flags() { - assert_eq!(format!("{:3d}", 1), " 1".to_owned()); - assert_eq!(format!("{:>3d}", 1), " 1".to_owned()); - assert_eq!(format!("{:>+3d}", 1), " +1".to_owned()); - assert_eq!(format!("{:<3d}", 1), "1 ".to_owned()); - assert_eq!(format!("{:#d}", 1), "1".to_owned()); - assert_eq!(format!("{:#x}", 10), "0xa".to_owned()); - assert_eq!(format!("{:#X}", 10), "0xA".to_owned()); - assert_eq!(format!("{:#5x}", 10), " 0xa".to_owned()); - assert_eq!(format!("{:#o}", 10), "0o12".to_owned()); - assert_eq!(format!("{:08x}", 10), "0000000a".to_owned()); - assert_eq!(format!("{:8x}", 10), " a".to_owned()); - assert_eq!(format!("{:<8x}", 10), "a ".to_owned()); - assert_eq!(format!("{:>8x}", 10), " a".to_owned()); - assert_eq!(format!("{:#08x}", 10), "0x00000a".to_owned()); - assert_eq!(format!("{:08d}", -10), "-0000010".to_owned()); - assert_eq!(format!("{:x}", -1u8), "ff".to_owned()); - assert_eq!(format!("{:X}", -1u8), "FF".to_owned()); - assert_eq!(format!("{:t}", -1u8), "11111111".to_owned()); - assert_eq!(format!("{:o}", -1u8), "377".to_owned()); - assert_eq!(format!("{:#x}", -1u8), "0xff".to_owned()); - assert_eq!(format!("{:#X}", -1u8), "0xFF".to_owned()); - assert_eq!(format!("{:#t}", -1u8), "0b11111111".to_owned()); - assert_eq!(format!("{:#o}", -1u8), "0o377".to_owned()); + assert!(format!("{:3d}", 1).as_slice() == " 1"); + assert!(format!("{:>3d}", 1).as_slice() == " 1"); + assert!(format!("{:>+3d}", 1).as_slice() == " +1"); + assert!(format!("{:<3d}", 1).as_slice() == "1 "); + assert!(format!("{:#d}", 1).as_slice() == "1"); + assert!(format!("{:#x}", 10).as_slice() == "0xa"); + assert!(format!("{:#X}", 10).as_slice() == "0xA"); + assert!(format!("{:#5x}", 10).as_slice() == " 0xa"); + assert!(format!("{:#o}", 10).as_slice() == "0o12"); + assert!(format!("{:08x}", 10).as_slice() == "0000000a"); + assert!(format!("{:8x}", 10).as_slice() == " a"); + assert!(format!("{:<8x}", 10).as_slice() == "a "); + assert!(format!("{:>8x}", 10).as_slice() == " a"); + assert!(format!("{:#08x}", 10).as_slice() == "0x00000a"); + assert!(format!("{:08d}", -10).as_slice() == "-0000010"); + assert!(format!("{:x}", -1u8).as_slice() == "ff"); + assert!(format!("{:X}", -1u8).as_slice() == "FF"); + assert!(format!("{:t}", -1u8).as_slice() == "11111111"); + assert!(format!("{:o}", -1u8).as_slice() == "377"); + assert!(format!("{:#x}", -1u8).as_slice() == "0xff"); + assert!(format!("{:#X}", -1u8).as_slice() == "0xFF"); + assert!(format!("{:#t}", -1u8).as_slice() == "0b11111111"); + assert!(format!("{:#o}", -1u8).as_slice() == "0o377"); } #[test] fn test_format_int_sign_padding() { - assert_eq!(format!("{:+5d}", 1), " +1".to_owned()); - assert_eq!(format!("{:+5d}", -1), " -1".to_owned()); - assert_eq!(format!("{:05d}", 1), "00001".to_owned()); - assert_eq!(format!("{:05d}", -1), "-0001".to_owned()); - assert_eq!(format!("{:+05d}", 1), "+0001".to_owned()); - assert_eq!(format!("{:+05d}", -1), "-0001".to_owned()); + assert!(format!("{:+5d}", 1).as_slice() == " +1"); + assert!(format!("{:+5d}", -1).as_slice() == " -1"); + assert!(format!("{:05d}", 1).as_slice() == "00001"); + assert!(format!("{:05d}", -1).as_slice() == "-0001"); + assert!(format!("{:+05d}", 1).as_slice() == "+0001"); + assert!(format!("{:+05d}", -1).as_slice() == "-0001"); } #[test] fn test_format_int_twos_complement() { use {i8, i16, i32, i64}; - assert_eq!(format!("{}", i8::MIN), "-128".to_owned()); - assert_eq!(format!("{}", i16::MIN), "-32768".to_owned()); - assert_eq!(format!("{}", i32::MIN), "-2147483648".to_owned()); - assert_eq!(format!("{}", i64::MIN), "-9223372036854775808".to_owned()); + assert!(format!("{}", i8::MIN).as_slice() == "-128"); + assert!(format!("{}", i16::MIN).as_slice() == "-32768"); + assert!(format!("{}", i32::MIN).as_slice() == "-2147483648"); + assert!(format!("{}", i64::MIN).as_slice() == "-9223372036854775808"); } #[test] fn test_format_radix() { - assert_eq!(format!("{:04}", radix(3, 2)), "0011".to_owned()); - assert_eq!(format!("{}", radix(55, 36)), "1j".to_owned()); + assert!(format!("{:04}", radix(3, 2)).as_slice() == "0011"); + assert!(format!("{}", radix(55, 36)).as_slice() == "1j"); } #[test] diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index aa7a8f0f8b6..b72eebe85c5 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -469,6 +469,7 @@ mod tests { use option::{Some,None}; use realstd::str::StrAllocating; use realstd::owned::Box; + use realstd::vec::Vec; use raw; #[test] @@ -568,7 +569,7 @@ mod tests { } unsafe { - assert_eq!(box [76u8], transmute("L".to_owned())); + assert!(Vec::from_slice([76u8]) == transmute("L".to_owned())); } } } diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 00f21ee4c9c..adea8ac630e 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -188,14 +188,14 @@ impl Option { /// /// # Example /// - /// Convert an `Option<~str>` into an `Option`, preserving the original. + /// Convert an `Option` into an `Option`, preserving the original. /// The `map` method takes the `self` argument by value, consuming the original, /// so this technique uses `as_ref` to first take an `Option` to a reference /// to the value inside the original. /// /// ``` - /// let num_as_str: Option<~str> = Some("10".to_owned()); - /// // First, cast `Option<~str>` to `Option<&~str>` with `as_ref`, + /// let num_as_str: Option = Some("10".to_strbuf()); + /// // First, cast `Option` to `Option<&StrBuf>` with `as_ref`, /// // then consume *that* with `map`, leaving `num_as_str` on the stack. /// let num_as_int: Option = num_as_str.as_ref().map(|n| n.len()); /// println!("still can print num_as_str: {}", num_as_str); @@ -278,10 +278,10 @@ impl Option { /// /// # Example /// - /// Convert an `Option<~str>` into an `Option`, consuming the original: + /// Convert an `Option` into an `Option`, consuming the original: /// /// ``` - /// let num_as_str: Option<~str> = Some("10".to_owned()); + /// let num_as_str: Option = Some("10".to_strbuf()); /// // `Option::map` takes self *by value*, consuming `num_as_str` /// let num_as_int: Option = num_as_str.map(|n| n.len()); /// ``` @@ -596,9 +596,10 @@ pub fn collect>, V: FromIterator>(iter: Iter) -> #[cfg(test)] mod tests { use realstd::vec::Vec; - use realstd::str::StrAllocating; + use realstd::strbuf::StrBuf; use option::collect; use prelude::*; + use realstd::str::{Str, StrAllocating}; use iter::range; use str::StrSlice; @@ -619,11 +620,11 @@ mod tests { #[test] fn test_get_str() { - let x = "test".to_owned(); - let addr_x = x.as_ptr(); + let x = "test".to_strbuf(); + let addr_x = x.as_slice().as_ptr(); let opt = Some(x); let y = opt.unwrap(); - let addr_y = y.as_ptr(); + let addr_y = y.as_slice().as_ptr(); assert_eq!(addr_x, addr_y); } @@ -745,7 +746,8 @@ mod tests { #[test] fn test_unwrap() { assert_eq!(Some(1).unwrap(), 1); - assert_eq!(Some("hello".to_owned()).unwrap(), "hello".to_owned()); + let s = Some("hello".to_strbuf()).unwrap(); + assert_eq!(s.as_slice(), "hello"); } #[test] @@ -758,7 +760,7 @@ mod tests { #[test] #[should_fail] fn test_unwrap_fail2() { - let x: Option<~str> = None; + let x: Option = None; x.unwrap(); } diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 870fa0ec53f..90b5b0d8753 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -486,6 +486,7 @@ pub mod ptr_tests { use mem; use libc; use realstd::str; + use realstd::str::Str; use slice::{ImmutableVector, MutableVector}; #[test] @@ -660,7 +661,7 @@ pub mod ptr_tests { let expected = expected_arr[ctr].with_ref(|buf| { str::raw::from_c_str(buf) }); - assert_eq!(actual, expected); + assert_eq!(actual.as_slice(), expected.as_slice()); ctr += 1; iteration_count += 1; }); @@ -693,7 +694,7 @@ pub mod ptr_tests { let expected = expected_arr[ctr].with_ref(|buf| { str::raw::from_c_str(buf) }); - assert_eq!(actual, expected); + assert_eq!(actual.as_slice(), expected.as_slice()); ctr += 1; iteration_count += 1; }); diff --git a/src/libcore/raw.rs b/src/libcore/raw.rs index 25f40b358ab..979eeb657b6 100644 --- a/src/libcore/raw.rs +++ b/src/libcore/raw.rs @@ -81,7 +81,6 @@ impl<'a, T> Repr> for &'a [T] {} impl<'a> Repr> for &'a str {} impl Repr<*Box> for @T {} impl Repr<*Vec> for ~[T] {} -impl Repr<*String> for ~str {} #[cfg(test)] mod tests { diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 46f4427e838..98d1782f20f 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -169,20 +169,24 @@ //! ~~~ //! use std::io::{File, Open, Write, IoError}; //! -//! struct Info { name: ~str, age: int, rating: int } +//! struct Info { +//! name: StrBuf, +//! age: int, +//! rating: int +//! } //! //! fn write_info(info: &Info) -> Result<(), IoError> { //! let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write); //! // Early return on error -//! match file.write_line(format!("name: {}", info.name)) { +//! match file.write_line(format!("name: {}", info.name).as_slice()) { //! Ok(_) => (), //! Err(e) => return Err(e) //! } -//! match file.write_line(format!("age: {}", info.age)) { +//! match file.write_line(format!("age: {}", info.age).as_slice()) { //! Ok(_) => (), //! Err(e) => return Err(e) //! } -//! return file.write_line(format!("rating: {}", info.rating)); +//! return file.write_line(format!("rating: {}", info.rating).as_slice()); //! } //! ~~~ //! @@ -191,14 +195,18 @@ //! ~~~ //! use std::io::{File, Open, Write, IoError}; //! -//! struct Info { name: ~str, age: int, rating: int } +//! struct Info { +//! name: StrBuf, +//! age: int, +//! rating: int +//! } //! //! fn write_info(info: &Info) -> Result<(), IoError> { //! let mut file = File::open_mode(&Path::new("my_best_friends.txt"), Open, Write); //! // Early return on error -//! try!(file.write_line(format!("name: {}", info.name))); -//! try!(file.write_line(format!("age: {}", info.age))); -//! try!(file.write_line(format!("rating: {}", info.rating))); +//! try!(file.write_line(format!("name: {}", info.name).as_slice())); +//! try!(file.write_line(format!("age: {}", info.age).as_slice())); +//! try!(file.write_line(format!("rating: {}", info.rating).as_slice())); //! return Ok(()); //! } //! ~~~ @@ -421,10 +429,10 @@ impl Result { /// let mut sum = 0; /// /// while !reader.eof() { - /// let line: IoResult<~str> = reader.read_line(); + /// let line: IoResult = reader.read_line(); /// // Convert the string line to a number using `map` and `from_str` /// let val: IoResult = line.map(|line| { - /// from_str::(line).unwrap_or(0) + /// from_str::(line.as_slice()).unwrap_or(0) /// }); /// // Add the value if there were no errors, otherwise add 0 /// sum += val.ok().unwrap_or(0); @@ -629,69 +637,68 @@ pub fn fold_>>(iterator: Iter) -> Result<(),E> { #[cfg(test)] mod tests { use realstd::vec::Vec; - use realstd::str::StrAllocating; + use realstd::strbuf::StrBuf; use result::{collect, fold, fold_}; use prelude::*; + use realstd::str::{Str, StrAllocating}; use iter::range; - pub fn op1() -> Result { Ok(666) } - pub fn op2() -> Result { Err("sadface".to_owned()) } + pub fn op1() -> Result { Ok(666) } + pub fn op2() -> Result { Err("sadface") } #[test] pub fn test_and() { assert_eq!(op1().and(Ok(667)).unwrap(), 667); - assert_eq!(op1().and(Err::<(), ~str>("bad".to_owned())).unwrap_err(), "bad".to_owned()); + assert_eq!(op1().and(Err::<(), &'static str>("bad")).unwrap_err(), + "bad"); - assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface".to_owned()); - assert_eq!(op2().and(Err::<(), ~str>("bad".to_owned())).unwrap_err(), "sadface".to_owned()); + assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface"); + assert_eq!(op2().and(Err::<(),&'static str>("bad")).unwrap_err(), + "sadface"); } #[test] pub fn test_and_then() { - assert_eq!(op1().and_then(|i| Ok::(i + 1)).unwrap(), 667); - assert_eq!(op1().and_then(|_| Err::("bad".to_owned())).unwrap_err(), - "bad".to_owned()); - - assert_eq!(op2().and_then(|i| Ok::(i + 1)).unwrap_err(), - "sadface".to_owned()); - assert_eq!(op2().and_then(|_| Err::("bad".to_owned())).unwrap_err(), - "sadface".to_owned()); + assert_eq!(op1().and_then(|i| Ok::(i + 1)).unwrap(), 667); + assert_eq!(op1().and_then(|_| Err::("bad")).unwrap_err(), + "bad"); + + assert_eq!(op2().and_then(|i| Ok::(i + 1)).unwrap_err(), + "sadface"); + assert_eq!(op2().and_then(|_| Err::("bad")).unwrap_err(), + "sadface"); } #[test] pub fn test_or() { assert_eq!(op1().or(Ok(667)).unwrap(), 666); - assert_eq!(op1().or(Err("bad".to_owned())).unwrap(), 666); + assert_eq!(op1().or(Err("bad")).unwrap(), 666); assert_eq!(op2().or(Ok(667)).unwrap(), 667); - assert_eq!(op2().or(Err("bad".to_owned())).unwrap_err(), "bad".to_owned()); + assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad"); } #[test] pub fn test_or_else() { - assert_eq!(op1().or_else(|_| Ok::(667)).unwrap(), 666); - assert_eq!(op1().or_else(|e| Err::(e + "!")).unwrap(), 666); + assert_eq!(op1().or_else(|_| Ok::(667)).unwrap(), 666); + assert_eq!(op1().or_else(|e| Err::(e)).unwrap(), 666); - assert_eq!(op2().or_else(|_| Ok::(667)).unwrap(), 667); - assert_eq!(op2().or_else(|e| Err::(e + "!")).unwrap_err(), - "sadface!".to_owned()); + assert_eq!(op2().or_else(|_| Ok::(667)).unwrap(), 667); + assert_eq!(op2().or_else(|e| Err::(e)).unwrap_err(), + "sadface"); } #[test] pub fn test_impl_map() { - assert_eq!(Ok::<~str, ~str>("a".to_owned()).map(|x| x + "b"), - Ok("ab".to_owned())); - assert_eq!(Err::<~str, ~str>("a".to_owned()).map(|x| x + "b"), - Err("a".to_owned())); + assert!(Ok::(1).map(|x| x + 1) == Ok(2)); + assert!(Err::(1).map(|x| x + 1) == Err(1)); } #[test] pub fn test_impl_map_err() { - assert_eq!(Ok::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), - Ok("a".to_owned())); - assert_eq!(Err::<~str, ~str>("a".to_owned()).map_err(|x| x + "b"), - Err("ab".to_owned())); + assert!(Ok::(1).map_err(|x| x + 1) == Ok(1)); + assert!(Err::(1).map_err(|x| x + 1) == Err(2)); } #[test] @@ -736,17 +743,19 @@ mod tests { #[test] pub fn test_fmt_default() { - let ok: Result = Ok(100); - let err: Result = Err("Err".to_owned()); + let ok: Result = Ok(100); + let err: Result = Err("Err"); - assert_eq!(format!("{}", ok), "Ok(100)".to_owned()); - assert_eq!(format!("{}", err), "Err(Err)".to_owned()); + let s = format!("{}", ok); + assert_eq!(s.as_slice(), "Ok(100)"); + let s = format!("{}", err); + assert_eq!(s.as_slice(), "Err(Err)"); } #[test] pub fn test_unwrap_or() { - let ok: Result = Ok(100); - let ok_err: Result = Err("Err".to_owned()); + let ok: Result = Ok(100); + let ok_err: Result = Err("Err"); assert_eq!(ok.unwrap_or(50), 100); assert_eq!(ok_err.unwrap_or(50), 50); @@ -754,16 +763,16 @@ mod tests { #[test] pub fn test_unwrap_or_else() { - fn handler(msg: ~str) -> int { - if msg == "I got this.".to_owned() { + fn handler(msg: &'static str) -> int { + if msg == "I got this." { 50 } else { fail!("BadBad") } } - let ok: Result = Ok(100); - let ok_err: Result = Err("I got this.".to_owned()); + let ok: Result = Ok(100); + let ok_err: Result = Err("I got this."); assert_eq!(ok.unwrap_or_else(handler), 100); assert_eq!(ok_err.unwrap_or_else(handler), 50); @@ -772,15 +781,15 @@ mod tests { #[test] #[should_fail] pub fn test_unwrap_or_else_failure() { - fn handler(msg: ~str) -> int { - if msg == "I got this.".to_owned() { + fn handler(msg: &'static str) -> int { + if msg == "I got this." { 50 } else { fail!("BadBad") } } - let bad_err: Result = Err("Unrecoverable mess.".to_owned()); + let bad_err: Result = Err("Unrecoverable mess."); let _ : int = bad_err.unwrap_or_else(handler); } } diff --git a/src/libcore/should_not_exist.rs b/src/libcore/should_not_exist.rs index 2046017869d..9a0e3ad7ca4 100644 --- a/src/libcore/should_not_exist.rs +++ b/src/libcore/should_not_exist.rs @@ -10,9 +10,9 @@ // As noted by this file name, this file should not exist. This file should not // exist because it performs allocations which libcore is not allowed to do. The -// reason for this file's existence is that the `~[T]` and `~str` types are -// language-defined types. Traits are defined in libcore, such as `Clone`, which -// these types need to implement, but the implementation can only be found in +// reason for this file's existence is that the `~[T]` type is a language- +// defined type. Traits are defined in libcore, such as `Clone`, which these +// types need to implement, but the implementation can only be found in // libcore. // // Plan of attack for solving this problem: @@ -24,13 +24,11 @@ // // Currently, no progress has been made on this list. -use char::Char; use clone::Clone; use container::Container; -use default::Default; use finally::try_finally; use intrinsics; -use iter::{range, Iterator, FromIterator}; +use iter::{range, Iterator}; use mem; use num::{CheckedMul, CheckedAdd}; use option::{Some, None}; @@ -38,9 +36,6 @@ use ptr::RawPtr; use ptr; use raw::Vec; use slice::ImmutableVector; -use str::StrSlice; - -#[cfg(not(test))] use ops::Add; #[allow(ctypes)] extern { @@ -60,105 +55,6 @@ unsafe fn alloc(cap: uint) -> *mut Vec<()> { ret } -// Strings - -impl Default for ~str { - fn default() -> ~str { - unsafe { - // Get some memory - let ptr = alloc(0); - - // Initialize the memory - (*ptr).fill = 0; - (*ptr).alloc = 0; - - mem::transmute(ptr) - } - } -} - -impl Clone for ~str { - fn clone(&self) -> ~str { - // Don't use the clone() implementation above because it'll start - // requiring the eh_personality lang item (no fun) - unsafe { - let bytes = self.as_bytes().as_ptr(); - let len = self.len(); - - let ptr = alloc(len) as *mut Vec; - ptr::copy_nonoverlapping_memory(&mut (*ptr).data, bytes, len); - (*ptr).fill = len; - (*ptr).alloc = len; - - mem::transmute(ptr) - } - } -} - -impl FromIterator for ~str { - #[inline] - fn from_iter>(mut iterator: T) -> ~str { - let (lower, _) = iterator.size_hint(); - let mut cap = if lower == 0 {16} else {lower}; - let mut len = 0; - let mut tmp = [0u8, ..4]; - - unsafe { - let mut ptr = alloc(cap) as *mut Vec; - let mut ret = mem::transmute(ptr); - for ch in iterator { - let amt = ch.encode_utf8(tmp); - - if len + amt > cap { - cap = cap.checked_mul(&2).unwrap(); - if cap < len + amt { - cap = len + amt; - } - let ptr2 = alloc(cap) as *mut Vec; - ptr::copy_nonoverlapping_memory(&mut (*ptr2).data, - &(*ptr).data, - len); - // FIXME: #13994: port to the sized deallocation API when available - rust_deallocate(ptr as *u8, 0, 8); - mem::forget(ret); - ret = mem::transmute(ptr2); - ptr = ptr2; - } - - let base = &mut (*ptr).data as *mut u8; - for byte in tmp.slice_to(amt).iter() { - *base.offset(len as int) = *byte; - len += 1; - } - (*ptr).fill = len; - } - ret - } - } -} - -#[cfg(not(test))] -impl<'a> Add<&'a str,~str> for &'a str { - #[inline] - fn add(&self, rhs: & &'a str) -> ~str { - let amt = self.len().checked_add(&rhs.len()).unwrap(); - unsafe { - let ptr = alloc(amt) as *mut Vec; - let base = &mut (*ptr).data as *mut _; - ptr::copy_nonoverlapping_memory(base, - self.as_bytes().as_ptr(), - self.len()); - let base = base.offset(self.len() as int); - ptr::copy_nonoverlapping_memory(base, - rhs.as_bytes().as_ptr(), - rhs.len()); - (*ptr).fill = amt; - (*ptr).alloc = amt; - mem::transmute(ptr) - } - } -} - // Arrays impl Clone for ~[A] { diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 19be2fd3169..0b264b1724d 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -25,7 +25,7 @@ use iter::range; use num::Saturating; use option::{None, Option, Some}; use raw::Repr; -use slice::{ImmutableVector, Vector}; +use slice::ImmutableVector; use slice; use uint; @@ -596,20 +596,6 @@ pub fn eq_slice(a: &str, b: &str) -> bool { eq_slice_(a, b) } -/// Bytewise string equality -#[cfg(not(test))] -#[lang="uniq_str_eq"] -#[inline] -pub fn eq(a: &~str, b: &~str) -> bool { - eq_slice(*a, *b) -} - -#[cfg(test)] -#[inline] -pub fn eq(a: &~str, b: &~str) -> bool { - eq_slice(*a, *b) -} - /* Section: Misc */ @@ -976,11 +962,6 @@ pub mod traits { } } - impl TotalOrd for ~str { - #[inline] - fn cmp(&self, other: &~str) -> Ordering { self.as_slice().cmp(&other.as_slice()) } - } - impl<'a> Eq for &'a str { #[inline] fn eq(&self, other: & &'a str) -> bool { @@ -990,36 +971,17 @@ pub mod traits { fn ne(&self, other: & &'a str) -> bool { !(*self).eq(other) } } - impl Eq for ~str { - #[inline] - fn eq(&self, other: &~str) -> bool { - eq_slice((*self), (*other)) - } - } - impl<'a> TotalEq for &'a str {} - impl TotalEq for ~str {} - impl<'a> Ord for &'a str { #[inline] fn lt(&self, other: & &'a str) -> bool { self.cmp(other) == Less } } - impl Ord for ~str { - #[inline] - fn lt(&self, other: &~str) -> bool { self.cmp(other) == Less } - } - impl<'a, S: Str> Equiv for &'a str { #[inline] fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) } } - - impl<'a, S: Str> Equiv for ~str { - #[inline] - fn equiv(&self, other: &S) -> bool { eq_slice(*self, other.as_slice()) } - } } #[cfg(test)] @@ -1036,11 +998,6 @@ impl<'a> Str for &'a str { fn as_slice<'a>(&'a self) -> &'a str { *self } } -impl<'a> Str for ~str { - #[inline] - fn as_slice<'a>(&'a self) -> &'a str { let s: &'a str = *self; s } -} - impl<'a> Container for &'a str { #[inline] fn len(&self) -> uint { @@ -1048,11 +1005,6 @@ impl<'a> Container for &'a str { } } -impl Container for ~str { - #[inline] - fn len(&self) -> uint { self.as_slice().len() } -} - /// Methods for string slices pub trait StrSlice<'a> { /// Returns true if one string contains another diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs index b73d85489a3..0e21c95807d 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -246,11 +246,11 @@ mod tests { use super::*; use clone::Clone; use cmp::*; - use realstd::str::StrAllocating; + use realstd::str::{Str, StrAllocating}; #[test] fn test_clone() { - let a = (1, "2".to_owned()); + let a = (1, "2"); let b = a.clone(); assert_eq!(a, b); } @@ -323,8 +323,11 @@ mod tests { #[test] fn test_show() { - assert_eq!(format!("{}", (1,)), "(1,)".to_owned()); - assert_eq!(format!("{}", (1, true)), "(1, true)".to_owned()); - assert_eq!(format!("{}", (1, "hi".to_owned(), true)), "(1, hi, true)".to_owned()); + let s = format!("{}", (1,)); + assert_eq!(s.as_slice(), "(1,)"); + let s = format!("{}", (1, true)); + assert_eq!(s.as_slice(), "(1, true)"); + let s = format!("{}", (1, "hi", true)); + assert_eq!(s.as_slice(), "(1, hi, true)"); } } -- cgit 1.4.1-3-g733a5