Skip to content

Commit 84406d4

Browse files
committed
auto merge of #14213 : kballard/rust/str_from_utf8_result, r=cmr
Change `str::from_utf8_owned()` and `StrBuf::from_utf8()` to return `Result`. This allows the vector to be recovered when it contains invalid UTF-8.
2 parents 632d486 + ba7844a commit 84406d4

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

src/doc/complement-cheatsheet.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ To return an Owned String (~str) use the str helper function [`from_utf8_owned`]
6060
~~~
6161
use std::str;
6262
63-
let x: Option<~str> = str::from_utf8_owned(~[104u8,105u8]);
63+
let x: Result<~str,~[u8]> = str::from_utf8_owned(~[104u8,105u8]);
6464
let y: ~str = x.unwrap();
6565
~~~
6666

src/libserialize/base64.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ impl<'a> FromBase64 for &'a str {
181181
* Convert any base64 encoded string (literal, `@`, `&`, or `~`)
182182
* to the byte values it encodes.
183183
*
184-
* You can use the `from_utf8_owned` function in `std::str`
185-
* to turn a `[u8]` into a string with characters corresponding to those
186-
* values.
184+
* You can use the `StrBuf::from_utf8` function in `std::strbuf` to turn a
185+
* `Vec<u8>` into a string with characters corresponding to those values.
187186
*
188187
* # Example
189188
*
@@ -199,7 +198,7 @@ impl<'a> FromBase64 for &'a str {
199198
* let res = hello_str.from_base64();
200199
* if res.is_ok() {
201200
* let opt_bytes = StrBuf::from_utf8(res.unwrap());
202-
* if opt_bytes.is_some() {
201+
* if opt_bytes.is_ok() {
203202
* println!("decoded from base64: {}", opt_bytes.unwrap());
204203
* }
205204
* }

src/libserialize/hex.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ impl<'a> FromHex for &'a str {
8080
* Convert any hexadecimal encoded string (literal, `@`, `&`, or `~`)
8181
* to the byte values it encodes.
8282
*
83-
* You can use the `from_utf8_owned` function in `std::str`
84-
* to turn a `[u8]` into a string with characters corresponding to those
85-
* values.
83+
* You can use the `StrBuf::from_utf8` function in `std::strbuf` to turn a
84+
* `Vec<u8>` into a string with characters corresponding to those values.
8685
*
8786
* # Example
8887
*

src/libstd/num/strconv.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use num::{Float, FPNaN, FPInfinite, ToPrimitive};
1919
use num;
2020
use ops::{Add, Sub, Mul, Div, Rem, Neg};
2121
use option::{None, Option, Some};
22+
use result::ResultUnwrap;
2223
use slice::{CloneableVector, ImmutableVector, MutableVector};
2324
use std::cmp::{Ord, Eq};
2425
use str::{StrAllocating, StrSlice};

src/libstd/str.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ use iter::{Iterator, range, AdditiveIterator};
8787
use mem::transmute;
8888
use mem;
8989
use option::{None, Option, Some};
90+
use result::{Result, Ok, Err};
9091
use slice::Vector;
9192
use slice::{ImmutableVector, MutableVector, CloneableVector};
9293
use strbuf::StrBuf;
@@ -105,12 +106,14 @@ Section: Creating a string
105106
*/
106107

107108
/// Consumes a vector of bytes to create a new utf-8 string.
108-
/// Returns None if the vector contains invalid UTF-8.
109-
pub fn from_utf8_owned(vv: ~[u8]) -> Option<~str> {
109+
///
110+
/// Returns `Err` with the original vector if the vector contains invalid
111+
/// UTF-8.
112+
pub fn from_utf8_owned(vv: ~[u8]) -> Result<~str, ~[u8]> {
110113
if is_utf8(vv) {
111-
Some(unsafe { raw::from_utf8_owned(vv) })
114+
Ok(unsafe { raw::from_utf8_owned(vv) })
112115
} else {
113-
None
116+
Err(vv)
114117
}
115118
}
116119

@@ -2120,13 +2123,13 @@ mod tests {
21202123
#[test]
21212124
fn test_str_from_utf8_owned() {
21222125
let xs = bytes!("hello").to_owned();
2123-
assert_eq!(from_utf8_owned(xs), Some("hello".to_owned()));
2126+
assert_eq!(from_utf8_owned(xs), Ok("hello".to_owned()));
21242127

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

21282131
let xs = bytes!("hello", 0xff).to_owned();
2129-
assert_eq!(from_utf8_owned(xs), None);
2132+
assert_eq!(from_utf8_owned(xs), Err(bytes!("hello", 0xff).to_owned()));
21302133
}
21312134

21322135
#[test]

src/libstd/strbuf.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use mem;
2020
use option::{None, Option, Some};
2121
use ptr::RawPtr;
2222
use ptr;
23+
use result::{Result, Ok, Err};
2324
use slice::{OwnedVector, Vector, CloneableVector};
2425
use str::{CharRange, OwnedStr, Str, StrSlice, StrAllocating};
2526
use str;
@@ -72,14 +73,17 @@ impl StrBuf {
7273
}
7374
}
7475

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

0 commit comments

Comments
 (0)