diff options
| author | ljedrz <ljedrz@gmail.com> | 2018-10-31 10:22:22 +0100 |
|---|---|---|
| committer | ljedrz <ljedrz@gmail.com> | 2018-10-31 10:22:22 +0100 |
| commit | 19aa10132cc727c8561730ab096b21d14507c81d (patch) | |
| tree | 7854de65862a4ea4a5a2abd0fac13c7ba66e6c01 /src/liballoc | |
| parent | 0db7abe5b6f3cdfca736f9238689cbea8ef61c7e (diff) | |
| download | rust-19aa10132cc727c8561730ab096b21d14507c81d.tar.gz rust-19aa10132cc727c8561730ab096b21d14507c81d.zip | |
Speed up String::from_utf16
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/string.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/liballoc/string.rs b/src/liballoc/string.rs index ff3587d5d87..8690f9017c4 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<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 |
