From ab00c17558deb2d5a8bcf92073b217764edcd76c Mon Sep 17 00:00:00 2001 From: zzyyyl Date: Sat, 10 Sep 2022 04:17:34 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=B0=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 7a7e947a9dc1f014797421bda2dde7433afc2d3b) --- src/MeoAssistant/AsstUtils.hpp | 12 ++++++------ src/MeoAssistant/Logger.hpp | 17 +++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/MeoAssistant/AsstUtils.hpp b/src/MeoAssistant/AsstUtils.hpp index 5ba1c1f6a0..90adc5312c 100644 --- a/src/MeoAssistant/AsstUtils.hpp +++ b/src/MeoAssistant/AsstUtils.hpp @@ -369,10 +369,10 @@ namespace asst::utils #endif template - 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(len) + 1U; auto wstr = new wchar_t[wstr_length]; @@ -399,10 +399,10 @@ namespace asst::utils } template - 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(len) + 1U; auto wsz_ansi = new wchar_t[wsz_ansi_length]; @@ -429,10 +429,10 @@ namespace asst::utils } template - 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(len) + 1U; auto wstr = new wchar_t[wstr_length]; diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 4de3fde851..00df84daa4 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -154,20 +154,21 @@ namespace asst : std::true_type {}; - template - static Stream& stream_put(Stream& s, std::filesystem::path&& v) - { - return stream_put(s, asst::utils::path_to_utf8_string(v)); - } - template static Stream& stream_put(Stream& s, T&& v) { - if constexpr (std::is_constructible_v) { + if constexpr (std::same_as>) { + if constexpr (ToAnsi) + s << utils::utf8_to_ansi(utils::path_to_utf8_string(std::forward(v))); + else + s << utils::path_to_utf8_string(std::forward(v)); + return s; + } + if constexpr (std::convertible_to) { if constexpr (ToAnsi) s << utils::utf8_to_ansi(std::forward(v)); else - s << std::string(std::forward(v)); + s << std::forward(v); return s; } else if constexpr (has_stream_insertion_operator::value) {