mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 00:34:06 +00:00
Change StrBuf::from_utf8() to return Result
This allows the original vector to be recovered in the event that it is not UTF-8. [breaking-change]
This commit is contained in:
parent
d0f3cb05df
commit
ba7844a7ff
@ -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
|
||||
*
|
||||
@ -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());
|
||||
* }
|
||||
* }
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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};
|
||||
|
@ -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;
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user