Skip to content

Commit

Permalink
Speed up String::from_utf16
Browse files Browse the repository at this point in the history
  • Loading branch information
ljedrz committed Oct 31, 2018
1 parent 0db7abe commit 19aa101
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/liballoc/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,15 @@ impl String {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
decode_utf16(v.iter().cloned()).collect::<Result<_, _>>().map_err(|_| FromUtf16Error(()))
let mut ret = String::with_capacity(v.len());
for c in decode_utf16(v.iter().cloned()) {
if let Ok(c) = c {
ret.push(c);
} else {
return Err(FromUtf16Error(()));
}
}
Ok(ret)
}

/// Decode a UTF-16 encoded slice `v` into a `String`, replacing
Expand Down

0 comments on commit 19aa101

Please sign in to comment.