Skip to content

Commit 19aa101

Browse files
committedOct 31, 2018
Speed up String::from_utf16
1 parent 0db7abe commit 19aa101

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed
 

‎src/liballoc/string.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,15 @@ impl String {
618618
/// ```
619619
#[stable(feature = "rust1", since = "1.0.0")]
620620
pub fn from_utf16(v: &[u16]) -> Result<String, FromUtf16Error> {
621-
decode_utf16(v.iter().cloned()).collect::<Result<_, _>>().map_err(|_| FromUtf16Error(()))
621+
let mut ret = String::with_capacity(v.len());
622+
for c in decode_utf16(v.iter().cloned()) {
623+
if let Ok(c) = c {
624+
ret.push(c);
625+
} else {
626+
return Err(FromUtf16Error(()));
627+
}
628+
}
629+
Ok(ret)
622630
}
623631

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

0 commit comments

Comments
 (0)
Please sign in to comment.