Skip to content

Change from_utf8_owned() to return Result #14213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/doc/complement-cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To return an Owned String (~str) use the str helper function [`from_utf8_owned`]
~~~
use std::str;

let x: Option<~str> = str::from_utf8_owned(~[104u8,105u8]);
let x: Result<~str,~[u8]> = str::from_utf8_owned(~[104u8,105u8]);
let y: ~str = x.unwrap();
~~~

Expand Down
7 changes: 3 additions & 4 deletions src/libserialize/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ impl<'a> FromBase64 for &'a str {
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
* to the byte values it encodes.
*
* You can use the `from_utf8_owned` function in `std::str`
* to turn a `[u8]` into a string with characters corresponding to those
* values.
* You can use the `StrBuf::from_utf8` function in `std::strbuf` to turn a
* `Vec<u8>` into a string with characters corresponding to those values.
*
* # Example
*
Expand All @@ -199,7 +198,7 @@ impl<'a> FromBase64 for &'a str {
* let res = hello_str.from_base64();
* if res.is_ok() {
* let opt_bytes = StrBuf::from_utf8(res.unwrap());
* if opt_bytes.is_some() {
* if opt_bytes.is_ok() {
* println!("decoded from base64: {}", opt_bytes.unwrap());
* }
* }
Expand Down
5 changes: 2 additions & 3 deletions src/libserialize/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ impl<'a> FromHex for &'a str {
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
* to the byte values it encodes.
*
* You can use the `from_utf8_owned` function in `std::str`
* to turn a `[u8]` into a string with characters corresponding to those
* values.
* You can use the `StrBuf::from_utf8` function in `std::strbuf` to turn a
* `Vec<u8>` into a string with characters corresponding to those values.
*
* # Example
*
Expand Down
1 change: 1 addition & 0 deletions src/libstd/num/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use num::{Float, FPNaN, FPInfinite, ToPrimitive};
use num;
use ops::{Add, Sub, Mul, Div, Rem, Neg};
use option::{None, Option, Some};
use result::ResultUnwrap;
use slice::{CloneableVector, ImmutableVector, MutableVector};
use std::cmp::{Ord, Eq};
use str::{StrAllocating, StrSlice};
Expand Down
17 changes: 10 additions & 7 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ use iter::{Iterator, range, AdditiveIterator};
use mem::transmute;
use mem;
use option::{None, Option, Some};
use result::{Result, Ok, Err};
use slice::Vector;
use slice::{ImmutableVector, MutableVector, CloneableVector};
use strbuf::StrBuf;
Expand All @@ -105,12 +106,14 @@ Section: Creating a string
*/

/// 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(vv: ~[u8]) -> Option<~str> {
///
/// Returns `Err` with the original vector if the vector contains invalid
/// UTF-8.
pub fn from_utf8_owned(vv: ~[u8]) -> Result<~str, ~[u8]> {
if is_utf8(vv) {
Some(unsafe { raw::from_utf8_owned(vv) })
Ok(unsafe { raw::from_utf8_owned(vv) })
} else {
None
Err(vv)
}
}

Expand Down Expand Up @@ -2115,13 +2118,13 @@ mod tests {
#[test]
fn test_str_from_utf8_owned() {
let xs = bytes!("hello").to_owned();
assert_eq!(from_utf8_owned(xs), Some("hello".to_owned()));
assert_eq!(from_utf8_owned(xs), Ok("hello".to_owned()));

let xs = bytes!("ศไทย中华Việt Nam").to_owned();
assert_eq!(from_utf8_owned(xs), Some("ศไทย中华Việt Nam".to_owned()));
assert_eq!(from_utf8_owned(xs), Ok("ศไทย中华Việt Nam".to_owned()));

let xs = bytes!("hello", 0xff).to_owned();
assert_eq!(from_utf8_owned(xs), None);
assert_eq!(from_utf8_owned(xs), Err(bytes!("hello", 0xff).to_owned()));
}

#[test]
Expand Down
14 changes: 9 additions & 5 deletions src/libstd/strbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use mem;
use option::{None, Option, Some};
use ptr::RawPtr;
use ptr;
use result::{Result, Ok, Err};
use slice::{OwnedVector, Vector, CloneableVector};
use str::{CharRange, OwnedStr, Str, StrSlice, StrAllocating};
use str;
Expand Down Expand Up @@ -72,14 +73,17 @@ impl StrBuf {
}
}

/// Tries to create a new string buffer from the given byte
/// vector, validating that the vector is UTF-8 encoded.
/// Returns the vector as a string buffer, if possible, taking care not to
/// copy it.
///
/// Returns `Err` with the original vector if the vector contains invalid
/// UTF-8.
#[inline]
pub fn from_utf8(vec: Vec<u8>) -> Option<StrBuf> {
pub fn from_utf8(vec: Vec<u8>) -> Result<StrBuf, Vec<u8>> {
if str::is_utf8(vec.as_slice()) {
Some(StrBuf { vec: vec })
Ok(StrBuf { vec: vec })
} else {
None
Err(vec)
}
}

Expand Down