Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Deprecate str::from_utf16
Use `String::from_utf16` instead

[breaking-change]
  • Loading branch information
aochagavia committed Jul 15, 2014
1 parent 173baac commit 6ac4fc7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
40 changes: 9 additions & 31 deletions src/libcollections/str.rs
Expand Up @@ -378,32 +378,10 @@ pub fn replace(s: &str, from: &str, to: &str) -> String {
Section: Misc
*/

/// Decode a UTF-16 encoded vector `v` into a string, returning `None`
/// if `v` contains any invalid data.
///
/// # Example
///
/// ```rust
/// use std::str;
///
/// // 𝄞music
/// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075,
/// 0x0073, 0x0069, 0x0063];
/// assert_eq!(str::from_utf16(v), Some("𝄞music".to_string()));
///
/// // 𝄞mu<invalid>ic
/// v[4] = 0xD800;
/// assert_eq!(str::from_utf16(v), None);
/// ```
/// Deprecated. Use `String::from_utf16`.
#[deprecated = "Replaced by String::from_utf16"]
pub fn from_utf16(v: &[u16]) -> Option<String> {
let mut s = String::with_capacity(v.len() / 2);
for c in utf16_items(v) {
match c {
ScalarValue(c) => s.push_char(c),
LoneSurrogate(_) => return None
}
}
Some(s)
String::from_utf16(v)
}

/// Decode a UTF-16 encoded vector `v` into a string, replacing
Expand Down Expand Up @@ -1722,15 +1700,15 @@ mod tests {
for p in pairs.iter() {
let (s, u) = (*p).clone();
let s_as_utf16 = s.as_slice().utf16_units().collect::<Vec<u16>>();
let u_as_string = from_utf16(u.as_slice()).unwrap();
let u_as_string = String::from_utf16(u.as_slice()).unwrap();

assert!(is_utf16(u.as_slice()));
assert_eq!(s_as_utf16, u);

assert_eq!(u_as_string, s);
assert_eq!(from_utf16_lossy(u.as_slice()), s);

assert_eq!(from_utf16(s_as_utf16.as_slice()).unwrap(), s);
assert_eq!(String::from_utf16(s_as_utf16.as_slice()).unwrap(), s);
assert_eq!(u_as_string.as_slice().utf16_units().collect::<Vec<u16>>(), u);
}
}
Expand All @@ -1739,15 +1717,15 @@ mod tests {
fn test_utf16_invalid() {
// completely positive cases tested above.
// lead + eof
assert_eq!(from_utf16([0xD800]), None);
assert_eq!(String::from_utf16([0xD800]), None);
// lead + lead
assert_eq!(from_utf16([0xD800, 0xD800]), None);
assert_eq!(String::from_utf16([0xD800, 0xD800]), None);

// isolated trail
assert_eq!(from_utf16([0x0061, 0xDC00]), None);
assert_eq!(String::from_utf16([0x0061, 0xDC00]), None);

// general
assert_eq!(from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None);
assert_eq!(String::from_utf16([0xD800, 0xd801, 0xdc8b, 0xD800]), None);
}

#[test]
Expand Down
26 changes: 26 additions & 0 deletions src/libcollections/string.rs
Expand Up @@ -91,6 +91,32 @@ impl String {
Err(vec)
}
}

/// Decode a UTF-16 encoded vector `v` into a string, returning `None`
/// if `v` contains any invalid data.
///
/// # Example
///
/// ```rust
/// // 𝄞music
/// let mut v = [0xD834, 0xDD1E, 0x006d, 0x0075,
/// 0x0073, 0x0069, 0x0063];
/// assert_eq!(String::from_utf16(v), Some("𝄞music".to_string()));
///
/// // 𝄞mu<invalid>ic
/// v[4] = 0xD800;
/// assert_eq!(String::from_utf16(v), None);
/// ```
pub fn from_utf16(v: &[u16]) -> Option<String> {
let mut s = String::with_capacity(v.len() / 2);
for c in str::utf16_items(v) {
match c {
str::ScalarValue(c) => s.push_char(c),
str::LoneSurrogate(_) => return None
}
}
Some(s)
}

/// Convert a vector of chars to a string
///
Expand Down
2 changes: 1 addition & 1 deletion src/libnative/io/file_win32.rs
Expand Up @@ -378,7 +378,7 @@ pub fn readdir(p: &CString) -> IoResult<Vec<CString>> {
} else {
let fp_vec = vec::raw::from_buf(fp_buf, libc::wcslen(fp_buf) as uint);
let fp_trimmed = str::truncate_utf16_at_nul(fp_vec.as_slice());
let fp_str = str::from_utf16(fp_trimmed)
let fp_str = String::from_utf16(fp_trimmed)
.expect("rust_list_dir_wfd_fp_buf returned invalid UTF-16");
paths.push(Path::new(fp_str));
}
Expand Down
8 changes: 4 additions & 4 deletions src/libstd/os.rs
Expand Up @@ -137,7 +137,7 @@ pub fn getcwd() -> Path {
fail!();
}
}
Path::new(str::from_utf16(str::truncate_utf16_at_nul(buf))
Path::new(String::from_utf16(str::truncate_utf16_at_nul(buf))
.expect("GetCurrentDirectoryW returned invalid UTF-16"))
}

Expand Down Expand Up @@ -180,7 +180,7 @@ pub mod win32 {
// We want to explicitly catch the case when the
// closure returned invalid UTF-16, rather than
// set `res` to None and continue.
let s = str::from_utf16(sub)
let s = String::from_utf16(sub)
.expect("fill_utf16_buf_and_decode: closure created invalid UTF-16");
res = option::Some(s)
}
Expand Down Expand Up @@ -1050,7 +1050,7 @@ pub fn error_string(errnum: uint) -> String {
return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err);
}

let msg = str::from_utf16(str::truncate_utf16_at_nul(buf));
let msg = String::from_utf16(str::truncate_utf16_at_nul(buf));
match msg {
Some(msg) => format!("OS Error {}: {}", errnum, msg),
None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum),
Expand Down Expand Up @@ -1207,7 +1207,7 @@ fn real_args() -> Vec<String> {

// Push it onto the list.
let opt_s = slice::raw::buf_as_slice(ptr as *const _, len, |buf| {
str::from_utf16(str::truncate_utf16_at_nul(buf))
String::from_utf16(str::truncate_utf16_at_nul(buf))
});
opt_s.expect("CommandLineToArgvW returned invalid UTF-16")
});
Expand Down

0 comments on commit 6ac4fc7

Please sign in to comment.