From fd9dc4be35b57e3fa57a2b7a7b607ca6603ad7da Mon Sep 17 00:00:00 2001 From: Pavel P Date: Sat, 7 May 2022 23:25:50 +0300 Subject: [PATCH] Avoid multiply defined `s_2_ws` linker error + use std::wstring directly without intermediate std::vector in `s_2_ws` fixes #87 --- include/mio/detail/mmap.ipp | 17 ++++++++++------- single_include/mio/mio.hpp | 17 ++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) 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<