From 19aa10132cc727c8561730ab096b21d14507c81d Mon Sep 17 00:00:00 2001 From: ljedrz Date: Wed, 31 Oct 2018 10:22:22 +0100 Subject: [PATCH] Speed up String::from_utf16 --- src/liballoc/string.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index ff3587d5d873..8690f9017c40 100644 --- a/src/liballoc/string.rs +++ b/src/liballoc/string.rs @@ -618,7 +618,15 @@ impl String { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf16(v: &[u16]) -> Result { - decode_utf16(v.iter().cloned()).collect::>().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