diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-11-28 23:52:11 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-12-04 22:35:53 +1100 |
| commit | 9d64e46013096997627da62ecc65225bc22682e8 (patch) | |
| tree | fa20c8fd1d653acff5f996253d16103dd61addd9 /src/libstd | |
| parent | 9635c763ba5edc8c8ea2868b895548b52f640e5a (diff) | |
| download | rust-9d64e46013096997627da62ecc65225bc22682e8.tar.gz rust-9d64e46013096997627da62ecc65225bc22682e8.zip | |
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.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/flate.rs | 6 | ||||
| -rw-r--r-- | src/libstd/io/fs.rs | 39 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 2 | ||||
| -rw-r--r-- | src/libstd/run.rs | 16 | ||||
| -rw-r--r-- | src/libstd/str.rs | 114 |
5 files changed, 37 insertions, 140 deletions
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<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Float+Round+ sign: SignFormat, digits: SignificantDigits) -> (~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 @@ -3078,33 +3051,6 @@ mod tests { } #[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 assert!(!is_utf8([0xc0, 0x80])); @@ -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(); @@ -3783,27 +3704,6 @@ mod tests { } #[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(); assert_eq!(from_utf8_owned(xs), ~"hello"); |
