-
Notifications
You must be signed in to change notification settings - Fork 278
Optimize to_string
#1615
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
base: master
Are you sure you want to change the base?
Optimize to_string
#1615
Changes from 6 commits
d9c702c
a81cb51
daa9e9f
d790563
4120584
fa9d953
f7e67bf
33fbb32
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 |
|---|---|---|
|
|
@@ -719,8 +719,18 @@ WINRT_EXPORT namespace winrt | |
| return{}; | ||
| } | ||
|
|
||
| #if defined(__cpp_lib_string_resize_and_overwrite) && __cpp_lib_string_resize_and_overwrite >= 202110L | ||
| std::string result; | ||
| result.resize_and_overwrite(size, [&](char* buffer, std::size_t) -> std::size_t | ||
| { | ||
| auto bytes_written = WINRT_IMPL_WideCharToMultiByte(65001 /*CP_UTF8*/, 0, value.data(), static_cast<std::int32_t>(value.size()), buffer, size, nullptr, nullptr); | ||
| WINRT_VERIFY_(size, bytes_written); | ||
| return bytes_written == size ? size : 0; | ||
|
Member
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. Nit: This slightly changes the specified behavior from the original. Now we truncate to zero if written differs from size, whereas before we would simply accept the result. In practice, assuming WideCharToMultiByte behaves sanely, written should always equal the value of size (obtained from the first call to WCTMB). I think we are committing to assuming this sane behavior, so let's keep the defensive guardrails consistent, and go ahead and return bytes_written directly. The WINRT_VERIFY_ sanity check is fine to keep as is to warn if something weird happened. |
||
| }); | ||
|
YexuanXiao marked this conversation as resolved.
|
||
| #else | ||
| std::string result(size, '?'); | ||
| WINRT_VERIFY_(size, WINRT_IMPL_WideCharToMultiByte(65001 /*CP_UTF8*/, 0, value.data(), static_cast<std::int32_t>(value.size()), result.data(), size, nullptr, nullptr)); | ||
| #endif | ||
| return result; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Feels slightly dirty to capture
sizeinstead of using the lambda's param. Perhaps this is simplified if we are selective about our lambda captures and capture only&value.