perf: 小优化

(cherry picked from commit 7a7e947a9d)
This commit is contained in:
zzyyyl
2022-09-10 04:17:34 +08:00
parent 14924eb8bc
commit ab00c17558
2 changed files with 15 additions and 14 deletions

View File

@@ -369,10 +369,10 @@ namespace asst::utils
#endif
template <typename _ = void>
inline std::string ansi_to_utf8(const std::string& ansi_str)
inline std::string ansi_to_utf8(std::string_view ansi_str)
{
#ifdef _WIN32
const char* src_str = ansi_str.c_str();
const char* src_str = ansi_str.data();
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, nullptr, 0);
const std::size_t wstr_length = static_cast<std::size_t>(len) + 1U;
auto wstr = new wchar_t[wstr_length];
@@ -399,10 +399,10 @@ namespace asst::utils
}
template <typename _ = void>
inline std::string utf8_to_ansi(const std::string& utf8_str)
inline std::string utf8_to_ansi(std::string_view utf8_str)
{
#ifdef _WIN32
const char* src_str = utf8_str.c_str();
const char* src_str = utf8_str.data();
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, nullptr, 0);
const std::size_t wsz_ansi_length = static_cast<std::size_t>(len) + 1U;
auto wsz_ansi = new wchar_t[wsz_ansi_length];
@@ -429,10 +429,10 @@ namespace asst::utils
}
template <typename _ = void>
inline std::string utf8_to_unicode_escape(const std::string& utf8_str)
inline std::string utf8_to_unicode_escape(std::string_view utf8_str)
{
#ifdef _WIN32
const char* src_str = utf8_str.c_str();
const char* src_str = utf8_str.data();
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, nullptr, 0);
const std::size_t wstr_length = static_cast<std::size_t>(len) + 1U;
auto wstr = new wchar_t[wstr_length];

View File

@@ -154,20 +154,21 @@ namespace asst
: std::true_type
{};
template <bool ToAnsi, typename Stream>
static Stream& stream_put(Stream& s, std::filesystem::path&& v)
{
return stream_put<ToAnsi>(s, asst::utils::path_to_utf8_string(v));
}
template <bool ToAnsi, typename Stream, typename T>
static Stream& stream_put(Stream& s, T&& v)
{
if constexpr (std::is_constructible_v<std::string, T>) {
if constexpr (std::same_as<std::filesystem::path, std::remove_cvref_t<T>>) {
if constexpr (ToAnsi)
s << utils::utf8_to_ansi(utils::path_to_utf8_string(std::forward<T>(v)));
else
s << utils::path_to_utf8_string(std::forward<T>(v));
return s;
}
if constexpr (std::convertible_to<T, std::string_view>) {
if constexpr (ToAnsi)
s << utils::utf8_to_ansi(std::forward<T>(v));
else
s << std::string(std::forward<T>(v));
s << std::forward<T>(v);
return s;
}
else if constexpr (has_stream_insertion_operator<Stream, T>::value) {