From 2ad82098140bf3e626af07c7c36c354c539b1df2 Mon Sep 17 00:00:00 2001 From: luocai <168062547@qq.com> Date: Tue, 28 May 2024 15:18:38 +0200 Subject: [PATCH] add wstring to stirng. --- Encrypt/CMakeLists.txt | 2 +- Universal/StringUtility.cpp | 16 +++++++++++++++- Universal/StringUtility.h | 1 + 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Encrypt/CMakeLists.txt b/Encrypt/CMakeLists.txt index 1a2475b..3e8b71a 100644 --- a/Encrypt/CMakeLists.txt +++ b/Encrypt/CMakeLists.txt @@ -28,7 +28,7 @@ target_include_directories(mbedtls-${MBEDTLS_VERSION} ) target_link_libraries(Encrypt - PRIVATE mbedtls-${MBEDTLS_VERSION} + PUBLIC mbedtls-${MBEDTLS_VERSION} ) if(UNIX) diff --git a/Universal/StringUtility.cpp b/Universal/StringUtility.cpp index 756d618..9ffad08 100644 --- a/Universal/StringUtility.cpp +++ b/Universal/StringUtility.cpp @@ -1,8 +1,10 @@ #include "StringUtility.h" #include "BoostLog.h" -#include #include #include +#ifdef WIN32 +#include +#endif namespace Amass { @@ -54,6 +56,18 @@ std::wstring stringToWString(const std::string &string) { return converter.from_bytes(string); } +std::string wstringToString(const std::wstring &string) { +#ifdef WIN32 + int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, string.c_str(), string.size(), NULL, 0, NULL, NULL); + std::string ret(sizeNeeded, 0); + WideCharToMultiByte(CP_UTF8, 0, string.c_str(), string.size(), &ret[0], sizeNeeded, NULL, NULL); +#else + std::string ret; + assert(false && "not implement."); +#endif + return ret; +} + bool equal(std::string_view lhs, std::string_view rhs, bool caseSensitivity) { auto n = lhs.size(); if (rhs.size() != n) return false; diff --git a/Universal/StringUtility.h b/Universal/StringUtility.h index 70d7a10..7da127c 100644 --- a/Universal/StringUtility.h +++ b/Universal/StringUtility.h @@ -60,6 +60,7 @@ size_t utf8Length(const std::string &text); std::string_view utf8At(const std::string &text, size_t index); size_t utf8CharacterByteSize(const char *character); std::wstring stringToWString(const std::string &string); +std::string wstringToString(const std::wstring &string); } // namespace StringUtility