From 9d64e46013096997627da62ecc65225bc22682e8 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 28 Nov 2013 23:52:11 +1100 Subject: std::str: remove from_utf8. This function had type &[u8] -> ~str, i.e. it allocates a string internally, even though the non-allocating version that take &[u8] -> &str and ~[u8] -> ~str are all that is necessary in most circumstances. --- src/libstd/io/flate.rs | 6 +-- src/libstd/io/fs.rs | 39 ++++++++-------- src/libstd/num/strconv.rs | 2 +- src/libstd/run.rs | 16 +++---- src/libstd/str.rs | 114 +++------------------------------------------- 5 files changed, 37 insertions(+), 140 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/io/flate.rs b/src/libstd/io/flate.rs index 8a5aa171eb8..6548c0e65c9 100644 --- a/src/libstd/io/flate.rs +++ b/src/libstd/io/flate.rs @@ -107,7 +107,7 @@ mod test { fn smoke_test() { let mem_writer = MemWriter::new(); let mut deflate_writer = DeflateWriter::new(mem_writer); - let in_msg = "test"; + let in_msg: &str = "test"; let in_bytes = in_msg.as_bytes(); deflate_writer.write(in_bytes); deflate_writer.flush(); @@ -117,7 +117,7 @@ mod test { let mut out_bytes = [0, .. 100]; let bytes_read = inflate_reader.read(out_bytes).unwrap(); assert_eq!(bytes_read, in_bytes.len()); - let out_msg = str::from_utf8(out_bytes); - assert!(in_msg == out_msg); + let out_msg = str::from_utf8_slice(out_bytes); + assert_eq!(in_msg, out_msg); } } diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index e239f630f01..db8de9df24a 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -770,9 +770,9 @@ mod test { let mut read_buf = [0, .. 1028]; let read_str = match read_stream.read(read_buf).unwrap() { -1|0 => fail!("shouldn't happen"), - n => str::from_utf8(read_buf.slice_to(n)) + n => str::from_utf8_owned(read_buf.slice_to(n).to_owned()) }; - assert!(read_str == message.to_owned()); + assert_eq!(read_str, message.to_owned()); } unlink(filename); }) @@ -801,7 +801,7 @@ mod test { }) test!(fn file_test_io_non_positional_read() { - let message = "ten-four"; + let message: &str = "ten-four"; let mut read_mem = [0, .. 8]; let tmpdir = tmpdir(); let filename = &tmpdir.join("file_rt_io_file_test_positional.txt"); @@ -821,8 +821,8 @@ mod test { } } unlink(filename); - let read_str = str::from_utf8(read_mem); - assert!(read_str == message.to_owned()); + let read_str = str::from_utf8_slice(read_mem); + assert_eq!(read_str, message); }) test!(fn file_test_io_seek_and_tell_smoke_test() { @@ -845,10 +845,10 @@ mod test { tell_pos_post_read = read_stream.tell(); } unlink(filename); - let read_str = str::from_utf8(read_mem); - assert!(read_str == message.slice(4, 8).to_owned()); - assert!(tell_pos_pre_read == set_cursor); - assert!(tell_pos_post_read == message.len() as u64); + let read_str = str::from_utf8_slice(read_mem); + assert_eq!(read_str, message.slice(4, 8)); + assert_eq!(tell_pos_pre_read, set_cursor); + assert_eq!(tell_pos_post_read, message.len() as u64); }) test!(fn file_test_io_seek_and_write() { @@ -870,16 +870,16 @@ mod test { read_stream.read(read_mem); } unlink(filename); - let read_str = str::from_utf8(read_mem); + let read_str = str::from_utf8_slice(read_mem); assert!(read_str == final_msg.to_owned()); }) test!(fn file_test_io_seek_shakedown() { use std::str; // 01234567890123 let initial_msg = "qwer-asdf-zxcv"; - let chunk_one = "qwer"; - let chunk_two = "asdf"; - let chunk_three = "zxcv"; + let chunk_one: &str = "qwer"; + let chunk_two: &str = "asdf"; + let chunk_three: &str = "zxcv"; let mut read_mem = [0, .. 4]; let tmpdir = tmpdir(); let filename = &tmpdir.join("file_rt_io_file_test_seek_shakedown.txt"); @@ -892,18 +892,15 @@ mod test { read_stream.seek(-4, SeekEnd); read_stream.read(read_mem); - let read_str = str::from_utf8(read_mem); - assert!(read_str == chunk_three.to_owned()); + assert_eq!(str::from_utf8_slice(read_mem), chunk_three); read_stream.seek(-9, SeekCur); read_stream.read(read_mem); - let read_str = str::from_utf8(read_mem); - assert!(read_str == chunk_two.to_owned()); + assert_eq!(str::from_utf8_slice(read_mem), chunk_two); read_stream.seek(0, SeekSet); read_stream.read(read_mem); - let read_str = str::from_utf8(read_mem); - assert!(read_str == chunk_one.to_owned()); + assert_eq!(str::from_utf8_slice(read_mem), chunk_one); } unlink(filename); }) @@ -977,12 +974,12 @@ mod test { { let n = f.filestem_str(); File::open(f).read(mem); - let read_str = str::from_utf8(mem); + let read_str = str::from_utf8_slice(mem); let expected = match n { None|Some("") => fail!("really shouldn't happen.."), Some(n) => prefix+n }; - assert!(expected == read_str); + assert_eq!(expected.as_slice(), read_str); } unlink(f); } diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 1028cef9dc6..8e678ab66b2 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -426,7 +426,7 @@ pub fn float_to_str_common (~str, bool) { let (bytes, special) = float_to_str_bytes_common(num, radix, negative_zero, sign, digits); - (str::from_utf8(bytes), special) + (str::from_utf8_owned(bytes), special) } // Some constants for from_str_bytes_common's input validation, diff --git a/src/libstd/run.rs b/src/libstd/run.rs index a22f536974b..6cc5e5cc9f2 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -347,7 +347,7 @@ mod tests { let run::ProcessOutput {status, output, error} = run::process_output("echo", [~"hello"]); - let output_str = str::from_utf8(output); + let output_str = str::from_utf8_owned(output); assert!(status.success()); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -439,7 +439,7 @@ mod tests { let mut prog = run::Process::new("echo", [~"hello"], run::ProcessOptions::new()); let run::ProcessOutput {status, output, error} = prog.finish_with_output(); - let output_str = str::from_utf8(output); + let output_str = str::from_utf8_owned(output); assert!(status.success()); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -457,7 +457,7 @@ mod tests { let run::ProcessOutput {status, output, error} = prog.finish_with_output(); - let output_str = str::from_utf8(output); + let output_str = str::from_utf8_owned(output); assert!(status.success()); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -504,7 +504,7 @@ mod tests { fn test_keep_current_working_dir() { let mut prog = run_pwd(None); - let output = str::from_utf8(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output); let parent_dir = os::getcwd(); let child_dir = Path::init(output.trim()); @@ -522,7 +522,7 @@ mod tests { let parent_dir = os::getcwd().dir_path(); let mut prog = run_pwd(Some(&parent_dir)); - let output = str::from_utf8(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output); let child_dir = Path::init(output.trim()); let parent_stat = parent_dir.stat(); @@ -561,7 +561,7 @@ mod tests { if running_on_valgrind() { return; } let mut prog = run_env(None); - let output = str::from_utf8(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output); let r = os::env(); for &(ref k, ref v) in r.iter() { @@ -575,7 +575,7 @@ mod tests { if running_on_valgrind() { return; } let mut prog = run_env(None); - let output = str::from_utf8(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output); let r = os::env(); for &(ref k, ref v) in r.iter() { @@ -594,7 +594,7 @@ mod tests { new_env.push((~"RUN_TEST_NEW_ENV", ~"123")); let mut prog = run_env(Some(new_env)); - let output = str::from_utf8(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output); assert!(output.contains("RUN_TEST_NEW_ENV=123")); } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index f65ec6971ab..2076d433fb6 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -124,34 +124,6 @@ condition! { Section: Creating a string */ -/// Convert a vector of bytes to a new UTF-8 string -/// -/// # Failure -/// -/// Raises the `not_utf8` condition if invalid UTF-8 -pub fn from_utf8(vv: &[u8]) -> ~str { - use str::not_utf8::cond; - - match from_utf8_opt(vv) { - None => { - let first_bad_byte = *vv.iter().find(|&b| !is_utf8([*b])).unwrap(); - cond.raise(format!("from_utf8: input is not UTF-8; first bad \ - byte is {}", first_bad_byte)) - } - Some(s) => s - } -} - -/// Convert a vector of bytes to a new UTF-8 string, if possible. -/// Returns None if the vector contains invalid UTF-8. -pub fn from_utf8_opt(vv: &[u8]) -> Option<~str> { - if is_utf8(vv) { - Some(unsafe { raw::from_utf8(vv) }) - } else { - None - } -} - /// Consumes a vector of bytes to create a new utf-8 string /// /// # Failure @@ -196,7 +168,7 @@ pub fn from_utf8_slice<'a>(v: &'a [u8]) -> &'a str { /// Returns None if the slice is not utf-8. pub fn from_utf8_slice_opt<'a>(v: &'a [u8]) -> Option<&'a str> { if is_utf8(v) { - Some(unsafe { cast::transmute(v) }) + Some(unsafe { raw::from_utf8_slice(v) }) } else { None } } @@ -1055,9 +1027,10 @@ pub mod raw { from_buf_len(buf as *u8, i as uint) } - /// Converts a vector of bytes to a new owned string. - pub unsafe fn from_utf8(v: &[u8]) -> ~str { - v.as_imm_buf(|buf, len| from_buf_len(buf, len)) + /// Converts a slice of bytes to a string slice without checking + /// that the string contains valid UTF-8. + pub unsafe fn from_utf8_slice<'a>(v: &'a [u8]) -> &'a str { + cast::transmute(v) } /// Converts an owned vector of bytes to a new owned string. This assumes @@ -1068,7 +1041,7 @@ pub mod raw { } /// Converts a byte to a string. - pub unsafe fn from_byte(u: u8) -> ~str { from_utf8([u]) } + pub unsafe fn from_byte(u: u8) -> ~str { from_utf8_owned(~[u]) } /// Form a slice from a C string. Unsafe because the caller must ensure the /// C string has the static lifetime, or else the return value may be @@ -3077,33 +3050,6 @@ mod tests { assert_eq!(b, 67u8); } - #[test] - fn test_unsafe_from_utf8() { - let a = ~[65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8]; - let b = unsafe { raw::from_utf8(a) }; - assert_eq!(b, ~"AAAAAAA"); - } - - #[test] - fn test_from_utf8() { - let ss = ~"ศไทย中华Việt Nam"; - let bb = ~[0xe0_u8, 0xb8_u8, 0xa8_u8, - 0xe0_u8, 0xb9_u8, 0x84_u8, - 0xe0_u8, 0xb8_u8, 0x97_u8, - 0xe0_u8, 0xb8_u8, 0xa2_u8, - 0xe4_u8, 0xb8_u8, 0xad_u8, - 0xe5_u8, 0x8d_u8, 0x8e_u8, - 0x56_u8, 0x69_u8, 0xe1_u8, - 0xbb_u8, 0x87_u8, 0x74_u8, - 0x20_u8, 0x4e_u8, 0x61_u8, - 0x6d_u8]; - - - assert_eq!(ss, from_utf8(bb)); - assert_eq!(~"𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰", - from_utf8(bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰"))); - } - #[test] fn test_is_utf8() { // deny overlong encodings @@ -3129,31 +3075,6 @@ mod tests { assert!(is_utf8([0xF4, 0x8F, 0xBF, 0xBF])); } - - #[test] - fn test_from_utf8_fail() { - use str::not_utf8::cond; - - let bb = ~[0xff_u8, 0xb8_u8, 0xa8_u8, - 0xe0_u8, 0xb9_u8, 0x84_u8, - 0xe0_u8, 0xb8_u8, 0x97_u8, - 0xe0_u8, 0xb8_u8, 0xa2_u8, - 0xe4_u8, 0xb8_u8, 0xad_u8, - 0xe5_u8, 0x8d_u8, 0x8e_u8, - 0x56_u8, 0x69_u8, 0xe1_u8, - 0xbb_u8, 0x87_u8, 0x74_u8, - 0x20_u8, 0x4e_u8, 0x61_u8, - 0x6d_u8]; - - let mut error_happened = false; - let _x = cond.trap(|err| { - assert_eq!(err, ~"from_utf8: input is not UTF-8; first bad byte is 255"); - error_happened = true; - ~"" - }).inside(|| from_utf8(bb)); - assert!(error_happened); - } - #[test] fn test_raw_from_c_str() { unsafe { @@ -3232,7 +3153,7 @@ mod tests { let s1: ~str = ~"All mimsy were the borogoves"; let v: ~[u8] = s1.as_bytes().to_owned(); - let s2: ~str = from_utf8(v); + let s2: ~str = from_utf8_slice(v).to_owned(); let mut i: uint = 0u; let n1: uint = s1.len(); let n2: uint = v.len(); @@ -3782,27 +3703,6 @@ mod tests { assert_eq!(from_utf8_slice_opt(xs), None); } - #[test] - fn test_str_from_utf8() { - let xs = bytes!("hello"); - assert_eq!(from_utf8(xs), ~"hello"); - - let xs = bytes!("ศไทย中华Việt Nam"); - assert_eq!(from_utf8(xs), ~"ศไทย中华Việt Nam"); - } - - #[test] - fn test_str_from_utf8_opt() { - let xs = bytes!("hello").to_owned(); - assert_eq!(from_utf8_opt(xs), Some(~"hello")); - - let xs = bytes!("ศไทย中华Việt Nam"); - assert_eq!(from_utf8_opt(xs), Some(~"ศไทย中华Việt Nam")); - - let xs = bytes!("hello", 0xff); - assert_eq!(from_utf8_opt(xs), None); - } - #[test] fn test_str_from_utf8_owned() { let xs = bytes!("hello").to_owned(); -- cgit 1.4.1-3-g733a5 From b0426edc0a83699de79ceffcbe603812b9b53374 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Mon, 2 Dec 2013 00:33:04 +1100 Subject: std::str: s/from_utf8_slice/from_utf8/, to make the basic case shorter. --- src/compiletest/runtest.rs | 2 +- src/libextra/ebml.rs | 2 +- src/libextra/test.rs | 2 +- src/librustc/back/archive.rs | 4 ++-- src/librustc/metadata/tydecode.rs | 4 ++-- src/librustc/metadata/tyencode.rs | 2 +- src/librustpkg/lib.rs | 2 +- src/librustpkg/tests.rs | 32 ++++++++++++++++---------------- src/librustpkg/version.rs | 8 ++++---- src/librustuv/file.rs | 2 +- src/librustuv/net.rs | 2 +- src/libstd/c_str.rs | 2 +- src/libstd/fmt/mod.rs | 2 +- src/libstd/io/flate.rs | 2 +- src/libstd/io/fs.rs | 14 +++++++------- src/libstd/io/mod.rs | 2 +- src/libstd/path/mod.rs | 14 +++++++------- src/libstd/path/posix.rs | 6 +++--- src/libstd/str.rs | 30 +++++++++++++++--------------- src/test/run-pass/const-str-ptr.rs | 4 ++-- src/test/run-pass/rtio-processes.rs | 2 +- 21 files changed, 70 insertions(+), 70 deletions(-) (limited to 'src/libstd') diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index e91652365c3..726fbcb5d7a 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -298,7 +298,7 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) { let adb_arg = format!("export LD_LIBRARY_PATH={}; gdbserver :5039 {}/{}", config.adb_test_dir.clone(), config.adb_test_dir.clone(), - str::from_utf8_slice(exe_file.filename().unwrap())); + str::from_utf8(exe_file.filename().unwrap())); let mut process = procsrv::run_background("", config.adb_path.clone(), [~"shell",adb_arg.clone()],~[(~"",~"")], Some(~"")); diff --git a/src/libextra/ebml.rs b/src/libextra/ebml.rs index 19959dd2705..aadb93f2e24 100644 --- a/src/libextra/ebml.rs +++ b/src/libextra/ebml.rs @@ -41,7 +41,7 @@ impl Doc { } pub fn as_str_slice<'a>(&'a self) -> &'a str { - str::from_utf8_slice(self.data.slice(self.start, self.end)) + str::from_utf8(self.data.slice(self.start, self.end)) } pub fn as_str(&self) -> ~str { diff --git a/src/libextra/test.rs b/src/libextra/test.rs index 693f4532a43..93ffb8f9fde 100644 --- a/src/libextra/test.rs +++ b/src/libextra/test.rs @@ -701,7 +701,7 @@ fn should_sort_failures_before_printing_them() { st.write_failures(); let s = match st.out { - Right(ref m) => str::from_utf8_slice(*m.inner_ref()), + Right(ref m) => str::from_utf8(*m.inner_ref()), Left(_) => unreachable!() }; diff --git a/src/librustc/back/archive.rs b/src/librustc/back/archive.rs index 2a84f01cbd6..4711381a7b8 100644 --- a/src/librustc/back/archive.rs +++ b/src/librustc/back/archive.rs @@ -41,8 +41,8 @@ fn run_ar(sess: Session, args: &str, cwd: Option<&Path>, let o = Process::new(ar, args.as_slice(), opts).finish_with_output(); if !o.status.success() { sess.err(format!("{} failed with: {}", ar, o.status)); - sess.note(format!("stdout ---\n{}", str::from_utf8_slice(o.output))); - sess.note(format!("stderr ---\n{}", str::from_utf8_slice(o.error))); + sess.note(format!("stdout ---\n{}", str::from_utf8(o.output))); + sess.note(format!("stderr ---\n{}", str::from_utf8(o.error))); sess.abort_if_errors(); } o diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index 74452a6217b..cbf3dcf2781 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -98,7 +98,7 @@ pub fn parse_ident(st: &mut PState, last: char) -> ast::Ident { fn parse_ident_(st: &mut PState, is_last: |char| -> bool) -> ast::Ident { scan(st, is_last, |bytes| { - st.tcx.sess.ident_of(str::from_utf8_slice(bytes)) + st.tcx.sess.ident_of(str::from_utf8(bytes)) }) } @@ -494,7 +494,7 @@ fn parse_abi_set(st: &mut PState) -> AbiSet { let mut abis = AbiSet::empty(); while peek(st) != ']' { scan(st, |c| c == ',', |bytes| { - let abi_str = str::from_utf8_slice(bytes).to_owned(); + let abi_str = str::from_utf8(bytes).to_owned(); let abi = abi::lookup(abi_str).expect(abi_str); abis.add(abi); }); diff --git a/src/librustc/metadata/tyencode.rs b/src/librustc/metadata/tyencode.rs index 1e8a6d85975..19a9a7efc57 100644 --- a/src/librustc/metadata/tyencode.rs +++ b/src/librustc/metadata/tyencode.rs @@ -72,7 +72,7 @@ pub fn enc_ty(w: @mut MemWriter, cx: @ctxt, t: ty::t) { None => { let wr = @mut MemWriter::new(); enc_sty(wr, cx, &ty::get(t).sty); - let s = str::from_utf8_slice(*wr.inner_ref()).to_managed(); + let s = str::from_utf8(*wr.inner_ref()).to_managed(); cx.tcx.short_names_cache.insert(t, s); s } diff --git a/src/librustpkg/lib.rs b/src/librustpkg/lib.rs index 35b6cc3fe64..22da8d6d8a8 100644 --- a/src/librustpkg/lib.rs +++ b/src/librustpkg/lib.rs @@ -184,7 +184,7 @@ impl<'self> PkgScript<'self> { [sysroot.as_str().unwrap().to_owned(), ~"configs"]); debug!("run_custom: second pkg command did {:?}", output.status); // Run the configs() function to get the configs - let cfgs = str::from_utf8_slice(output.output).words() + let cfgs = str::from_utf8(output.output).words() .map(|w| w.to_owned()).collect(); (cfgs, output.status) } diff --git a/src/librustpkg/tests.rs b/src/librustpkg/tests.rs index 1932182560a..17793f23286 100644 --- a/src/librustpkg/tests.rs +++ b/src/librustpkg/tests.rs @@ -154,7 +154,7 @@ fn run_git(args: &[~str], env: Option<~[(~str, ~str)]>, cwd: &Path, err_msg: &st let rslt = prog.finish_with_output(); if !rslt.status.success() { fail!("{} [git returned {:?}, output = {}, error = {}]", err_msg, - rslt.status, str::from_utf8_slice(rslt.output), str::from_utf8_slice(rslt.error)); + rslt.status, str::from_utf8(rslt.output), str::from_utf8(rslt.error)); } } @@ -290,13 +290,13 @@ fn command_line_test_with_env(args: &[~str], cwd: &Path, env: Option<~[(~str, ~s }); let output = prog.finish_with_output(); debug!("Output from command {} with args {:?} was {} \\{{}\\}[{:?}]", - cmd, args, str::from_utf8_slice(output.output), - str::from_utf8_slice(output.error), + cmd, args, str::from_utf8(output.output), + str::from_utf8(output.error), output.status); if !output.status.success() { - debug!("Command {} {:?} failed with exit code {:?}; its output was --- {} ---", + debug!("Command {} {:?} failed with exit code {:?}; its output was --- {} {} ---", cmd, args, output.status, - str::from_utf8_slice(output.output) + str::from_utf8_slice(output.error)); + str::from_utf8(output.output), str::from_utf8(output.error)); Fail(output) } else { @@ -455,7 +455,7 @@ fn built_library_exists(repo: &Path, short_name: &str) -> bool { fn command_line_test_output(args: &[~str]) -> ~[~str] { let mut result = ~[]; let p_output = command_line_test(args, &os::getcwd()); - let test_output = str::from_utf8_slice(p_output.output); + let test_output = str::from_utf8(p_output.output); for s in test_output.split('\n') { result.push(s.to_owned()); } @@ -469,7 +469,7 @@ fn command_line_test_output_with_env(args: &[~str], env: ~[(~str, ~str)]) -> ~[~ Fail(_) => fail!("Command-line test failed"), Success(r) => r }; - let test_output = str::from_utf8_slice(p_output.output); + let test_output = str::from_utf8(p_output.output); for s in test_output.split('\n') { result.push(s.to_owned()); } @@ -1212,7 +1212,7 @@ fn test_uninstall() { let workspace = create_local_package(&PkgId::new("foo")); command_line_test([~"uninstall", ~"foo"], workspace.path()); let output = command_line_test([~"list"], workspace.path()); - assert!(!str::from_utf8_slice(output.output).contains("foo")); + assert!(!str::from_utf8(output.output).contains("foo")); } #[test] @@ -1282,8 +1282,8 @@ fn test_extern_mod() { let outp = prog.finish_with_output(); if !outp.status.success() { fail!("output was {}, error was {}", - str::from_utf8_slice(outp.output), - str::from_utf8_slice(outp.error)); + str::from_utf8(outp.output), + str::from_utf8(outp.error)); } assert!(exec_file.exists() && is_executable(&exec_file)); } @@ -1337,8 +1337,8 @@ fn test_extern_mod_simpler() { let outp = prog.finish_with_output(); if !outp.status.success() { fail!("output was {}, error was {}", - str::from_utf8_slice(outp.output), - str::from_utf8_slice(outp.error)); + str::from_utf8(outp.output), + str::from_utf8(outp.error)); } assert!(exec_file.exists() && is_executable(&exec_file)); } @@ -2101,7 +2101,7 @@ fn test_rustpkg_test_creates_exec() { fn test_rustpkg_test_output() { let workspace = create_local_package_with_test(&PkgId::new("foo")); let output = command_line_test([~"test", ~"foo"], workspace.path()); - let output_str = str::from_utf8_slice(output.output); + let output_str = str::from_utf8(output.output); // The first two assertions are separate because test output may // contain color codes, which could appear between "test f" and "ok". assert!(output_str.contains("test f")); @@ -2132,7 +2132,7 @@ fn test_rustpkg_test_cfg() { "#[test] #[cfg(not(foobar))] fn f() { assert!('a' != 'a'); }"); let output = command_line_test([~"test", ~"--cfg", ~"foobar", ~"foo"], foo_workspace); - let output_str = str::from_utf8_slice(output.output); + let output_str = str::from_utf8(output.output); assert!(output_str.contains("0 passed; 0 failed; 0 ignored; 0 measured")); } @@ -2430,8 +2430,8 @@ fn correct_error_dependency() { Fail(ProcessOutput{ error: error, output: output, .. }) => { assert!(str::is_utf8(error)); assert!(str::is_utf8(output)); - let error_str = str::from_utf8_slice(error); - let out_str = str::from_utf8_slice(output); + let error_str = str::from_utf8(error); + let out_str = str::from_utf8(output); debug!("ss = {}", error_str); debug!("out_str = {}", out_str); if out_str.contains("Package badpkg depends on some_package_that_doesnt_exist") && diff --git a/src/librustpkg/version.rs b/src/librustpkg/version.rs index f11f5962c39..eced433868f 100644 --- a/src/librustpkg/version.rs +++ b/src/librustpkg/version.rs @@ -114,7 +114,7 @@ pub fn try_getting_local_version(local_path: &Path) -> Option { } let mut output = None; - let output_text = str::from_utf8_slice(outp.output); + let output_text = str::from_utf8(outp.output); for l in output_text.lines() { if !l.is_whitespace() { output = Some(l); @@ -145,8 +145,8 @@ pub fn try_getting_version(remote_path: &Path) -> Option { tmp_dir.as_str().unwrap().to_owned()]); if outp.status.success() { debug!("Cloned it... ( {}, {} )", - str::from_utf8_slice(outp.output), - str::from_utf8_slice(outp.error)); + str::from_utf8(outp.output), + str::from_utf8(outp.error)); let mut output = None; let git_dir = tmp_dir.join(".git"); debug!("(getting version, now getting tags) executing \\{git --git-dir={} tag -l\\}", @@ -155,7 +155,7 @@ pub fn try_getting_version(remote_path: &Path) -> Option { let outp = run::process_output("git", ["--git-dir=" + git_dir.as_str().unwrap(), ~"tag", ~"-l"]); - let output_text = str::from_utf8_slice(outp.output); + let output_text = str::from_utf8(outp.output); debug!("Full output: ( {} ) [{:?}]", output_text, outp.status); for l in output_text.lines() { debug!("A line of output: {}", l); diff --git a/src/librustuv/file.rs b/src/librustuv/file.rs index d2ecb92fffe..78454f3e0d0 100644 --- a/src/librustuv/file.rs +++ b/src/librustuv/file.rs @@ -487,7 +487,7 @@ mod test { let nread = result.unwrap(); assert!(nread > 0); - let read_str = str::from_utf8_slice(read_mem.slice_to(nread as uint)); + let read_str = str::from_utf8(read_mem.slice_to(nread as uint)); assert_eq!(read_str, "hello"); } // unlink diff --git a/src/librustuv/net.rs b/src/librustuv/net.rs index a7feb6db923..0abd476d7b9 100644 --- a/src/librustuv/net.rs +++ b/src/librustuv/net.rs @@ -81,7 +81,7 @@ pub fn sockaddr_to_socket_addr(addr: *sockaddr) -> SocketAddr { }; port as u16 }; - let ip_str = str::from_utf8_slice(ip_name).trim_right_chars(&'\x00'); + let ip_str = str::from_utf8(ip_name).trim_right_chars(&'\x00'); let ip_addr = FromStr::from_str(ip_str).unwrap(); SocketAddr { ip: ip_addr, port: ip_port } diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index 306ee331929..11845c766ed 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -167,7 +167,7 @@ impl CString { if self.buf.is_null() { return None; } let buf = self.as_bytes(); let buf = buf.slice_to(buf.len()-1); // chop off the trailing NUL - str::from_utf8_slice_opt(buf) + str::from_utf8_opt(buf) } /// Return a CString iterator. diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index c74a9bc9051..463540b3677 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -802,7 +802,7 @@ impl<'self> Formatter<'self> { fn runplural(&mut self, value: uint, pieces: &[rt::Piece]) { ::uint::to_str_bytes(value, 10, |buf| { - let valuestr = str::from_utf8_slice(buf); + let valuestr = str::from_utf8(buf); for piece in pieces.iter() { self.run(piece, Some(valuestr)); } diff --git a/src/libstd/io/flate.rs b/src/libstd/io/flate.rs index 6548c0e65c9..4a31449e105 100644 --- a/src/libstd/io/flate.rs +++ b/src/libstd/io/flate.rs @@ -117,7 +117,7 @@ mod test { let mut out_bytes = [0, .. 100]; let bytes_read = inflate_reader.read(out_bytes).unwrap(); assert_eq!(bytes_read, in_bytes.len()); - let out_msg = str::from_utf8_slice(out_bytes); + let out_msg = str::from_utf8(out_bytes); assert_eq!(in_msg, out_msg); } } diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index db8de9df24a..f0b51a2c3e0 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -821,7 +821,7 @@ mod test { } } unlink(filename); - let read_str = str::from_utf8_slice(read_mem); + let read_str = str::from_utf8(read_mem); assert_eq!(read_str, message); }) @@ -845,7 +845,7 @@ mod test { tell_pos_post_read = read_stream.tell(); } unlink(filename); - let read_str = str::from_utf8_slice(read_mem); + let read_str = str::from_utf8(read_mem); assert_eq!(read_str, message.slice(4, 8)); assert_eq!(tell_pos_pre_read, set_cursor); assert_eq!(tell_pos_post_read, message.len() as u64); @@ -870,7 +870,7 @@ mod test { read_stream.read(read_mem); } unlink(filename); - let read_str = str::from_utf8_slice(read_mem); + let read_str = str::from_utf8(read_mem); assert!(read_str == final_msg.to_owned()); }) @@ -892,15 +892,15 @@ mod test { read_stream.seek(-4, SeekEnd); read_stream.read(read_mem); - assert_eq!(str::from_utf8_slice(read_mem), chunk_three); + assert_eq!(str::from_utf8(read_mem), chunk_three); read_stream.seek(-9, SeekCur); read_stream.read(read_mem); - assert_eq!(str::from_utf8_slice(read_mem), chunk_two); + assert_eq!(str::from_utf8(read_mem), chunk_two); read_stream.seek(0, SeekSet); read_stream.read(read_mem); - assert_eq!(str::from_utf8_slice(read_mem), chunk_one); + assert_eq!(str::from_utf8(read_mem), chunk_one); } unlink(filename); }) @@ -974,7 +974,7 @@ mod test { { let n = f.filestem_str(); File::open(f).read(mem); - let read_str = str::from_utf8_slice(mem); + let read_str = str::from_utf8(mem); let expected = match n { None|Some("") => fail!("really shouldn't happen.."), Some(n) => prefix+n diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index c00233dda55..208c64f5ef4 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1049,7 +1049,7 @@ pub trait Buffer: Reader { Some(n) if n == width => {} Some(..) | None => return None // read error } - match str::from_utf8_slice_opt(buf.slice_to(width)) { + match str::from_utf8_opt(buf.slice_to(width)) { Some(s) => Some(s.char_at(0)), None => None } diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index 8ecfdb3a9e0..79989b838f6 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -176,7 +176,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// If the path is not representable in utf-8, this returns None. #[inline] fn as_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8_slice_opt(self.as_vec()) + str::from_utf8_opt(self.as_vec()) } /// Returns the path as a byte vector @@ -207,7 +207,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// See `dirname` for details. #[inline] fn dirname_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8_slice_opt(self.dirname()) + str::from_utf8_opt(self.dirname()) } /// Returns the file component of `self`, as a byte vector. /// If `self` represents the root of the file hierarchy, returns None. @@ -217,7 +217,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// See `filename` for details. #[inline] fn filename_str<'a>(&'a self) -> Option<&'a str> { - self.filename().and_then(str::from_utf8_slice_opt) + self.filename().and_then(str::from_utf8_opt) } /// Returns the stem of the filename of `self`, as a byte vector. /// The stem is the portion of the filename just before the last '.'. @@ -239,7 +239,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// See `filestem` for details. #[inline] fn filestem_str<'a>(&'a self) -> Option<&'a str> { - self.filestem().and_then(str::from_utf8_slice_opt) + self.filestem().and_then(str::from_utf8_opt) } /// Returns the extension of the filename of `self`, as an optional byte vector. /// The extension is the portion of the filename just after the last '.'. @@ -262,7 +262,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe { /// See `extension` for details. #[inline] fn extension_str<'a>(&'a self) -> Option<&'a str> { - self.extension().and_then(str::from_utf8_slice_opt) + self.extension().and_then(str::from_utf8_opt) } /// Replaces the filename portion of the path with the given byte vector or string. @@ -493,12 +493,12 @@ pub trait BytesContainer { /// Raises `str::null_byte` if not utf-8 #[inline] fn container_as_str<'a>(&'a self) -> &'a str { - str::from_utf8_slice(self.container_as_bytes()) + str::from_utf8(self.container_as_bytes()) } /// Returns the receiver interpreted as a utf-8 string, if possible #[inline] fn container_as_str_opt<'a>(&'a self) -> Option<&'a str> { - str::from_utf8_slice_opt(self.container_as_bytes()) + str::from_utf8_opt(self.container_as_bytes()) } /// Returns whether .container_as_str() is guaranteed to not fail // FIXME (#8888): Remove unused arg once :: works diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index b2bc00dd247..ddf2cce21b0 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -396,13 +396,13 @@ impl Path { /// Returns an iterator that yields each component of the path as Option<&str>. /// See components() for details. pub fn str_components<'a>(&'a self) -> StrComponentIter<'a> { - self.components().map(str::from_utf8_slice_opt) + self.components().map(str::from_utf8_opt) } /// Returns an iterator that yields each component of the path in reverse as Option<&str>. /// See components() for details. pub fn rev_str_components<'a>(&'a self) -> RevStrComponentIter<'a> { - self.rev_components().map(str::from_utf8_slice_opt) + self.rev_components().map(str::from_utf8_opt) } } @@ -684,7 +684,7 @@ mod tests { (s: $path:expr, $op:ident, $exp:expr, opt) => ( { let path = Path::init($path); - let left = path.$op().map(|x| str::from_utf8_slice(x)); + let left = path.$op().map(|x| str::from_utf8(x)); assert_eq!(left, $exp); } ); diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 2076d433fb6..c1898a9b920 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -159,16 +159,16 @@ pub fn from_utf8_owned_opt(vv: ~[u8]) -> Option<~str> { /// # Failure /// /// Fails if invalid UTF-8 -pub fn from_utf8_slice<'a>(v: &'a [u8]) -> &'a str { - from_utf8_slice_opt(v).expect("from_utf8_slice: not utf-8") +pub fn from_utf8<'a>(v: &'a [u8]) -> &'a str { + from_utf8_opt(v).expect("from_utf8: not utf-8") } /// Converts a vector to a string slice without performing any allocations. /// /// Returns None if the slice is not utf-8. -pub fn from_utf8_slice_opt<'a>(v: &'a [u8]) -> Option<&'a str> { +pub fn from_utf8_opt<'a>(v: &'a [u8]) -> Option<&'a str> { if is_utf8(v) { - Some(unsafe { raw::from_utf8_slice(v) }) + Some(unsafe { raw::from_utf8(v) }) } else { None } } @@ -1029,7 +1029,7 @@ pub mod raw { /// Converts a slice of bytes to a string slice without checking /// that the string contains valid UTF-8. - pub unsafe fn from_utf8_slice<'a>(v: &'a [u8]) -> &'a str { + pub unsafe fn from_utf8<'a>(v: &'a [u8]) -> &'a str { cast::transmute(v) } @@ -3153,7 +3153,7 @@ mod tests { let s1: ~str = ~"All mimsy were the borogoves"; let v: ~[u8] = s1.as_bytes().to_owned(); - let s2: ~str = from_utf8_slice(v).to_owned(); + let s2: ~str = from_utf8(v).to_owned(); let mut i: uint = 0u; let n1: uint = s1.len(); let n2: uint = v.len(); @@ -3676,31 +3676,31 @@ mod tests { } #[test] - fn test_str_from_utf8_slice() { + fn test_str_from_utf8() { let xs = bytes!("hello"); - assert_eq!(from_utf8_slice(xs), "hello"); + assert_eq!(from_utf8(xs), "hello"); let xs = bytes!("ศไทย中华Việt Nam"); - assert_eq!(from_utf8_slice(xs), "ศไทย中华Việt Nam"); + assert_eq!(from_utf8(xs), "ศไทย中华Việt Nam"); } #[test] #[should_fail] - fn test_str_from_utf8_slice_invalid() { + fn test_str_from_utf8_invalid() { let xs = bytes!("hello", 0xff); - let _ = from_utf8_slice(xs); + let _ = from_utf8(xs); } #[test] - fn test_str_from_utf8_slice_opt() { + fn test_str_from_utf8_opt() { let xs = bytes!("hello"); - assert_eq!(from_utf8_slice_opt(xs), Some("hello")); + assert_eq!(from_utf8_opt(xs), Some("hello")); let xs = bytes!("ศไทย中华Việt Nam"); - assert_eq!(from_utf8_slice_opt(xs), Some("ศไทย中华Việt Nam")); + assert_eq!(from_utf8_opt(xs), Some("ศไทย中华Việt Nam")); let xs = bytes!("hello", 0xff); - assert_eq!(from_utf8_slice_opt(xs), None); + assert_eq!(from_utf8_opt(xs), None); } #[test] diff --git a/src/test/run-pass/const-str-ptr.rs b/src/test/run-pass/const-str-ptr.rs index f4c6e4e689a..b0efa3bb33f 100644 --- a/src/test/run-pass/const-str-ptr.rs +++ b/src/test/run-pass/const-str-ptr.rs @@ -17,13 +17,13 @@ static C: *u8 = B as *u8; pub fn main() { unsafe { let foo = &A as *u8; - assert_eq!(str::raw::from_utf8_slice(A), "hi"); + assert_eq!(str::raw::from_utf8(A), "hi"); assert_eq!(str::raw::from_buf_len(foo, A.len()), ~"hi"); assert_eq!(str::raw::from_buf_len(C, B.len()), ~"hi"); assert!(*C == A[0]); assert!(*(&B[0] as *u8) == A[0]); - let bar = str::raw::from_utf8_slice(A).to_c_str(); + let bar = str::raw::from_utf8(A).to_c_str(); assert_eq!(bar.with_ref(|buf| str::raw::from_c_str(buf)), ~"hi"); } } diff --git a/src/test/run-pass/rtio-processes.rs b/src/test/run-pass/rtio-processes.rs index b9287494bae..6463a1d5321 100644 --- a/src/test/run-pass/rtio-processes.rs +++ b/src/test/run-pass/rtio-processes.rs @@ -108,7 +108,7 @@ fn read_all(input: &mut Reader) -> ~str { loop { match input.read(buf) { None => { break } - Some(n) => { ret.push_str(str::from_utf8_slice(buf.slice_to(n))); } + Some(n) => { ret.push_str(str::from_utf8(buf.slice_to(n))); } } } return ret; -- cgit 1.4.1-3-g733a5