diff --git a/src/compiletest/procsrv.rs b/src/compiletest/procsrv.rs index 1ee6f2b500c13..28ff2c18ad3ba 100644 --- a/src/compiletest/procsrv.rs +++ b/src/compiletest/procsrv.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::str; use std::io::process::{ProcessExit, Command, Process, ProcessOutput}; use std::dynamic_lib::DynamicLibrary; @@ -25,7 +24,7 @@ fn add_target_env(cmd: &mut Command, lib_path: &str, aux_path: Option<&str>) { // Add the new dylib search path var let var = DynamicLibrary::envvar(); let newpath = DynamicLibrary::create_path(path.as_slice()); - let newpath = str::from_utf8(newpath.as_slice()).unwrap().to_string(); + let newpath = String::from_utf8(newpath).unwrap(); cmd.env(var.to_string(), newpath); } @@ -55,8 +54,8 @@ pub fn run(lib_path: &str, Some(Result { status: status, - out: str::from_utf8(output.as_slice()).unwrap().to_string(), - err: str::from_utf8(error.as_slice()).unwrap().to_string() + out: String::from_utf8(output).unwrap(), + err: String::from_utf8(error).unwrap() }) }, Err(..) => None diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index f28604908e0b3..0c325a0d65aef 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -158,7 +158,7 @@ fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) { match props.pp_exact { Some(_) => 1, None => 2 }; let src = File::open(testfile).read_to_end().unwrap(); - let src = str::from_utf8(src.as_slice()).unwrap().to_string(); + let src = String::from_utf8(src.clone()).unwrap(); let mut srcs = vec!(src); let mut round = 0; @@ -185,10 +185,10 @@ fn run_pretty_test(config: &Config, props: &TestProps, testfile: &Path) { Some(ref file) => { let filepath = testfile.dir_path().join(file); let s = File::open(&filepath).read_to_end().unwrap(); - str::from_utf8(s.as_slice()).unwrap().to_string() - } - None => { (*srcs.get(srcs.len() - 2u)).clone() } - }; + String::from_utf8(s).unwrap() + } + None => { (*srcs.get(srcs.len() - 2u)).clone() } + }; let mut actual = (*srcs.get(srcs.len() - 1u)).clone(); if props.pp_exact.is_some() { @@ -582,8 +582,8 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path) process.wait_with_output().unwrap(); (status, - str::from_utf8(output.as_slice()).unwrap().to_string(), - str::from_utf8(error.as_slice()).unwrap().to_string()) + String::from_utf8(output).unwrap(), + String::from_utf8(error).unwrap()) }, Err(e) => { fatal(format!("Failed to setup Python process for \ @@ -813,7 +813,7 @@ fn check_expected_errors(expected_errors: Vec , c } } ).collect(); - str::from_chars(c.as_slice()).to_string() + String::from_chars(c.as_slice()) } #[cfg(target_os = "win32")] diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 19db88453809f..c2bde31b85981 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -94,66 +94,27 @@ pub use unicode::{Words, UnicodeStrSlice}; Section: Creating a string */ -/// Consumes a vector of bytes to create a new utf-8 string. -/// -/// Returns `Err` with the original vector if the vector contains invalid -/// UTF-8. -/// -/// # Example -/// -/// ```rust -/// use std::str; -/// let hello_vec = vec![104, 101, 108, 108, 111]; -/// let string = str::from_utf8_owned(hello_vec); -/// assert_eq!(string, Ok("hello".to_string())); -/// ``` +/// Deprecated. Replaced by `String::from_utf8` +#[deprecated = "Replaced by `String::from_utf8`"] pub fn from_utf8_owned(vv: Vec) -> Result> { String::from_utf8(vv) } -/// Convert a byte to a UTF-8 string -/// -/// # Failure -/// -/// Fails if invalid UTF-8 -/// -/// # Example -/// -/// ```rust -/// use std::str; -/// let string = str::from_byte(104); -/// assert_eq!(string.as_slice(), "h"); -/// ``` +/// Deprecated. Replaced by `String::from_byte` +#[deprecated = "Replaced by String::from_byte"] pub fn from_byte(b: u8) -> String { assert!(b < 128u8); String::from_char(1, b as char) } -/// Convert a char to a string -/// -/// # Example -/// -/// ```rust -/// use std::str; -/// let string = str::from_char('b'); -/// assert_eq!(string.as_slice(), "b"); -/// ``` +/// Deprecated. Use `String::from_char` or `char::to_string()` instead +#[deprecated = "use String::from_char or char.to_string()"] pub fn from_char(ch: char) -> String { - let mut buf = String::new(); - buf.push_char(ch); - buf + String::from_char(1, ch) } -/// Convert a vector of chars to a string -/// -/// # Example -/// -/// ```rust -/// use std::str; -/// let chars = ['h', 'e', 'l', 'l', 'o']; -/// let string = str::from_chars(chars); -/// assert_eq!(string.as_slice(), "hello"); -/// ``` +/// Deprecated. Replaced by `String::from_chars` +#[deprecated = "use String::from_chars instead"] pub fn from_chars(chs: &[char]) -> String { chs.iter().map(|c| *c).collect() } @@ -377,51 +338,16 @@ pub fn replace(s: &str, from: &str, to: &str) -> String { Section: Misc */ -/// Decode a UTF-16 encoded vector `v` into a string, returning `None` -/// if `v` contains any invalid data. -/// -/// # Example -/// -/// ```rust -/// use std::str; -/// -/// // ๐„žmusic -/// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075, -/// 0x0073, 0x0069, 0x0063]; -/// assert_eq!(str::from_utf16(v), Some("๐„žmusic".to_string())); -/// -/// // ๐„žmuic -/// v[4] = 0xD800; -/// assert_eq!(str::from_utf16(v), None); -/// ``` +/// Deprecated. Use `String::from_utf16`. +#[deprecated = "Replaced by String::from_utf16"] pub fn from_utf16(v: &[u16]) -> Option { - let mut s = String::with_capacity(v.len() / 2); - for c in utf16_items(v) { - match c { - ScalarValue(c) => s.push_char(c), - LoneSurrogate(_) => return None - } - } - Some(s) + String::from_utf16(v) } -/// Decode a UTF-16 encoded vector `v` into a string, replacing -/// invalid data with the replacement character (U+FFFD). -/// -/// # Example -/// ```rust -/// use std::str; -/// -/// // ๐„žmusic -/// let v = [0xD834, 0xDD1E, 0x006d, 0x0075, -/// 0x0073, 0xDD1E, 0x0069, 0x0063, -/// 0xD834]; -/// -/// assert_eq!(str::from_utf16_lossy(v), -/// "๐„žmus\uFFFDic\uFFFD".to_string()); -/// ``` +/// Deprecated. Use `String::from_utf16_lossy`. +#[deprecated = "Replaced by String::from_utf16_lossy"] pub fn from_utf16_lossy(v: &[u16]) -> String { - utf16_items(v).map(|c| c.to_char_lossy()).collect() + String::from_utf16_lossy(v) } // Return the initial codepoint accumulator for the first byte. @@ -436,131 +362,10 @@ macro_rules! utf8_acc_cont_byte( ($ch:expr, $byte:expr) => (($ch << 6) | ($byte & 63u8) as u32) ) -static TAG_CONT_U8: u8 = 128u8; - -/// Converts a vector of bytes to a new utf-8 string. -/// Any invalid utf-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER. -/// -/// # Example -/// -/// ```rust -/// let input = b"Hello \xF0\x90\x80World"; -/// let output = std::str::from_utf8_lossy(input); -/// assert_eq!(output.as_slice(), "Hello \uFFFDWorld"); -/// ``` +/// Deprecated. Use `String::from_utf8_lossy`. +#[deprecated = "Replaced by String::from_utf8_lossy"] pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a> { - if is_utf8(v) { - return Slice(unsafe { mem::transmute(v) }) - } - - static REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8 - let mut i = 0; - let total = v.len(); - fn unsafe_get(xs: &[u8], i: uint) -> u8 { - unsafe { *xs.unsafe_ref(i) } - } - fn safe_get(xs: &[u8], i: uint, total: uint) -> u8 { - if i >= total { - 0 - } else { - unsafe_get(xs, i) - } - } - - let mut res = String::with_capacity(total); - - if i > 0 { - unsafe { - res.push_bytes(v.slice_to(i)) - }; - } - - // subseqidx is the index of the first byte of the subsequence we're looking at. - // It's used to copy a bunch of contiguous good codepoints at once instead of copying - // them one by one. - let mut subseqidx = 0; - - while i < total { - let i_ = i; - let byte = unsafe_get(v, i); - i += 1; - - macro_rules! error(() => ({ - unsafe { - if subseqidx != i_ { - res.push_bytes(v.slice(subseqidx, i_)); - } - subseqidx = i; - res.push_bytes(REPLACEMENT); - } - })) - - if byte < 128u8 { - // subseqidx handles this - } else { - let w = utf8_char_width(byte); - - match w { - 2 => { - if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { - error!(); - continue; - } - i += 1; - } - 3 => { - match (byte, safe_get(v, i, total)) { - (0xE0 , 0xA0 .. 0xBF) => (), - (0xE1 .. 0xEC, 0x80 .. 0xBF) => (), - (0xED , 0x80 .. 0x9F) => (), - (0xEE .. 0xEF, 0x80 .. 0xBF) => (), - _ => { - error!(); - continue; - } - } - i += 1; - if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { - error!(); - continue; - } - i += 1; - } - 4 => { - match (byte, safe_get(v, i, total)) { - (0xF0 , 0x90 .. 0xBF) => (), - (0xF1 .. 0xF3, 0x80 .. 0xBF) => (), - (0xF4 , 0x80 .. 0x8F) => (), - _ => { - error!(); - continue; - } - } - i += 1; - if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { - error!(); - continue; - } - i += 1; - if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { - error!(); - continue; - } - i += 1; - } - _ => { - error!(); - continue; - } - } - } - } - if subseqidx < total { - unsafe { - res.push_bytes(v.slice(subseqidx, total)) - }; - } - Owned(res.into_string()) + String::from_utf8_lossy(v) } /* @@ -804,7 +609,6 @@ pub mod raw { #[test] fn test_from_buf_len() { use slice::ImmutableVector; - use str::StrAllocating; unsafe { let a = vec![65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 0u8]; @@ -1009,8 +813,7 @@ mod tests { use std::default::Default; use std::char::Char; use std::clone::Clone; - use std::cmp::{Equal, Greater, Less, Ord, Eq, PartialOrd, PartialEq, Equiv}; - use std::result::{Ok, Err}; + use std::cmp::{Equal, Greater, Less, Ord, PartialOrd, Equiv}; use std::option::{Some, None}; use std::ptr::RawPtr; use std::iter::{Iterator, DoubleEndedIterator}; @@ -1676,95 +1479,6 @@ mod tests { assert!(!"".contains_char('a')); } - #[test] - fn test_utf16() { - let pairs = - [(String::from_str("๐…๐Œฟ๐Œป๐†๐Œน๐Œป๐Œฐ\n"), - vec![0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16, - 0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16, - 0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16, - 0xd800_u16, 0xdf30_u16, 0x000a_u16]), - - (String::from_str("๐’๐‘‰๐ฎ๐‘€๐ฒ๐‘‹ ๐๐ฒ๐‘\n"), - vec![0xd801_u16, 0xdc12_u16, 0xd801_u16, - 0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16, - 0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16, - 0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16, - 0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16, - 0x000a_u16]), - - (String::from_str("๐Œ€๐Œ–๐Œ‹๐Œ„๐Œ‘๐Œ‰ยท๐ŒŒ๐Œ„๐Œ•๐Œ„๐Œ‹๐Œ‰๐Œ‘\n"), - vec![0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16, - 0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16, - 0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16, - 0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16, - 0xdf04_u16, 0xd800_u16, 0xdf15_u16, 0xd800_u16, - 0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16, - 0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]), - - (String::from_str("๐’‹๐’˜๐’ˆ๐’‘๐’›๐’’ ๐’•๐’“ ๐’ˆ๐’š๐’ ๐’๐’œ๐’’๐’–๐’† ๐’•๐’†\n"), - vec![0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16, - 0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16, - 0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16, - 0x0020_u16, 0xd801_u16, 0xdc95_u16, 0xd801_u16, - 0xdc93_u16, 0x0020_u16, 0xd801_u16, 0xdc88_u16, - 0xd801_u16, 0xdc9a_u16, 0xd801_u16, 0xdc8d_u16, - 0x0020_u16, 0xd801_u16, 0xdc8f_u16, 0xd801_u16, - 0xdc9c_u16, 0xd801_u16, 0xdc92_u16, 0xd801_u16, - 0xdc96_u16, 0xd801_u16, 0xdc86_u16, 0x0020_u16, - 0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16, - 0x000a_u16 ]), - // Issue #12318, even-numbered non-BMP planes - (String::from_str("\U00020000"), - vec![0xD840, 0xDC00])]; - - for p in pairs.iter() { - let (s, u) = (*p).clone(); - let s_as_utf16 = s.as_slice().utf16_units().collect::>(); - let u_as_string = from_utf16(u.as_slice()).unwrap(); - - assert!(is_utf16(u.as_slice())); - assert_eq!(s_as_utf16, u); - - assert_eq!(u_as_string, s); - assert_eq!(from_utf16_lossy(u.as_slice()), s); - - assert_eq!(from_utf16(s_as_utf16.as_slice()).unwrap(), s); - assert_eq!(u_as_string.as_slice().utf16_units().collect::>(), u); - } - } - - #[test] - fn test_utf16_invalid() { - // completely positive cases tested above. - // lead + eof - assert_eq!(from_utf16([0xD800]), None); - // lead + lead - assert_eq!(from_utf16([0xD800, 0xD800]), None); - - // isolated trail - assert_eq!(from_utf16([0x0061, 0xDC00]), None); - - // general - assert_eq!(from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None); - } - - #[test] - fn test_utf16_lossy() { - // completely positive cases tested above. - // lead + eof - assert_eq!(from_utf16_lossy([0xD800]), String::from_str("\uFFFD")); - // lead + lead - assert_eq!(from_utf16_lossy([0xD800, 0xD800]), String::from_str("\uFFFD\uFFFD")); - - // isolated trail - assert_eq!(from_utf16_lossy([0x0061, 0xDC00]), String::from_str("a\uFFFD")); - - // general - assert_eq!(from_utf16_lossy([0xD800, 0xd801, 0xdc8b, 0xD800]), - String::from_str("\uFFFD๐’‹\uFFFD")); - } - #[test] fn test_truncate_utf16_at_nul() { let v = []; @@ -1790,7 +1504,7 @@ mod tests { let mut pos = 0; for ch in v.iter() { assert!(s.char_at(pos) == *ch); - pos += from_char(*ch).len(); + pos += String::from_char(1, *ch).len(); } } @@ -1801,7 +1515,7 @@ mod tests { let mut pos = s.len(); for ch in v.iter().rev() { assert!(s.char_at_reverse(pos) == *ch); - pos -= from_char(*ch).len(); + pos -= String::from_char(1, *ch).len(); } } @@ -2175,54 +1889,6 @@ String::from_str("\u1111\u1171\u11b6")); assert_eq!(from_utf8(xs), None); } - #[test] - fn test_str_from_utf8_owned() { - let xs = Vec::from_slice(b"hello"); - assert_eq!(from_utf8_owned(xs), Ok(String::from_str("hello"))); - - let xs = Vec::from_slice("เธจเน„เธ—เธขไธญๅŽViแป‡t Nam".as_bytes()); - assert_eq!(from_utf8_owned(xs), Ok(String::from_str("เธจเน„เธ—เธขไธญๅŽViแป‡t Nam"))); - - let xs = Vec::from_slice(b"hello\xFF"); - assert_eq!(from_utf8_owned(xs), - Err(Vec::from_slice(b"hello\xFF"))); - } - - #[test] - fn test_str_from_utf8_lossy() { - let xs = b"hello"; - assert_eq!(from_utf8_lossy(xs), Slice("hello")); - - let xs = "เธจเน„เธ—เธขไธญๅŽViแป‡t Nam".as_bytes(); - assert_eq!(from_utf8_lossy(xs), Slice("เธจเน„เธ—เธขไธญๅŽViแป‡t Nam")); - - let xs = b"Hello\xC2 There\xFF Goodbye"; - assert_eq!(from_utf8_lossy(xs), Owned(String::from_str("Hello\uFFFD There\uFFFD Goodbye"))); - - let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye"; - assert_eq!(from_utf8_lossy(xs), - Owned(String::from_str("Hello\uFFFD\uFFFD There\uFFFD Goodbye"))); - - let xs = b"\xF5foo\xF5\x80bar"; - assert_eq!(from_utf8_lossy(xs), Owned(String::from_str("\uFFFDfoo\uFFFD\uFFFDbar"))); - - let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz"; - assert_eq!(from_utf8_lossy(xs), Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFDbaz"))); - - let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz"; - assert_eq!(from_utf8_lossy(xs), - Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFD\uFFFDbaz"))); - - let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar"; - assert_eq!(from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFD\uFFFD\ - foo\U00010000bar"))); - - // surrogates - let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar"; - assert_eq!(from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFDfoo\ - \uFFFD\uFFFD\uFFFDbar"))); - } - #[test] fn test_maybe_owned_traits() { let s = Slice("abcde"); @@ -2288,10 +1954,8 @@ String::from_str("\u1111\u1171\u11b6")); mod bench { use test::Bencher; use super::*; - use vec::Vec; use std::iter::{Iterator, DoubleEndedIterator}; use std::collections::Collection; - use std::slice::Vector; #[bench] fn char_iterator(b: &mut Bencher) { @@ -2432,42 +2096,6 @@ mod bench { }); } - #[bench] - fn from_utf8_lossy_100_ascii(b: &mut Bencher) { - let s = b"Hello there, the quick brown fox jumped over the lazy dog! \ - Lorem ipsum dolor sit amet, consectetur. "; - - assert_eq!(100, s.len()); - b.iter(|| { - let _ = from_utf8_lossy(s); - }); - } - - #[bench] - fn from_utf8_lossy_100_multibyte(b: &mut Bencher) { - let s = "๐Œ€๐Œ–๐Œ‹๐Œ„๐Œ‘๐Œ‰เธ›เธฃุฏูˆู„ุฉ ุงู„ูƒูˆูŠุชเธ—เธจเน„เธ—เธขไธญๅŽ๐…๐Œฟ๐Œป๐†๐Œน๐Œป๐Œฐ".as_bytes(); - assert_eq!(100, s.len()); - b.iter(|| { - let _ = from_utf8_lossy(s); - }); - } - - #[bench] - fn from_utf8_lossy_invalid(b: &mut Bencher) { - let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye"; - b.iter(|| { - let _ = from_utf8_lossy(s); - }); - } - - #[bench] - fn from_utf8_lossy_100_invalid(b: &mut Bencher) { - let s = Vec::from_elem(100, 0xF5u8); - b.iter(|| { - let _ = from_utf8_lossy(s.as_slice()); - }); - } - #[bench] fn bench_connect(b: &mut Bencher) { let s = "เธจเน„เธ—เธขไธญๅŽViแป‡t Nam; Mary had a little lamb, Little lamb"; diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 74b9465f2a569..5450f2d7c31a3 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -21,7 +21,7 @@ use core::raw::Slice; use {Collection, Mutable}; use hash; use str; -use str::{CharRange, StrAllocating}; +use str::{CharRange, StrAllocating, MaybeOwned, Owned, Slice}; use vec::Vec; /// A growable string stored as a UTF-8 encoded buffer. @@ -75,6 +75,14 @@ impl String { /// /// Returns `Err` with the original vector if the vector contains invalid /// UTF-8. + /// + /// # Example + /// + /// ```rust + /// let hello_vec = vec![104, 101, 108, 108, 111]; + /// let string = String::from_utf8(hello_vec); + /// assert_eq!(string, Ok("hello".to_string())); + /// ``` #[inline] pub fn from_utf8(vec: Vec) -> Result> { if str::is_utf8(vec.as_slice()) { @@ -84,6 +92,189 @@ impl String { } } + /// Converts a vector of bytes to a new utf-8 string. + /// Any invalid utf-8 sequences are replaced with U+FFFD REPLACEMENT CHARACTER. + /// + /// # Example + /// + /// ```rust + /// let input = b"Hello \xF0\x90\x80World"; + /// let output = String::from_utf8_lossy(input); + /// assert_eq!(output.as_slice(), "Hello \uFFFDWorld"); + /// ``` + pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a> { + if str::is_utf8(v) { + return Slice(unsafe { mem::transmute(v) }) + } + + static TAG_CONT_U8: u8 = 128u8; + static REPLACEMENT: &'static [u8] = b"\xEF\xBF\xBD"; // U+FFFD in UTF-8 + let mut i = 0; + let total = v.len(); + fn unsafe_get(xs: &[u8], i: uint) -> u8 { + unsafe { *xs.unsafe_ref(i) } + } + fn safe_get(xs: &[u8], i: uint, total: uint) -> u8 { + if i >= total { + 0 + } else { + unsafe_get(xs, i) + } + } + + let mut res = String::with_capacity(total); + + if i > 0 { + unsafe { + res.push_bytes(v.slice_to(i)) + }; + } + + // subseqidx is the index of the first byte of the subsequence we're looking at. + // It's used to copy a bunch of contiguous good codepoints at once instead of copying + // them one by one. + let mut subseqidx = 0; + + while i < total { + let i_ = i; + let byte = unsafe_get(v, i); + i += 1; + + macro_rules! error(() => ({ + unsafe { + if subseqidx != i_ { + res.push_bytes(v.slice(subseqidx, i_)); + } + subseqidx = i; + res.push_bytes(REPLACEMENT); + } + })) + + if byte < 128u8 { + // subseqidx handles this + } else { + let w = str::utf8_char_width(byte); + + match w { + 2 => { + if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { + error!(); + continue; + } + i += 1; + } + 3 => { + match (byte, safe_get(v, i, total)) { + (0xE0 , 0xA0 .. 0xBF) => (), + (0xE1 .. 0xEC, 0x80 .. 0xBF) => (), + (0xED , 0x80 .. 0x9F) => (), + (0xEE .. 0xEF, 0x80 .. 0xBF) => (), + _ => { + error!(); + continue; + } + } + i += 1; + if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { + error!(); + continue; + } + i += 1; + } + 4 => { + match (byte, safe_get(v, i, total)) { + (0xF0 , 0x90 .. 0xBF) => (), + (0xF1 .. 0xF3, 0x80 .. 0xBF) => (), + (0xF4 , 0x80 .. 0x8F) => (), + _ => { + error!(); + continue; + } + } + i += 1; + if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { + error!(); + continue; + } + i += 1; + if safe_get(v, i, total) & 192u8 != TAG_CONT_U8 { + error!(); + continue; + } + i += 1; + } + _ => { + error!(); + continue; + } + } + } + } + if subseqidx < total { + unsafe { + res.push_bytes(v.slice(subseqidx, total)) + }; + } + Owned(res.into_string()) + } + + /// Decode a UTF-16 encoded vector `v` into a `String`, returning `None` + /// if `v` contains any invalid data. + /// + /// # Example + /// + /// ```rust + /// // ๐„žmusic + /// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075, + /// 0x0073, 0x0069, 0x0063]; + /// assert_eq!(String::from_utf16(v), Some("๐„žmusic".to_string())); + /// + /// // ๐„žmuic + /// v[4] = 0xD800; + /// assert_eq!(String::from_utf16(v), None); + /// ``` + pub fn from_utf16(v: &[u16]) -> Option { + let mut s = String::with_capacity(v.len() / 2); + for c in str::utf16_items(v) { + match c { + str::ScalarValue(c) => s.push_char(c), + str::LoneSurrogate(_) => return None + } + } + Some(s) + } + + /// Decode a UTF-16 encoded vector `v` into a string, replacing + /// invalid data with the replacement character (U+FFFD). + /// + /// # Example + /// ```rust + /// // ๐„žmusic + /// let v = [0xD834, 0xDD1E, 0x006d, 0x0075, + /// 0x0073, 0xDD1E, 0x0069, 0x0063, + /// 0xD834]; + /// + /// assert_eq!(String::from_utf16_lossy(v), + /// "๐„žmus\uFFFDic\uFFFD".to_string()); + /// ``` + pub fn from_utf16_lossy(v: &[u16]) -> String { + str::utf16_items(v).map(|c| c.to_char_lossy()).collect() + } + + /// Convert a vector of chars to a string + /// + /// # Example + /// + /// ```rust + /// let chars = ['h', 'e', 'l', 'l', 'o']; + /// let string = String::from_chars(chars); + /// assert_eq!(string.as_slice(), "hello"); + /// ``` + #[inline] + pub fn from_chars(chs: &[char]) -> String { + chs.iter().map(|c| *c).collect() + } + /// Return the underlying byte buffer, encoded as UTF-8. #[inline] pub fn into_bytes(self) -> Vec { @@ -115,6 +306,23 @@ impl String { buf } + /// Convert a byte to a UTF-8 string + /// + /// # Failure + /// + /// Fails if invalid UTF-8 + /// + /// # Example + /// + /// ```rust + /// let string = String::from_byte(104); + /// assert_eq!(string.as_slice(), "h"); + /// ``` + pub fn from_byte(b: u8) -> String { + assert!(b < 128u8); + String::from_char(1, b as char) + } + /// Pushes the given string onto this string buffer. #[inline] pub fn push_str(&mut self, string: &str) { @@ -366,8 +574,10 @@ mod tests { use test::Bencher; use Mutable; - use str::{Str, StrSlice}; + use str; + use str::{Str, StrSlice, Owned, Slice}; use super::String; + use vec::Vec; #[test] fn test_from_str() { @@ -375,20 +585,144 @@ mod tests { assert_eq!(owned.as_ref().map(|s| s.as_slice()), Some("string")); } - #[bench] - fn bench_with_capacity(b: &mut Bencher) { - b.iter(|| { - String::with_capacity(100) - }); + #[test] + fn test_from_utf8() { + let xs = Vec::from_slice(b"hello"); + assert_eq!(String::from_utf8(xs), Ok(String::from_str("hello"))); + + let xs = Vec::from_slice("เธจเน„เธ—เธขไธญๅŽViแป‡t Nam".as_bytes()); + assert_eq!(String::from_utf8(xs), Ok(String::from_str("เธจเน„เธ—เธขไธญๅŽViแป‡t Nam"))); + + let xs = Vec::from_slice(b"hello\xFF"); + assert_eq!(String::from_utf8(xs), + Err(Vec::from_slice(b"hello\xFF"))); } - #[bench] - fn bench_push_str(b: &mut Bencher) { - let s = "เธจเน„เธ—เธขไธญๅŽViแป‡t Nam; Mary had a little lamb, Little lamb"; - b.iter(|| { - let mut r = String::new(); - r.push_str(s); - }); + #[test] + fn test_from_utf8_lossy() { + let xs = b"hello"; + assert_eq!(String::from_utf8_lossy(xs), Slice("hello")); + + let xs = "เธจเน„เธ—เธขไธญๅŽViแป‡t Nam".as_bytes(); + assert_eq!(String::from_utf8_lossy(xs), Slice("เธจเน„เธ—เธขไธญๅŽViแป‡t Nam")); + + let xs = b"Hello\xC2 There\xFF Goodbye"; + assert_eq!(String::from_utf8_lossy(xs), + Owned(String::from_str("Hello\uFFFD There\uFFFD Goodbye"))); + + let xs = b"Hello\xC0\x80 There\xE6\x83 Goodbye"; + assert_eq!(String::from_utf8_lossy(xs), + Owned(String::from_str("Hello\uFFFD\uFFFD There\uFFFD Goodbye"))); + + let xs = b"\xF5foo\xF5\x80bar"; + assert_eq!(String::from_utf8_lossy(xs), + Owned(String::from_str("\uFFFDfoo\uFFFD\uFFFDbar"))); + + let xs = b"\xF1foo\xF1\x80bar\xF1\x80\x80baz"; + assert_eq!(String::from_utf8_lossy(xs), + Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFDbaz"))); + + let xs = b"\xF4foo\xF4\x80bar\xF4\xBFbaz"; + assert_eq!(String::from_utf8_lossy(xs), + Owned(String::from_str("\uFFFDfoo\uFFFDbar\uFFFD\uFFFDbaz"))); + + let xs = b"\xF0\x80\x80\x80foo\xF0\x90\x80\x80bar"; + assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFD\uFFFD\ + foo\U00010000bar"))); + + // surrogates + let xs = b"\xED\xA0\x80foo\xED\xBF\xBFbar"; + assert_eq!(String::from_utf8_lossy(xs), Owned(String::from_str("\uFFFD\uFFFD\uFFFDfoo\ + \uFFFD\uFFFD\uFFFDbar"))); + } + + #[test] + fn test_from_utf16() { + let pairs = + [(String::from_str("๐…๐Œฟ๐Œป๐†๐Œน๐Œป๐Œฐ\n"), + vec![0xd800_u16, 0xdf45_u16, 0xd800_u16, 0xdf3f_u16, + 0xd800_u16, 0xdf3b_u16, 0xd800_u16, 0xdf46_u16, + 0xd800_u16, 0xdf39_u16, 0xd800_u16, 0xdf3b_u16, + 0xd800_u16, 0xdf30_u16, 0x000a_u16]), + + (String::from_str("๐’๐‘‰๐ฎ๐‘€๐ฒ๐‘‹ ๐๐ฒ๐‘\n"), + vec![0xd801_u16, 0xdc12_u16, 0xd801_u16, + 0xdc49_u16, 0xd801_u16, 0xdc2e_u16, 0xd801_u16, + 0xdc40_u16, 0xd801_u16, 0xdc32_u16, 0xd801_u16, + 0xdc4b_u16, 0x0020_u16, 0xd801_u16, 0xdc0f_u16, + 0xd801_u16, 0xdc32_u16, 0xd801_u16, 0xdc4d_u16, + 0x000a_u16]), + + (String::from_str("๐Œ€๐Œ–๐Œ‹๐Œ„๐Œ‘๐Œ‰ยท๐ŒŒ๐Œ„๐Œ•๐Œ„๐Œ‹๐Œ‰๐Œ‘\n"), + vec![0xd800_u16, 0xdf00_u16, 0xd800_u16, 0xdf16_u16, + 0xd800_u16, 0xdf0b_u16, 0xd800_u16, 0xdf04_u16, + 0xd800_u16, 0xdf11_u16, 0xd800_u16, 0xdf09_u16, + 0x00b7_u16, 0xd800_u16, 0xdf0c_u16, 0xd800_u16, + 0xdf04_u16, 0xd800_u16, 0xdf15_u16, 0xd800_u16, + 0xdf04_u16, 0xd800_u16, 0xdf0b_u16, 0xd800_u16, + 0xdf09_u16, 0xd800_u16, 0xdf11_u16, 0x000a_u16 ]), + + (String::from_str("๐’‹๐’˜๐’ˆ๐’‘๐’›๐’’ ๐’•๐’“ ๐’ˆ๐’š๐’ ๐’๐’œ๐’’๐’–๐’† ๐’•๐’†\n"), + vec![0xd801_u16, 0xdc8b_u16, 0xd801_u16, 0xdc98_u16, + 0xd801_u16, 0xdc88_u16, 0xd801_u16, 0xdc91_u16, + 0xd801_u16, 0xdc9b_u16, 0xd801_u16, 0xdc92_u16, + 0x0020_u16, 0xd801_u16, 0xdc95_u16, 0xd801_u16, + 0xdc93_u16, 0x0020_u16, 0xd801_u16, 0xdc88_u16, + 0xd801_u16, 0xdc9a_u16, 0xd801_u16, 0xdc8d_u16, + 0x0020_u16, 0xd801_u16, 0xdc8f_u16, 0xd801_u16, + 0xdc9c_u16, 0xd801_u16, 0xdc92_u16, 0xd801_u16, + 0xdc96_u16, 0xd801_u16, 0xdc86_u16, 0x0020_u16, + 0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16, + 0x000a_u16 ]), + // Issue #12318, even-numbered non-BMP planes + (String::from_str("\U00020000"), + vec![0xD840, 0xDC00])]; + + for p in pairs.iter() { + let (s, u) = (*p).clone(); + let s_as_utf16 = s.as_slice().utf16_units().collect::>(); + let u_as_string = String::from_utf16(u.as_slice()).unwrap(); + + assert!(str::is_utf16(u.as_slice())); + assert_eq!(s_as_utf16, u); + + assert_eq!(u_as_string, s); + assert_eq!(String::from_utf16_lossy(u.as_slice()), s); + + assert_eq!(String::from_utf16(s_as_utf16.as_slice()).unwrap(), s); + assert_eq!(u_as_string.as_slice().utf16_units().collect::>(), u); + } + } + + #[test] + fn test_utf16_invalid() { + // completely positive cases tested above. + // lead + eof + assert_eq!(String::from_utf16([0xD800]), None); + // lead + lead + assert_eq!(String::from_utf16([0xD800, 0xD800]), None); + + // isolated trail + assert_eq!(String::from_utf16([0x0061, 0xDC00]), None); + + // general + assert_eq!(String::from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None); + } + + #[test] + fn test_from_utf16_lossy() { + // completely positive cases tested above. + // lead + eof + assert_eq!(String::from_utf16_lossy([0xD800]), String::from_str("\uFFFD")); + // lead + lead + assert_eq!(String::from_utf16_lossy([0xD800, 0xD800]), String::from_str("\uFFFD\uFFFD")); + + // isolated trail + assert_eq!(String::from_utf16_lossy([0x0061, 0xDC00]), String::from_str("a\uFFFD")); + + // general + assert_eq!(String::from_utf16_lossy([0xD800, 0xd801, 0xdc8b, 0xD800]), + String::from_str("\uFFFD๐’‹\uFFFD")); } #[test] @@ -492,4 +826,57 @@ mod tests { assert_eq!(b.len(), 7); assert_eq!(b.as_slice(), "1234522"); } + + #[bench] + fn bench_with_capacity(b: &mut Bencher) { + b.iter(|| { + String::with_capacity(100) + }); + } + + #[bench] + fn bench_push_str(b: &mut Bencher) { + let s = "เธจเน„เธ—เธขไธญๅŽViแป‡t Nam; Mary had a little lamb, Little lamb"; + b.iter(|| { + let mut r = String::new(); + r.push_str(s); + }); + } + + #[bench] + fn from_utf8_lossy_100_ascii(b: &mut Bencher) { + let s = b"Hello there, the quick brown fox jumped over the lazy dog! \ + Lorem ipsum dolor sit amet, consectetur. "; + + assert_eq!(100, s.len()); + b.iter(|| { + let _ = String::from_utf8_lossy(s); + }); + } + + #[bench] + fn from_utf8_lossy_100_multibyte(b: &mut Bencher) { + let s = "รฐยล’โ‚ฌรฐยล’โ€“รฐยล’โ€นรฐยล’โ€žรฐยล’โ€˜รฐยล’โ€ฐร ยธโ€บร ยธยฃร˜ยฏร™ห†ร™โ€žร˜ยฉ\ + ร˜ยงร™โ€žร™ฦ’ร™ห†ร™ล ร˜ยชร ยธโ€”ร ยธยจร ยนโ€žร ยธโ€”ร ยธยขรคยธยญรฅยลฝรฐยยโ€ฆรฐยล’ยฟรฐยล’ยปรฐยยโ€ รฐยล’ยนรฐยล’ยปรฐยล’ยฐ".as_bytes(); + assert_eq!(100, s.len()); + b.iter(|| { + let _ = String::from_utf8_lossy(s); + }); + } + + #[bench] + fn from_utf8_lossy_invalid(b: &mut Bencher) { + let s = b"Hello\xC0\x80 There\xE6\x83 Goodbye"; + b.iter(|| { + let _ = String::from_utf8_lossy(s); + }); + } + + #[bench] + fn from_utf8_lossy_100_invalid(b: &mut Bencher) { + let s = Vec::from_elem(100, 0xF5u8); + b.iter(|| { + let _ = String::from_utf8_lossy(s.as_slice()); + }); + } } diff --git a/src/libdebug/repr.rs b/src/libdebug/repr.rs index 3e541929dbf08..9755d54a13205 100644 --- a/src/libdebug/repr.rs +++ b/src/libdebug/repr.rs @@ -575,7 +575,6 @@ struct P {a: int, b: f64} #[test] fn test_repr() { - use std::str; use std::io::stdio::println; use std::char::is_alphabetic; use std::mem::swap; @@ -584,7 +583,7 @@ fn test_repr() { fn exact_test(t: &T, e:&str) { let mut m = io::MemWriter::new(); write_repr(&mut m as &mut io::Writer, t).unwrap(); - let s = str::from_utf8(m.unwrap().as_slice()).unwrap().to_string(); + let s = String::from_utf8(m.unwrap()).unwrap(); assert_eq!(s.as_slice(), e); } diff --git a/src/libnative/io/file_win32.rs b/src/libnative/io/file_win32.rs index dbca4ff7ff719..0f8fa2618027f 100644 --- a/src/libnative/io/file_win32.rs +++ b/src/libnative/io/file_win32.rs @@ -378,7 +378,7 @@ pub fn readdir(p: &CString) -> IoResult> { } else { let fp_vec = vec::raw::from_buf(fp_buf, libc::wcslen(fp_buf) as uint); let fp_trimmed = str::truncate_utf16_at_nul(fp_vec.as_slice()); - let fp_str = str::from_utf16(fp_trimmed) + let fp_str = String::from_utf16(fp_trimmed) .expect("rust_list_dir_wfd_fp_buf returned invalid UTF-16"); paths.push(Path::new(fp_str)); } diff --git a/src/libregex/parse.rs b/src/libregex/parse.rs index a2747526df859..109d32f69b967 100644 --- a/src/libregex/parse.rs +++ b/src/libregex/parse.rs @@ -13,7 +13,6 @@ use std::cmp; use std::fmt; use std::iter; use std::num; -use std::str; /// Static data containing Unicode ranges for general categories and scripts. use unicode::regex::{UNICODE_CLASSES, PERLD, PERLS, PERLW}; @@ -510,7 +509,7 @@ impl<'a> Parser<'a> { }; self.chari = closer; let greed = try!(self.get_next_greedy()); - let inner = str::from_chars( + let inner = String::from_chars( self.chars.as_slice().slice(start + 1, closer)); // Parse the min and max values from the regex. @@ -944,7 +943,7 @@ impl<'a> Parser<'a> { } fn slice(&self, start: uint, end: uint) -> String { - str::from_chars(self.chars.as_slice().slice(start, end)).to_string() + String::from_chars(self.chars.as_slice().slice(start, end)) } } diff --git a/src/libregex/test/bench.rs b/src/libregex/test/bench.rs index 0e4581a401e90..2a606ccd0cee3 100644 --- a/src/libregex/test/bench.rs +++ b/src/libregex/test/bench.rs @@ -10,7 +10,6 @@ #![allow(non_snake_case_functions)] use std::rand::{Rng, task_rng}; -use std::str; use stdtest::Bencher; use regex::{Regex, NoExpand}; @@ -163,7 +162,7 @@ fn gen_text(n: uint) -> String { *b = '\n' as u8 } } - str::from_utf8(bytes.as_slice()).unwrap().to_string() + String::from_utf8(bytes).unwrap() } throughput!(easy0_32, easy0(), 32) diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index cad164c9e2019..6fa8a1530234e 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -61,7 +61,7 @@ pub fn llvm_err(sess: &Session, msg: String) -> ! { sess.fatal(msg.as_slice()); } else { let err = CString::new(cstr, true); - let err = str::from_utf8_lossy(err.as_bytes()); + let err = String::from_utf8_lossy(err.as_bytes()); sess.fatal(format!("{}: {}", msg.as_slice(), err.as_slice()).as_slice()); @@ -380,8 +380,7 @@ pub mod write { sess.note(format!("{}", &cmd).as_slice()); let mut note = prog.error.clone(); note.push_all(prog.output.as_slice()); - sess.note(str::from_utf8(note.as_slice()).unwrap() - .as_slice()); + sess.note(str::from_utf8(note.as_slice()).unwrap()); sess.abort_if_errors(); } }, @@ -1177,8 +1176,7 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool, sess.note(format!("{}", &cmd).as_slice()); let mut output = prog.error.clone(); output.push_all(prog.output.as_slice()); - sess.note(str::from_utf8(output.as_slice()).unwrap() - .as_slice()); + sess.note(str::from_utf8(output.as_slice()).unwrap()); sess.abort_if_errors(); } }, diff --git a/src/librustc/driver/mod.rs b/src/librustc/driver/mod.rs index e850b71dda8cc..3fd402c90fdd8 100644 --- a/src/librustc/driver/mod.rs +++ b/src/librustc/driver/mod.rs @@ -20,7 +20,6 @@ use metadata; use std::any::AnyRefExt; use std::io; use std::os; -use std::str; use std::task::TaskBuilder; use syntax::ast; @@ -82,8 +81,7 @@ fn run_compiler(args: &[String]) { let ifile = matches.free.get(0).as_slice(); if ifile == "-" { let contents = io::stdin().read_to_end().unwrap(); - let src = str::from_utf8(contents.as_slice()).unwrap() - .to_string(); + let src = String::from_utf8(contents).unwrap(); (StrInput(src), None) } else { (FileInput(Path::new(ifile)), Some(Path::new(ifile))) diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index 6cb0ab51ca10d..87333499ec3a2 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -35,7 +35,6 @@ use std::hash::Hash; use std::hash; use std::io::MemWriter; use std::mem; -use std::str; use std::collections::HashMap; use syntax::abi; use syntax::ast::*; @@ -619,7 +618,7 @@ fn encode_visibility(ebml_w: &mut Encoder, visibility: Visibility) { Public => 'y', Inherited => 'i', }; - ebml_w.wr_str(str::from_char(ch).as_slice()); + ebml_w.wr_str(ch.to_string().as_slice()); ebml_w.end_tag(); } @@ -1922,5 +1921,5 @@ pub fn encoded_ty(tcx: &ty::ctxt, t: ty::t) -> String { tcx: tcx, abbrevs: &RefCell::new(HashMap::new()) }, t); - str::from_utf8_owned(Vec::from_slice(wr.get_ref())).unwrap().to_string() + String::from_utf8(wr.unwrap()).unwrap() } diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 3cb5cdc043962..ecdc736790dbe 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -13,7 +13,6 @@ //! This module uses libsyntax's lexer to provide token-based highlighting for //! the HTML documentation generated by rustdoc. -use std::str; use std::io; use syntax::parse; @@ -37,7 +36,7 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String { class, id, &mut out).unwrap(); - str::from_utf8_lossy(out.unwrap().as_slice()).to_string() + String::from_utf8_lossy(out.unwrap().as_slice()).into_string() } /// Exhausts the `lexer` writing the output into `out`. diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index ed047ef629dff..244fada5b9ada 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -460,7 +460,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> io::IoResult try!(write!(&mut w, "]}};")); - Ok(str::from_utf8(w.unwrap().as_slice()).unwrap().to_string()) + Ok(String::from_utf8(w.unwrap()).unwrap()) } fn write_shared(cx: &Context, @@ -723,9 +723,9 @@ impl<'a> SourceCollector<'a> { // Remove the utf-8 BOM if any let contents = if contents.starts_with("\ufeff") { - contents.as_slice().slice_from(3) + contents.slice_from(3) } else { - contents.as_slice() + contents }; // Create the intermediate directories diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 76b9f11089f9b..2cbac090835ed 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -29,7 +29,6 @@ extern crate time; use std::io; use std::io::{File, MemWriter}; -use std::str; use std::gc::Gc; use serialize::{json, Decodable, Encodable}; use externalfiles::ExternalHtml; @@ -429,7 +428,7 @@ fn json_output(krate: clean::Crate, res: Vec , let mut encoder = json::Encoder::new(&mut w as &mut io::Writer); krate.encode(&mut encoder).unwrap(); } - str::from_utf8_owned(w.unwrap()).unwrap() + String::from_utf8(w.unwrap()).unwrap() }; let crate_json = match json::from_str(crate_json_str.as_slice()) { Ok(j) => j, diff --git a/src/libserialize/base64.rs b/src/libserialize/base64.rs index 63cfbd6d9aa17..80a8a06edda6b 100644 --- a/src/libserialize/base64.rs +++ b/src/libserialize/base64.rs @@ -146,7 +146,7 @@ impl<'a> ToBase64 for &'a [u8] { } unsafe { - str::raw::from_utf8(v.as_slice()).to_string() + str::raw::from_utf8_owned(v) } } } diff --git a/src/libserialize/hex.rs b/src/libserialize/hex.rs index 51fab7b135458..3083c06c8773f 100644 --- a/src/libserialize/hex.rs +++ b/src/libserialize/hex.rs @@ -45,7 +45,7 @@ impl<'a> ToHex for &'a [u8] { } unsafe { - str::raw::from_utf8(v.as_slice()).to_string() + str::raw::from_utf8_owned(v) } } } diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index df4d3437b1ce6..f7301abef51f2 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -240,7 +240,7 @@ pub fn decode>(s: &str) -> DecodeResult /// Shortcut function to encode a `T` into a JSON `String` pub fn encode<'a, T: Encodable, io::IoError>>(object: &T) -> String { let buff = Encoder::buffer_encode(object); - str::from_utf8_owned(buff).unwrap() + String::from_utf8(buff).unwrap() } impl fmt::Show for ErrorCode { @@ -517,8 +517,7 @@ impl<'a> ::Encoder for Encoder<'a> { let mut check_encoder = Encoder::new(&mut buf); try!(f(transmute(&mut check_encoder))); } - let out = str::from_utf8_owned(buf.unwrap()).unwrap(); - let out = out.as_slice(); + let out = str::from_utf8(buf.get_ref()).unwrap(); let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"'; if needs_wrapping { try!(write!(self.writer, "\"")); } try!(f(self)); @@ -762,8 +761,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> { let mut check_encoder = PrettyEncoder::new(&mut buf); try!(f(transmute(&mut check_encoder))); } - let out = str::from_utf8_owned(buf.unwrap()).unwrap(); - let out = out.as_slice(); + let out = str::from_utf8(buf.get_ref()).unwrap(); let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"'; if needs_wrapping { try!(write!(self.writer, "\"")); } try!(f(self)); @@ -810,7 +808,7 @@ impl Json { pub fn to_pretty_str(&self) -> String { let mut s = MemWriter::new(); self.to_pretty_writer(&mut s as &mut io::Writer).unwrap(); - str::from_utf8_owned(s.unwrap()).unwrap() + String::from_utf8(s.unwrap()).unwrap() } /// If the Json value is an Object, returns the value associated with the provided key. @@ -1728,14 +1726,14 @@ impl> Builder { /// Decodes a json value from an `&mut io::Reader` pub fn from_reader(rdr: &mut io::Reader) -> Result { let contents = match rdr.read_to_end() { - Ok(c) => c, + Ok(c) => c, Err(e) => return Err(io_error_to_error(e)) }; - let s = match str::from_utf8_owned(contents) { - Ok(s) => s, - _ => return Err(SyntaxError(NotUtf8, 0, 0)) + let s = match str::from_utf8(contents.as_slice()) { + Some(s) => s, + _ => return Err(SyntaxError(NotUtf8, 0, 0)) }; - let mut builder = Builder::new(s.as_slice().chars()); + let mut builder = Builder::new(s.chars()); builder.build() } diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 796147ce7a05d..b9c86e2b23586 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -19,7 +19,6 @@ use mem; use option::{Option, Some, None}; use slice::{ImmutableVector, MutableVector, Vector}; use str::{OwnedStr, Str, StrAllocating, StrSlice}; -use str; use string::String; use to_str::{IntoStr}; use vec::Vec; @@ -438,7 +437,7 @@ unsafe fn str_map_bytes(string: String, map: &'static [u8]) -> String { *b = map[*b as uint]; } - String::from_str(str::from_utf8(bytes.as_slice()).unwrap()) + String::from_utf8(bytes).unwrap() } #[inline] @@ -525,7 +524,6 @@ static ASCII_UPPER_MAP: &'static [u8] = &[ mod tests { use prelude::*; use super::*; - use str::from_char; use char::from_u32; use vec::Vec; use str::StrSlice; @@ -677,8 +675,8 @@ mod tests { while i <= 500 { let upper = if 'a' as u32 <= i && i <= 'z' as u32 { i + 'A' as u32 - 'a' as u32 } else { i }; - assert_eq!(from_char(from_u32(i).unwrap()).as_slice().to_ascii_upper(), - from_char(from_u32(upper).unwrap()).to_string()) + assert_eq!((from_u32(i).unwrap()).to_string().as_slice().to_ascii_upper(), + (from_u32(upper).unwrap()).to_string()) i += 1; } } @@ -693,8 +691,8 @@ mod tests { while i <= 500 { let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 } else { i }; - assert_eq!(from_char(from_u32(i).unwrap()).as_slice().to_ascii_lower(), - from_char(from_u32(lower).unwrap()).to_string()) + assert_eq!((from_u32(i).unwrap()).to_string().as_slice().to_ascii_lower(), + (from_u32(lower).unwrap()).to_string()) i += 1; } } @@ -709,8 +707,8 @@ mod tests { while i <= 500 { let upper = if 'a' as u32 <= i && i <= 'z' as u32 { i + 'A' as u32 - 'a' as u32 } else { i }; - assert_eq!(from_char(from_u32(i).unwrap()).to_string().into_ascii_upper(), - from_char(from_u32(upper).unwrap()).to_string()) + assert_eq!((from_u32(i).unwrap()).to_string().into_ascii_upper(), + (from_u32(upper).unwrap()).to_string()) i += 1; } } @@ -726,8 +724,8 @@ mod tests { while i <= 500 { let lower = if 'A' as u32 <= i && i <= 'Z' as u32 { i + 'a' as u32 - 'A' as u32 } else { i }; - assert_eq!(from_char(from_u32(i).unwrap()).to_string().into_ascii_lower(), - from_char(from_u32(lower).unwrap()).to_string()) + assert_eq!((from_u32(i).unwrap()).to_string().into_ascii_lower(), + (from_u32(lower).unwrap()).to_string()) i += 1; } } @@ -747,11 +745,8 @@ mod tests { let c = i; let lower = if 'A' as u32 <= c && c <= 'Z' as u32 { c + 'a' as u32 - 'A' as u32 } else { c }; - assert!(from_char(from_u32(i).unwrap()).as_slice() - .eq_ignore_ascii_case( - from_char( - from_u32(lower) - .unwrap()).as_slice())); + assert!((from_u32(i).unwrap()).to_string().as_slice().eq_ignore_ascii_case( + (from_u32(lower).unwrap()).to_string().as_slice())); i += 1; } } diff --git a/src/libstd/fmt.rs b/src/libstd/fmt.rs index 5834e576b0814..b9c6220c0e2de 100644 --- a/src/libstd/fmt.rs +++ b/src/libstd/fmt.rs @@ -417,10 +417,7 @@ the `}` character is escaped with `}}`. use io::Writer; use io; use result::{Ok, Err}; -use str::{Str, StrAllocating}; -use str; use string; -use slice::Vector; pub use core::fmt::{Formatter, Result, FormatWriter, rt}; pub use core::fmt::{Show, Bool, Char, Signed, Unsigned, Octal, Binary}; @@ -464,7 +461,7 @@ pub use core::fmt::{secret_pointer}; pub fn format(args: &Arguments) -> string::String{ let mut output = io::MemWriter::new(); let _ = write!(&mut output, "{}", args); - str::from_utf8(output.unwrap().as_slice()).unwrap().into_string() + string::String::from_utf8(output.unwrap()).unwrap() } impl<'a> Writer for Formatter<'a> { diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index caff7d5e4c593..d49c56b470440 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -999,9 +999,9 @@ mod test { let mut read_buf = [0, .. 1028]; let read_str = match check!(read_stream.read(read_buf)) { -1|0 => fail!("shouldn't happen"), - n => str::from_utf8(read_buf.slice_to(n)).unwrap().to_owned() + n => str::from_utf8(read_buf.slice_to(n)).unwrap().to_string() }; - assert_eq!(read_str, message.to_owned()); + assert_eq!(read_str.as_slice(), message); } check!(unlink(filename)); }) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 6ac092fd8c657..0df2bb0f57c5a 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -705,9 +705,9 @@ pub trait Reader { /// UTF-8 bytes. fn read_to_string(&mut self) -> IoResult { self.read_to_end().and_then(|s| { - match str::from_utf8(s.as_slice()) { - Some(s) => Ok(String::from_str(s)), - None => Err(standard_error(InvalidInput)), + match String::from_utf8(s) { + Ok(s) => Ok(s), + Err(_) => Err(standard_error(InvalidInput)), } }) } @@ -1440,9 +1440,9 @@ pub trait Buffer: Reader { /// valid UTF-8 sequence of bytes. fn read_line(&mut self) -> IoResult { self.read_until('\n' as u8).and_then(|line| - match str::from_utf8(line.as_slice()) { - Some(s) => Ok(String::from_str(s)), - None => Err(standard_error(InvalidInput)), + match String::from_utf8(line) { + Ok(s) => Ok(s), + Err(_) => Err(standard_error(InvalidInput)), } ) } diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs index 07574b7264579..1eee69834948f 100644 --- a/src/libstd/io/process.rs +++ b/src/libstd/io/process.rs @@ -14,7 +14,6 @@ use prelude::*; -use str; use fmt; use os; use io::{IoResult, IoError}; @@ -313,7 +312,6 @@ impl Command { /// /// ``` /// use std::io::Command; - /// use std::str; /// /// let output = match Command::new("cat").arg("foot.txt").output() { /// Ok(output) => output, @@ -321,8 +319,8 @@ impl Command { /// }; /// /// println!("status: {}", output.status); - /// println!("stdout: {}", str::from_utf8_lossy(output.output.as_slice())); - /// println!("stderr: {}", str::from_utf8_lossy(output.error.as_slice())); + /// println!("stdout: {}", String::from_utf8_lossy(output.output.as_slice())); + /// println!("stderr: {}", String::from_utf8_lossy(output.error.as_slice())); /// ``` pub fn output(&self) -> IoResult { self.spawn().and_then(|p| p.wait_with_output()) @@ -353,9 +351,9 @@ impl fmt::Show for Command { /// non-utf8 data is lossily converted using the utf8 replacement /// character. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "{}", str::from_utf8_lossy(self.program.as_bytes_no_nul()))); + try!(write!(f, "{}", String::from_utf8_lossy(self.program.as_bytes_no_nul()))); for arg in self.args.iter() { - try!(write!(f, " '{}'", str::from_utf8_lossy(arg.as_bytes_no_nul()))); + try!(write!(f, " '{}'", String::from_utf8_lossy(arg.as_bytes_no_nul()))); } Ok(()) } @@ -813,8 +811,7 @@ mod tests { use os; let prog = pwd_cmd().spawn().unwrap(); - let output = str::from_utf8(prog.wait_with_output().unwrap() - .output.as_slice()).unwrap().to_string(); + let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap(); let parent_dir = os::getcwd(); let child_dir = Path::new(output.as_slice().trim()); @@ -832,8 +829,7 @@ mod tests { let parent_dir = os::getcwd().dir_path(); let prog = pwd_cmd().cwd(&parent_dir).spawn().unwrap(); - let output = str::from_utf8(prog.wait_with_output().unwrap() - .output.as_slice()).unwrap().to_string(); + let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap(); let child_dir = Path::new(output.as_slice().trim().into_string()); let parent_stat = parent_dir.stat().unwrap(); @@ -867,8 +863,7 @@ mod tests { if running_on_valgrind() { return; } let prog = env_cmd().spawn().unwrap(); - let output = str::from_utf8(prog.wait_with_output().unwrap() - .output.as_slice()).unwrap().to_string(); + let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap(); let r = os::env(); for &(ref k, ref v) in r.iter() { @@ -884,9 +879,7 @@ mod tests { if running_on_valgrind() { return; } let mut prog = env_cmd().spawn().unwrap(); - let output = str::from_utf8(prog.wait_with_output() - .unwrap().output.as_slice()) - .unwrap().to_string(); + let output = String::from_utf8(prog.wait_with_output().unwrap().output).unwrap(); let r = os::env(); for &(ref k, ref v) in r.iter() { @@ -908,7 +901,7 @@ mod tests { let new_env = vec![("RUN_TEST_NEW_ENV", "123")]; let prog = env_cmd().env_set_all(new_env.as_slice()).spawn().unwrap(); let result = prog.wait_with_output().unwrap(); - let output = str::from_utf8_lossy(result.output.as_slice()).into_string(); + let output = String::from_utf8_lossy(result.output.as_slice()).into_string(); assert!(output.as_slice().contains("RUN_TEST_NEW_ENV=123"), "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 86577a9840d04..96d3b3e3e6a53 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -137,7 +137,7 @@ pub fn getcwd() -> Path { fail!(); } } - Path::new(str::from_utf16(str::truncate_utf16_at_nul(buf)) + Path::new(String::from_utf16(str::truncate_utf16_at_nul(buf)) .expect("GetCurrentDirectoryW returned invalid UTF-16")) } @@ -151,7 +151,6 @@ pub mod win32 { use slice::{MutableVector, ImmutableVector}; use string::String; use str::StrSlice; - use str; use vec::Vec; pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD) @@ -180,7 +179,7 @@ pub mod win32 { // We want to explicitly catch the case when the // closure returned invalid UTF-16, rather than // set `res` to None and continue. - let s = str::from_utf16(sub) + let s = String::from_utf16(sub) .expect("fill_utf16_buf_and_decode: closure created invalid UTF-16"); res = option::Some(s) } @@ -208,7 +207,7 @@ fn with_env_lock(f: || -> T) -> T { /// Returns a vector of (variable, value) pairs, for all the environment /// variables of the current process. /// -/// Invalid UTF-8 bytes are replaced with \uFFFD. See `str::from_utf8_lossy()` +/// Invalid UTF-8 bytes are replaced with \uFFFD. See `String::from_utf8_lossy()` /// for details. /// /// # Example @@ -223,8 +222,8 @@ fn with_env_lock(f: || -> T) -> T { /// ``` pub fn env() -> Vec<(String,String)> { env_as_bytes().move_iter().map(|(k,v)| { - let k = String::from_str(str::from_utf8_lossy(k.as_slice()).as_slice()); - let v = String::from_str(str::from_utf8_lossy(v.as_slice()).as_slice()); + let k = String::from_utf8_lossy(k.as_slice()).into_string(); + let v = String::from_utf8_lossy(v.as_slice()).into_string(); (k,v) }).collect() } @@ -266,7 +265,7 @@ pub fn env_as_bytes() -> Vec<(Vec,Vec)> { let p = &*ch.offset(i); let len = ptr::position(p, |c| *c == 0); raw::buf_as_slice(p, len, |s| { - result.push(str::from_utf16_lossy(s).into_bytes()); + result.push(String::from_utf16_lossy(s).into_bytes()); }); i += len as int + 1; } @@ -316,7 +315,7 @@ pub fn env_as_bytes() -> Vec<(Vec,Vec)> { /// None if the variable isn't set. /// /// Any invalid UTF-8 bytes in the value are replaced by \uFFFD. See -/// `str::from_utf8_lossy()` for details. +/// `String::from_utf8_lossy()` for details. /// /// # Failure /// @@ -334,7 +333,7 @@ pub fn env_as_bytes() -> Vec<(Vec,Vec)> { /// } /// ``` pub fn getenv(n: &str) -> Option { - getenv_as_bytes(n).map(|v| String::from_str(str::from_utf8_lossy(v.as_slice()).as_slice())) + getenv_as_bytes(n).map(|v| String::from_utf8_lossy(v.as_slice()).into_string()) } #[cfg(unix)] @@ -1050,7 +1049,7 @@ pub fn error_string(errnum: uint) -> String { return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err); } - let msg = str::from_utf16(str::truncate_utf16_at_nul(buf)); + let msg = String::from_utf16(str::truncate_utf16_at_nul(buf)); match msg { Some(msg) => format!("OS Error {}: {}", errnum, msg), None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum), @@ -1186,7 +1185,7 @@ fn real_args_as_bytes() -> Vec> { fn real_args() -> Vec { real_args_as_bytes().move_iter() .map(|v| { - str::from_utf8_lossy(v.as_slice()).into_string() + String::from_utf8_lossy(v.as_slice()).into_string() }).collect() } @@ -1207,7 +1206,7 @@ fn real_args() -> Vec { // Push it onto the list. let opt_s = slice::raw::buf_as_slice(ptr as *const _, len, |buf| { - str::from_utf16(str::truncate_utf16_at_nul(buf)) + String::from_utf16(str::truncate_utf16_at_nul(buf)) }); opt_s.expect("CommandLineToArgvW returned invalid UTF-16") }); @@ -1244,7 +1243,7 @@ extern "system" { /// via the command line). /// /// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD. -/// See `str::from_utf8_lossy` for details. +/// See `String::from_utf8_lossy` for details. /// # Example /// /// ```rust diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 7d814df8ebf95..ececfab5f7433 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -72,7 +72,7 @@ use fmt; use iter::Iterator; use option::{Option, None, Some}; use str; -use str::{MaybeOwned, Str, StrSlice, from_utf8_lossy}; +use str::{MaybeOwned, Str, StrSlice}; use string::String; use slice::Vector; use slice::{ImmutableEqVector, ImmutableVector}; @@ -483,7 +483,7 @@ impl<'a, P: GenericPath> Display<'a, P> { /// unicode replacement char. This involves allocation. #[inline] pub fn as_maybe_owned(&self) -> MaybeOwned<'a> { - from_utf8_lossy(if self.filename { + String::from_utf8_lossy(if self.filename { match self.path.filename() { None => &[], Some(v) => v diff --git a/src/libstd/rt/backtrace.rs b/src/libstd/rt/backtrace.rs index 09922b5ad7615..d01a1b5b1313c 100644 --- a/src/libstd/rt/backtrace.rs +++ b/src/libstd/rt/backtrace.rs @@ -992,12 +992,11 @@ mod imp { mod test { use prelude::*; use io::MemWriter; - use str; macro_rules! t( ($a:expr, $b:expr) => ({ let mut m = MemWriter::new(); super::demangle(&mut m, $a).unwrap(); - assert_eq!(str::from_utf8(m.unwrap().as_slice()).unwrap().to_owned(), $b.to_owned()); + assert_eq!(String::from_utf8(m.unwrap()).unwrap(), $b.to_string()); }) ) #[test] diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 5ac9dc86fcec2..703adcbd33552 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -22,7 +22,6 @@ use print::pprust; use std::gc::Gc; use std::io::File; use std::rc::Rc; -use std::str; // These macros all relate to the file system; they either return // the column/row/filename of the expression, or they include @@ -122,17 +121,17 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) } Ok(bytes) => bytes, }; - match str::from_utf8(bytes.as_slice()) { - Some(src) => { + match String::from_utf8(bytes) { + Ok(src) => { // Add this input file to the code map to make it available as // dependency information let filename = file.display().to_string(); - let interned = token::intern_and_get_ident(src); - cx.codemap().new_filemap(filename, src.to_string()); + let interned = token::intern_and_get_ident(src.as_slice()); + cx.codemap().new_filemap(filename, src); base::MacExpr::new(cx.expr_str(sp, interned)) } - None => { + Err(_) => { cx.span_err(sp, format!("{} wasn't a utf-8 file", file.display()).as_slice()); diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 3f3a8a723f10c..c53638ed07d13 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -339,7 +339,7 @@ pub fn gather_comments_and_literals(span_diagnostic: &diagnostic::SpanHandler, srdr: &mut io::Reader) -> (Vec, Vec) { let src = srdr.read_to_end().unwrap(); - let src = str::from_utf8(src.as_slice()).unwrap().to_string(); + let src = String::from_utf8(src).unwrap(); let cm = CodeMap::new(); let filemap = cm.new_filemap(path, src); let mut rdr = lexer::StringReader::new_raw(span_diagnostic, filemap); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 615a4489a73de..d524622f8ecf5 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -30,7 +30,6 @@ use std::gc::Gc; use std::io::{IoResult, MemWriter}; use std::io; use std::mem; -use std::str; pub enum AnnNode<'a> { NodeBlock(&'a ast::Block), @@ -138,7 +137,7 @@ pub fn to_string(f: |&mut State| -> IoResult<()>) -> String { // downcasts. let (_, wr): (uint, Box) = mem::transmute_copy(&s.s.out); let result = - str::from_utf8_owned(Vec::from_slice(wr.get_ref())).unwrap(); + String::from_utf8(Vec::from_slice(wr.get_ref())).unwrap(); mem::forget(wr); result.to_string() } diff --git a/src/libterm/terminfo/parser/compiled.rs b/src/libterm/terminfo/parser/compiled.rs index 9467eb6992170..94ed7fbbf306e 100644 --- a/src/libterm/terminfo/parser/compiled.rs +++ b/src/libterm/terminfo/parser/compiled.rs @@ -14,7 +14,6 @@ use std::collections::HashMap; use std::io; -use std::str; use super::super::TermInfo; // These are the orders ncurses uses in its compiled format (as of 5.9). Not sure if portable. @@ -214,9 +213,9 @@ pub fn parse(file: &mut io::Reader, longnames: bool) // don't read NUL let bytes = try!(file.read_exact(names_bytes as uint - 1)); - let names_str = match str::from_utf8(bytes.as_slice()) { - Some(s) => s.to_string(), - None => return Err("input not utf-8".to_string()), + let names_str = match String::from_utf8(bytes) { + Ok(s) => s, + Err(_) => return Err("input not utf-8".to_string()), }; let term_names: Vec = names_str.as_slice() diff --git a/src/libterm/terminfo/searcher.rs b/src/libterm/terminfo/searcher.rs index 0002b20d205ca..7ad14d7975493 100644 --- a/src/libterm/terminfo/searcher.rs +++ b/src/libterm/terminfo/searcher.rs @@ -14,7 +14,7 @@ use std::io::File; use std::os::getenv; -use std::{os, str}; +use std::os; /// Return path to database entry for `term` pub fn get_dbpath_for_term(term: &str) -> Option> { @@ -59,7 +59,7 @@ pub fn get_dbpath_for_term(term: &str) -> Option> { // Look for the terminal in all of the search directories for p in dirs_to_search.iter() { if p.exists() { - let f = str::from_char(first_char); + let f = first_char.to_string(); let newp = p.join_many([f.as_slice(), term]); if newp.exists() { return Some(box newp); diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 8fbb821d0b96e..a857bc7535d9c 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -60,7 +60,6 @@ use std::io::stdio::StdWriter; use std::io::{File, ChanReader, ChanWriter}; use std::io; use std::os; -use std::str; use std::string::String; use std::task::TaskBuilder; @@ -636,7 +635,7 @@ impl ConsoleTestState { if stdout.len() > 0 { fail_out.push_str(format!("---- {} stdout ----\n\t", f.name.as_slice()).as_slice()); - let output = str::from_utf8_lossy(stdout.as_slice()); + let output = String::from_utf8_lossy(stdout.as_slice()); fail_out.push_str(output.as_slice() .replace("\n", "\n\t") .as_slice()); @@ -873,7 +872,7 @@ fn should_sort_failures_before_printing_them() { st.write_failures().unwrap(); let s = match st.out { - Raw(ref m) => str::from_utf8_lossy(m.get_ref()), + Raw(ref m) => String::from_utf8_lossy(m.get_ref()), Pretty(_) => unreachable!() }; diff --git a/src/libtest/stats.rs b/src/libtest/stats.rs index 325582ff99c98..5169652116532 100644 --- a/src/libtest/stats.rs +++ b/src/libtest/stats.rs @@ -457,7 +457,6 @@ mod tests { use stats::write_5_number_summary; use stats::write_boxplot; use std::io; - use std::str; use std::f64; macro_rules! assert_approx_eq( @@ -1030,7 +1029,7 @@ mod tests { use std::io::MemWriter; let mut m = MemWriter::new(); write_boxplot(&mut m as &mut io::Writer, s, 30).unwrap(); - let out = str::from_utf8(m.unwrap().as_slice()).unwrap().to_string(); + let out = String::from_utf8(m.unwrap()).unwrap(); assert_eq!(out, expected); } diff --git a/src/libtime/lib.rs b/src/libtime/lib.rs index 7655ace0ecb08..41ba448754d08 100644 --- a/src/libtime/lib.rs +++ b/src/libtime/lib.rs @@ -31,7 +31,6 @@ extern crate libc; use std::io::BufReader; use std::num; use std::string::String; -use std::str; static NSEC_PER_SEC: i32 = 1_000_000_000_i32; @@ -486,9 +485,7 @@ pub fn strptime(s: &str, format: &str) -> Result { if c == range.ch { Ok(range.next) } else { - Err(format!("Expected {}, found {}", - str::from_char(c), - str::from_char(range.ch))) + Err(format!("Expected {}, found {}", c, range.ch)) } } @@ -789,7 +786,7 @@ pub fn strptime(s: &str, format: &str) -> Result { } '%' => parse_char(s, pos, '%'), ch => { - Err(format!("unknown formatting type: {}", str::from_char(ch))) + Err(format!("unknown formatting type: {}", ch)) } } } @@ -1086,7 +1083,7 @@ pub fn strftime(format: &str, tm: &Tm) -> String { } } - str::from_utf8(buf.as_slice()).unwrap().to_string() + String::from_utf8(buf).unwrap() } #[cfg(test)] diff --git a/src/libuuid/lib.rs b/src/libuuid/lib.rs index 1f84306f9ccc9..233743175b503 100644 --- a/src/libuuid/lib.rs +++ b/src/libuuid/lib.rs @@ -81,7 +81,6 @@ use std::num::FromStrRadix; use std::rand; use std::rand::Rng; use std::slice; -use std::str; use serialize::{Encoder, Encodable, Decoder, Decodable}; @@ -327,7 +326,7 @@ impl Uuid { *s.get_mut(i*2+0) = digit.as_bytes()[0]; *s.get_mut(i*2+1) = digit.as_bytes()[1]; } - str::from_utf8(s.as_slice()).unwrap().to_string() + String::from_utf8(s).unwrap() } /// Returns a string of hexadecimal digits, separated into groups with a hyphen. @@ -685,7 +684,7 @@ mod test { let hs = uuid1.to_hyphenated_str(); let ss = uuid1.to_string(); - let hsn = str::from_chars(hs.as_slice() + let hsn = String::from_chars(hs.as_slice() .chars() .filter(|&c| c != '-') .collect::>() diff --git a/src/test/run-fail/glob-use-std.rs b/src/test/run-fail/glob-use-std.rs index 458a95b91cf9c..fffe146f7f4d4 100644 --- a/src/test/run-fail/glob-use-std.rs +++ b/src/test/run-fail/glob-use-std.rs @@ -16,7 +16,7 @@ use std::*; fn main() { - str::from_byte('a' as u8); // avoid an unused import message + String::from_byte(b'a'); // avoid an unused import message fail!("fail works") } diff --git a/src/test/run-make/unicode-input/multiple_files.rs b/src/test/run-make/unicode-input/multiple_files.rs index 314949cc59312..c436958171d3b 100644 --- a/src/test/run-make/unicode-input/multiple_files.rs +++ b/src/test/run-make/unicode-input/multiple_files.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::{char, os, str}; +use std::{char, os}; use std::io::{File, Command}; use std::rand::{task_rng, Rng}; @@ -61,7 +61,7 @@ fn main() { main_file.as_str() .unwrap()).as_slice()) .output().unwrap(); - let err = str::from_utf8_lossy(result.error.as_slice()); + let err = String::from_utf8_lossy(result.error.as_slice()); // positive test so that this test will be updated when the // compiler changes. diff --git a/src/test/run-make/unicode-input/span_length.rs b/src/test/run-make/unicode-input/span_length.rs index 903ca69f247f2..913f1318ebf84 100644 --- a/src/test/run-make/unicode-input/span_length.rs +++ b/src/test/run-make/unicode-input/span_length.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::{char, os, str}; +use std::{char, os}; use std::io::{File, Command}; use std::rand::{task_rng, Rng}; @@ -57,7 +57,7 @@ fn main() { .unwrap()).as_slice()) .output().unwrap(); - let err = str::from_utf8_lossy(result.error.as_slice()); + let err = String::from_utf8_lossy(result.error.as_slice()); // the span should end the line (e.g no extra ~'s) let expected_span = format!("^{}\n", "~".repeat(n - 1)); diff --git a/src/test/run-pass/issue-11881.rs b/src/test/run-pass/issue-11881.rs index 913e09dd8cb35..e0cd91adb1c59 100644 --- a/src/test/run-pass/issue-11881.rs +++ b/src/test/run-pass/issue-11881.rs @@ -14,7 +14,6 @@ use serialize::{Encodable, Encoder}; use serialize::json; use serialize::ebml::writer; use std::io::MemWriter; -use std::str::from_utf8_owned; #[deriving(Encodable)] struct Foo { diff --git a/src/test/run-pass/issue-3563-3.rs b/src/test/run-pass/issue-3563-3.rs index e38969c25263c..84f303de7057b 100644 --- a/src/test/run-pass/issue-3563-3.rs +++ b/src/test/run-pass/issue-3563-3.rs @@ -20,7 +20,6 @@ // Extern mod controls linkage. Use controls the visibility of names to modules that are // already linked in. Using WriterUtil allows us to use the write_line method. -use std::str; use std::slice; use std::fmt; @@ -100,9 +99,7 @@ impl fmt::Show for AsciiArt { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Convert each line into a string. let lines = self.lines.iter() - .map(|line| { - str::from_chars(line.as_slice()).to_string() - }) + .map(|line| String::from_chars(line.as_slice())) .collect::>(); // Concatenate the lines together using a new-line. diff --git a/src/test/run-pass/out-of-stack.rs b/src/test/run-pass/out-of-stack.rs index 19d1635a9a5e1..7f2f9f9ece83d 100644 --- a/src/test/run-pass/out-of-stack.rs +++ b/src/test/run-pass/out-of-stack.rs @@ -12,7 +12,6 @@ use std::io::process::Command; use std::os; -use std::str; // lifted from the test module // Inlining to avoid llvm turning the recursive functions into tail calls, @@ -42,12 +41,12 @@ fn main() { } else { let silent = Command::new(args[0].as_slice()).arg("silent").output().unwrap(); assert!(!silent.status.success()); - let error = str::from_utf8_lossy(silent.error.as_slice()); + let error = String::from_utf8_lossy(silent.error.as_slice()); assert!(error.as_slice().contains("has overflowed its stack")); let loud = Command::new(args[0].as_slice()).arg("loud").output().unwrap(); assert!(!loud.status.success()); - let error = str::from_utf8_lossy(silent.error.as_slice()); + let error = String::from_utf8_lossy(silent.error.as_slice()); assert!(error.as_slice().contains("has overflowed its stack")); } } diff --git a/src/test/run-pass/utf8_chars.rs b/src/test/run-pass/utf8_chars.rs index 93c8111ad2d1f..d26a17d19f807 100644 --- a/src/test/run-pass/utf8_chars.rs +++ b/src/test/run-pass/utf8_chars.rs @@ -13,13 +13,13 @@ use std::str; pub fn main() { // Chars of 1, 2, 3, and 4 bytes let chs: Vec = vec!('e', 'รฉ', 'โ‚ฌ', '\U00010000'); - let s: String = str::from_chars(chs.as_slice()).to_string(); + let s: String = String::from_chars(chs.as_slice()).to_string(); let schs: Vec = s.as_slice().chars().collect(); assert!(s.len() == 10u); assert!(s.as_slice().char_len() == 4u); assert!(schs.len() == 4u); - assert!(str::from_chars(schs.as_slice()).to_string() == s); + assert!(String::from_chars(schs.as_slice()) == s); assert!(s.as_slice().char_at(0u) == 'e'); assert!(s.as_slice().char_at(1u) == 'รฉ');