mirror of
https://github.com/Blinue/Magpie.git
synced 2026-06-24 02:04:10 +00:00
62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
#include "pch.h"
|
|
#include "StrUtils.h"
|
|
|
|
|
|
extern std::shared_ptr<spdlog::logger> logger;
|
|
|
|
std::wstring StrUtils::UTF8ToUTF16(std::string_view str) {
|
|
int convertResult = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0);
|
|
if (convertResult <= 0) {
|
|
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("MultiByteToWideChar 失败"));
|
|
assert(false);
|
|
return {};
|
|
}
|
|
|
|
std::wstring r(convertResult + 10, L'\0');
|
|
convertResult = MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &r[0], (int)r.size());
|
|
if (convertResult <= 0) {
|
|
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("MultiByteToWideChar 失败"));
|
|
assert(false);
|
|
return {};
|
|
}
|
|
|
|
return std::wstring(r.begin(), r.begin() + convertResult);
|
|
}
|
|
|
|
std::string StrUtils::UTF16ToUTF8(std::wstring_view str) {
|
|
int convertResult = WideCharToMultiByte(CP_UTF8, 0, str.data(), (int)str.size(), nullptr, 0, nullptr, nullptr);
|
|
if (convertResult <= 0) {
|
|
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("WideCharToMultiByte 失败"));
|
|
assert(false);
|
|
return {};
|
|
}
|
|
|
|
std::string r(convertResult + 10, L'\0');
|
|
convertResult = WideCharToMultiByte(CP_UTF8, 0, str.data(), (int)str.size(), &r[0], (int)r.size(), nullptr, nullptr);
|
|
if (convertResult <= 0) {
|
|
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("WideCharToMultiByte 失败"));
|
|
assert(false);
|
|
return {};
|
|
}
|
|
|
|
return std::string(r.begin(), r.begin() + convertResult);
|
|
}
|
|
|
|
void StrUtils::Trim(std::string_view& str) {
|
|
for (int i = 0; i < str.size(); ++i) {
|
|
if (!isspace(str[i])) {
|
|
str.remove_prefix(i);
|
|
|
|
size_t i = str.size() - 1;
|
|
for (; i > 0; --i) {
|
|
if (!isspace(str[i])) {
|
|
break;
|
|
}
|
|
}
|
|
str.remove_suffix(str.size() - 1 - i);
|
|
return;
|
|
}
|
|
}
|
|
|
|
str.remove_prefix(str.size());
|
|
}
|