Skip to content

Add _opt variants to str byte-conversion functions #8750

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

Closed
wants to merge 1 commit into from
Closed
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
117 changes: 108 additions & 9 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,23 @@ Section: Creating a string
pub fn from_bytes(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(fmt!("from_bytes: input is not UTF-8; first bad byte is %u",
first_bad_byte as uint))
match from_bytes_opt(vv) {
None => {
let first_bad_byte = *vv.iter().find(|&b| !is_utf8([*b])).unwrap();
cond.raise(fmt!("from_bytes: input is not UTF-8; first bad byte is %u",
first_bad_byte as uint))
}
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_bytes_opt(vv: &[u8]) -> Option<~str> {
if is_utf8(vv) {
Some(unsafe { raw::from_bytes(vv) })
} else {
return unsafe { raw::from_bytes(vv) }
None
}
}

Expand All @@ -78,7 +89,17 @@ pub fn from_bytes_owned(vv: ~[u8]) -> ~str {
cond.raise(fmt!("from_bytes: input is not UTF-8; first bad byte is %u",
first_bad_byte as uint))
} else {
return unsafe { raw::from_bytes_owned(vv) }
unsafe { raw::from_bytes_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_bytes_owned_opt(vv: ~[u8]) -> Option<~str> {
if is_utf8(vv) {
Some(unsafe { raw::from_bytes_owned(vv) })
} else {
None
}
}

Expand All @@ -91,8 +112,16 @@ pub fn from_bytes_owned(vv: ~[u8]) -> ~str {
///
/// Fails if invalid UTF-8
pub fn from_bytes_slice<'a>(v: &'a [u8]) -> &'a str {
assert!(is_utf8(v));
unsafe { cast::transmute(v) }
from_bytes_slice_opt(v).expect("from_bytes_slice: 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_bytes_slice_opt<'a>(v: &'a [u8]) -> Option<&'a str> {
if is_utf8(v) {
Some(unsafe { cast::transmute(v) })
} else { None }
}

impl ToStr for ~str {
Expand Down Expand Up @@ -2358,7 +2387,7 @@ impl Zero for @str {
#[cfg(test)]
mod tests {
use container::Container;
use option::Some;
use option::{None, Some};
use libc::c_char;
use libc;
use ptr;
Expand Down Expand Up @@ -3539,6 +3568,76 @@ mod tests {
let mut s = ~"\u00FC"; // ü
s.truncate(1);
}

#[test]
fn test_str_from_bytes_slice() {
let xs = bytes!("hello");
assert_eq!(from_bytes_slice(xs), "hello");

let xs = bytes!("ศไทย中华Việt Nam");
assert_eq!(from_bytes_slice(xs), "ศไทย中华Việt Nam");
}

#[test]
#[should_fail]
fn test_str_from_bytes_slice_invalid() {
let xs = bytes!("hello", 0xff);
let _ = from_bytes_slice(xs);
}

#[test]
fn test_str_from_bytes_slice_opt() {
let xs = bytes!("hello");
assert_eq!(from_bytes_slice_opt(xs), Some("hello"));

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

let xs = bytes!("hello", 0xff);
assert_eq!(from_bytes_slice_opt(xs), None);
}

#[test]
fn test_str_from_bytes() {
let xs = bytes!("hello");
assert_eq!(from_bytes(xs), ~"hello");

let xs = bytes!("ศไทย中华Việt Nam");
assert_eq!(from_bytes(xs), ~"ศไทย中华Việt Nam");
}

#[test]
fn test_str_from_bytes_opt() {
let xs = bytes!("hello").to_owned();
assert_eq!(from_bytes_opt(xs), Some(~"hello"));

let xs = bytes!("ศไทย中华Việt Nam");
assert_eq!(from_bytes_opt(xs), Some(~"ศไทย中华Việt Nam"));

let xs = bytes!("hello", 0xff);
assert_eq!(from_bytes_opt(xs), None);
}

#[test]
fn test_str_from_bytes_owned() {
let xs = bytes!("hello").to_owned();
assert_eq!(from_bytes_owned(xs), ~"hello");

let xs = bytes!("ศไทย中华Việt Nam").to_owned();
assert_eq!(from_bytes_owned(xs), ~"ศไทย中华Việt Nam");
}

#[test]
fn test_str_from_bytes_owned_opt() {
let xs = bytes!("hello").to_owned();
assert_eq!(from_bytes_owned_opt(xs), Some(~"hello"));

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

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

#[cfg(test)]
Expand Down