Deprecate str::from_utf16

Use `String::from_utf16` instead

[breaking-change]
This commit is contained in:
Adolfo Ochagavía
2014-07-10 17:43:03 +02:00
parent 173baac495
commit 6ac4fc7fc2
4 changed files with 40 additions and 36 deletions
+9 -31
View File
@@ -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
@@ -1722,7 +1700,7 @@ fn test_utf16() {
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);
@@ -1730,7 +1708,7 @@ fn test_utf16() {
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);
}
}
@@ -1739,15 +1717,15 @@ fn test_utf16() {
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]
+26
View File
@@ -91,6 +91,32 @@ pub fn from_utf8(vec: Vec<u8>) -> Result<String, Vec<u8>> {
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
///
+1 -1
View File
@@ -378,7 +378,7 @@ fn prune(root: &CString, dirs: Vec<Path>) -> 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));
}
+4 -4
View File
@@ -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"))
}
@@ -180,7 +180,7 @@ pub fn fill_utf16_buf_and_decode(f: |*mut u16, DWORD| -> DWORD)
// 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)
}
@@ -1050,7 +1050,7 @@ fn FormatMessageW(flags: DWORD,
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),
@@ -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")
});