diff options
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/c_str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/fmt/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/flate.rs | 4 | ||||
| -rw-r--r-- | src/libstd/io/fs.rs | 31 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 2 | ||||
| -rw-r--r-- | src/libstd/path/mod.rs | 14 | ||||
| -rw-r--r-- | src/libstd/path/posix.rs | 6 | ||||
| -rw-r--r-- | src/libstd/run.rs | 16 | ||||
| -rw-r--r-- | src/libstd/str.rs | 136 |
10 files changed, 56 insertions, 159 deletions
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 8a5aa171eb8..4a31449e105 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(); @@ -118,6 +118,6 @@ mod test { 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); + assert_eq!(in_msg, out_msg); } } diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index e239f630f01..f0b51a2c3e0 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"); @@ -822,7 +822,7 @@ mod test { } unlink(filename); let read_str = str::from_utf8(read_mem); - assert!(read_str == message.to_owned()); + assert_eq!(read_str, message); }) test!(fn file_test_io_seek_and_tell_smoke_test() { @@ -846,9 +846,9 @@ mod test { } 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); + 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() { @@ -877,9 +877,9 @@ mod test { 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(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(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(read_mem), chunk_one); } unlink(filename); }) @@ -982,7 +979,7 @@ mod test { 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/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/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/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 ::<for T> 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/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..c1898a9b920 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 @@ -187,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 { cast::transmute(v) }) + Some(unsafe { raw::from_utf8(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<'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(v).to_owned(); let mut i: uint = 0u; let n1: uint = s1.len(); let n2: uint = v.len(); @@ -3755,49 +3676,28 @@ 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); - } - - #[test] - fn test_str_from_utf8_slice_opt() { - let xs = bytes!("hello"); - assert_eq!(from_utf8_slice_opt(xs), Some("hello")); - - let xs = bytes!("ศไทย中华Việt Nam"); - assert_eq!(from_utf8_slice_opt(xs), Some("ศไทย中华Việt Nam")); - - let xs = bytes!("hello", 0xff); - 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"); + let _ = from_utf8(xs); } #[test] fn test_str_from_utf8_opt() { - let xs = bytes!("hello").to_owned(); - assert_eq!(from_utf8_opt(xs), Some(~"hello")); + let xs = bytes!("hello"); + assert_eq!(from_utf8_opt(xs), Some("hello")); let xs = bytes!("ศไทย中华Việt Nam"); - assert_eq!(from_utf8_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_opt(xs), None); |
