-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Speed up String::from_utf16 #55530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Speed up String::from_utf16 #55530
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to have a comment explaining why the code works this way instead of the more "obvious" Basically, what you wrote in the PR should be in the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea; since it is already rolled up, I can add this comment afterwards, along with some other assorted code adjustments. |
||
} else { | ||
return Err(FromUtf16Error(())); | ||
} | ||
} | ||
Ok(ret) | ||
} | ||
|
||
/// Decode a UTF-16 encoded slice `v` into a `String`, replacing | ||
|
Uh oh!
There was an error while loading. Please reload this page.