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

View file

@ -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
///