-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move functions related to encoding to encoding.hpp
- Loading branch information
1 parent
3ee0cc0
commit 6be8c03
Showing
2 changed files
with
48 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
#ifdef _WIN32 | ||
#include <Windows.h> | ||
#endif | ||
#include <string> | ||
|
||
namespace encoding { | ||
|
||
#ifdef _WIN32 | ||
inline std::string ansi2utf8(const std::string &str) { | ||
int wCharLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, 0, 0); | ||
if (wCharLen == 0) | ||
return ""; | ||
wchar_t *wStr = new wchar_t[wCharLen]; | ||
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wStr, wCharLen); | ||
int utf8Len = WideCharToMultiByte(CP_UTF8, 0, wStr, -1, 0, 0, 0, 0); | ||
if (utf8Len == 0) { | ||
delete[] wStr; | ||
return ""; | ||
} | ||
char *utf8Str = new char[utf8Len]; | ||
WideCharToMultiByte(CP_UTF8, 0, wStr, -1, utf8Str, utf8Len, 0, 0); | ||
std::string result(utf8Str); | ||
delete[] wStr; | ||
delete[] utf8Str; | ||
return result; | ||
} | ||
#endif | ||
|
||
inline std::string platform_str(const std::string &str) { | ||
#ifdef _WIN32 | ||
return ansi2utf8(str); | ||
#else | ||
return str; | ||
#endif | ||
} | ||
|
||
} // namespace encoding |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters