diff --git a/include/mio/detail/mmap.ipp b/include/mio/detail/mmap.ipp index 1b6dc4d..716a171 100644 --- a/include/mio/detail/mmap.ipp +++ b/include/mio/detail/mmap.ipp @@ -52,14 +52,17 @@ inline DWORD int64_low(int64_t n) noexcept return n & 0xffffffff; } -std::wstring s_2_ws(const std::string& s) +inline std::wstring s_2_ws(const std::string& s) { - if (s.empty()) - return{}; - const auto s_length = static_cast(s.length()); - auto buf = std::vector(s_length); - const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s_length, buf.data(), s_length); - return std::wstring(buf.data(), wide_char_count); + std::wstring ret; + if (!s.empty()) + { + ret.resize(s.size()); + int wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), + static_cast(s.size()), &ret[0], static_cast(s.size())); + ret.resize(wide_char_count); + } + return ret; } template< diff --git a/single_include/mio/mio.hpp b/single_include/mio/mio.hpp index c568a46..6ad6a74 100644 --- a/single_include/mio/mio.hpp +++ b/single_include/mio/mio.hpp @@ -794,14 +794,17 @@ inline DWORD int64_low(int64_t n) noexcept return n & 0xffffffff; } -std::wstring s_2_ws(const std::string& s) +inline std::wstring s_2_ws(const std::string& s) { - if (s.empty()) - return{}; - const auto s_length = static_cast(s.length()); - auto buf = std::vector(s_length); - const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s_length, buf.data(), s_length); - return std::wstring(buf.data(), wide_char_count); + std::wstring ret; + if (!s.empty()) + { + ret.resize(s.size()); + int wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), + static_cast(s.size()), &ret[0], static_cast(s.size())); + ret.resize(wide_char_count); + } + return ret; } template<