diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2013-12-23 17:45:01 +0100 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2014-01-21 15:48:48 -0800 |
| commit | 05ae134acebee3f35af4880de113a7ae7ce20002 (patch) | |
| tree | 096daf1c7c42bd04ac3d1f11f710fd9786d9937a /src/libstd | |
| parent | b8c41492939c77b7139e46ee67375b47041f6692 (diff) | |
| download | rust-05ae134acebee3f35af4880de113a7ae7ce20002.tar.gz rust-05ae134acebee3f35af4880de113a7ae7ce20002.zip | |
[std::str] Rename from_utf8_owned_opt() to from_utf8_owned(), drop the old from_utf8_owned() behavior
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/fmt/mod.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/fs.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 4 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 2 | ||||
| -rw-r--r-- | src/libstd/repr.rs | 4 | ||||
| -rw-r--r-- | src/libstd/run.rs | 18 | ||||
| -rw-r--r-- | src/libstd/str.rs | 34 |
7 files changed, 20 insertions, 46 deletions
diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index 4bee1f42b86..411f9f25459 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -691,7 +691,7 @@ pub fn format(args: &Arguments) -> ~str { pub unsafe fn format_unsafe(fmt: &[rt::Piece], args: &[Argument]) -> ~str { let mut output = MemWriter::new(); write_unsafe(&mut output as &mut io::Writer, fmt, args); - return str::from_utf8_owned(output.unwrap()); + return str::from_utf8_owned(output.unwrap()).unwrap(); } impl<'a> Formatter<'a> { diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index 0564311ad22..cb98ff21105 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -754,7 +754,7 @@ 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_owned(read_buf.slice_to(n).to_owned()) + n => str::from_utf8_owned(read_buf.slice_to(n).to_owned()).unwrap() }; assert_eq!(read_str, message.to_owned()); } diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index b12adbf230f..30827983360 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -607,7 +607,7 @@ pub trait Reader { /// This function will raise all the same conditions as the `read` method, /// along with raising a condition if the input is not valid UTF-8. fn read_to_str(&mut self) -> ~str { - match str::from_utf8_owned_opt(self.read_to_end()) { + match str::from_utf8_owned(self.read_to_end()) { Some(s) => s, None => { io_error::cond.raise(standard_error(InvalidInput)); @@ -1117,7 +1117,7 @@ pub trait Buffer: Reader { /// The task will also fail if sequence of bytes leading up to /// the newline character are not valid UTF-8. fn read_line(&mut self) -> Option<~str> { - self.read_until('\n' as u8).map(str::from_utf8_owned) + self.read_until('\n' as u8).map(|line| str::from_utf8_owned(line).unwrap()) } /// Create an iterator that reads a line on each iteration until EOF. diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 5c35c500e6c..bf9e6b739f2 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -427,7 +427,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_owned(bytes), special) + (str::from_utf8_owned(bytes).unwrap(), special) } // Some constants for from_str_bytes_common's input validation, diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index 8f7d01f263d..8919f9f8903 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -608,7 +608,7 @@ pub fn repr_to_str<T>(t: &T) -> ~str { let mut result = io::MemWriter::new(); write_repr(&mut result as &mut io::Writer, t); - str::from_utf8_owned(result.unwrap()) + str::from_utf8_owned(result.unwrap()).unwrap() } #[cfg(test)] @@ -626,7 +626,7 @@ fn test_repr() { fn exact_test<T>(t: &T, e:&str) { let mut m = io::MemWriter::new(); write_repr(&mut m as &mut io::Writer, t); - let s = str::from_utf8_owned(m.unwrap()); + let s = str::from_utf8_owned(m.unwrap()).unwrap(); assert_eq!(s.as_slice(), e); } diff --git a/src/libstd/run.rs b/src/libstd/run.rs index 3595a7d45ac..f460d3f4944 100644 --- a/src/libstd/run.rs +++ b/src/libstd/run.rs @@ -372,7 +372,7 @@ mod tests { let run::ProcessOutput {status, output, error} = run::process_output("echo", [~"hello"]).expect("failed to exec `echo`"); - let output_str = str::from_utf8_owned(output); + let output_str = str::from_utf8_owned(output).unwrap(); assert!(status.success()); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -439,7 +439,7 @@ mod tests { None => break } } - str::from_utf8_owned(res) + str::from_utf8_owned(res).unwrap() } #[test] @@ -467,7 +467,7 @@ mod tests { .expect("failed to exec `echo`"); let run::ProcessOutput {status, output, error} = prog.finish_with_output(); - let output_str = str::from_utf8_owned(output); + let output_str = str::from_utf8_owned(output).unwrap(); assert!(status.success()); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -486,7 +486,7 @@ mod tests { let run::ProcessOutput {status, output, error} = prog.finish_with_output(); - let output_str = str::from_utf8_owned(output); + let output_str = str::from_utf8_owned(output).unwrap(); assert!(status.success()); assert_eq!(output_str.trim().to_owned(), ~"hello"); @@ -533,7 +533,7 @@ mod tests { fn test_keep_current_working_dir() { let mut prog = run_pwd(None); - let output = str::from_utf8_owned(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output).unwrap(); let parent_dir = os::getcwd(); let child_dir = Path::new(output.trim()); @@ -551,7 +551,7 @@ mod tests { let parent_dir = os::getcwd().dir_path(); let mut prog = run_pwd(Some(&parent_dir)); - let output = str::from_utf8_owned(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output).unwrap(); let child_dir = Path::new(output.trim()); let parent_stat = parent_dir.stat(); @@ -590,7 +590,7 @@ mod tests { if running_on_valgrind() { return; } let mut prog = run_env(None); - let output = str::from_utf8_owned(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output).unwrap(); let r = os::env(); for &(ref k, ref v) in r.iter() { @@ -604,7 +604,7 @@ mod tests { if running_on_valgrind() { return; } let mut prog = run_env(None); - let output = str::from_utf8_owned(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output).unwrap(); let r = os::env(); for &(ref k, ref v) in r.iter() { @@ -623,7 +623,7 @@ mod tests { new_env.push((~"RUN_TEST_NEW_ENV", ~"123")); let mut prog = run_env(Some(new_env)); - let output = str::from_utf8_owned(prog.finish_with_output().output); + let output = str::from_utf8_owned(prog.finish_with_output().output).unwrap(); assert!(output.contains("RUN_TEST_NEW_ENV=123")); } diff --git a/src/libstd/str.rs b/src/libstd/str.rs index c266ddfea4b..6d52e94064c 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -129,26 +129,9 @@ condition! { Section: Creating a string */ -/// Consumes a vector of bytes to create a new utf-8 string -/// -/// # Failure -/// -/// Raises the `not_utf8` condition if invalid UTF-8 -pub fn from_utf8_owned(vv: ~[u8]) -> ~str { - use str::not_utf8::cond; - - if !is_utf8(vv) { - 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)) - } else { - unsafe { raw::from_utf8_owned(vv) } - } -} - /// Consumes a vector of bytes to create a new utf-8 string. /// Returns None if the vector contains invalid UTF-8. -pub fn from_utf8_owned_opt(vv: ~[u8]) -> Option<~str> { +pub fn from_utf8_owned(vv: ~[u8]) -> Option<~str> { if is_utf8(vv) { Some(unsafe { raw::from_utf8_owned(vv) }) } else { @@ -3964,22 +3947,13 @@ mod tests { #[test] fn test_str_from_utf8_owned() { let xs = bytes!("hello").to_owned(); - assert_eq!(from_utf8_owned(xs), ~"hello"); - - let xs = bytes!("ศไทย中华Việt Nam").to_owned(); - assert_eq!(from_utf8_owned(xs), ~"ศไทย中华Việt Nam"); - } - - #[test] - fn test_str_from_utf8_owned_opt() { - let xs = bytes!("hello").to_owned(); - assert_eq!(from_utf8_owned_opt(xs), Some(~"hello")); + assert_eq!(from_utf8_owned(xs), Some(~"hello")); let xs = bytes!("ศไทย中华Việt Nam").to_owned(); - assert_eq!(from_utf8_owned_opt(xs), Some(~"ศไทย中华Việt Nam")); + assert_eq!(from_utf8_owned(xs), Some(~"ศไทย中华Việt Nam")); let xs = bytes!("hello", 0xff).to_owned(); - assert_eq!(from_utf8_owned_opt(xs), None); + assert_eq!(from_utf8_owned(xs), None); } #[test] |
