Skip to content

Commit

Permalink
[ADD] : add UTF8 <> wstring decode/encode functions and little refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aiekick committed Mar 13, 2024
1 parent 6f42626 commit 03042e9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
28 changes: 12 additions & 16 deletions include/ctools/cTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ using namespace cocos2d;
namespace ct // cTools
{

/////////////////////////////////////////////
////// UTF8 <> WideString ///////////////////
/////////////////////////////////////////////

// Convert a wide Unicode String to a UTF8 string
CTOOLS_API std::string UTF8Encode(const std::wstring& wstr);

// Convert a UTF8 string to a wide Unicode String
CTOOLS_API std::wstring UTF8Decode(const std::string& str);

/////////////////////////////////////////////
/////////////////////////////////////////////
/////////////////////////////////////////////
Expand All @@ -171,23 +181,9 @@ namespace ct // cTools
// and if this is the good class, the magic_number will point to a random memory zone
// so will not have the good value

inline int64_t EncodeId(const std::string& vArr) {
if (vArr.empty() || vArr.size() != 8U) {
return 0;
}
return vArr[0] | //
(vArr[1] << 8) | //
(vArr[2] << 16) | //
(vArr[3] << 24) | //
((int64_t)(vArr[4]) << 32) | //
((int64_t)(vArr[5]) << 40) | //
((int64_t)(vArr[6]) << 48) | //
((int64_t)(vArr[7]) << 56);
}
CTOOLS_API int64_t EncodeId(const std::string& vArr);

inline bool IsIdEqualTo(const int64_t& vId, char vArr[8]) {
return (EncodeId(vArr) == vId);
}
CTOOLS_API bool IsIdEqualTo(const int64_t& vId, char vArr[8]);

/////////////////////////////////////////////
/////////////////////////////////////////////
Expand Down
57 changes: 55 additions & 2 deletions src/cTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ SOFTWARE.

#include <ctools/cTools.h>

#ifdef WIN32
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN64__) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
#include <Windows.h>
#elif defined(LINUX) or defined(APPLE)
#else
#endif

#include <cstdarg> // For va_start, etc.
Expand All @@ -49,6 +49,59 @@ SOFTWARE.
#include <cwchar>
#endif

std::string ct::UTF8Encode(const std::wstring& wstr) {
std::string res;
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN64__) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
if (!wstr.empty()) {
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
if (size_needed) {
res = std::string(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &res[0], size_needed, NULL, NULL);
}
}
#else
// Suppress warnings from the compiler.
(void)wstr;
#endif // _IGFD_WIN_
return res;
}

// Convert an UTF8 string to a wide Unicode String
std::wstring ct::UTF8Decode(const std::string& str) {
std::wstring res;
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN64__) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
if (!str.empty()) {
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
if (size_needed) {
res = std::wstring(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &res[0], size_needed);
}
}
#else
// Suppress warnings from the compiler.
(void)str;
#endif // _IGFD_WIN_
return res;
}

int64_t ct::EncodeId(const std::string& vArr) {
if (vArr.empty() || vArr.size() != 8U) {
return 0;
}
return vArr[0] | //
(vArr[1] << 8) | //
(vArr[2] << 16) | //
(vArr[3] << 24) | //
((int64_t)(vArr[4]) << 32) | //
((int64_t)(vArr[5]) << 40) | //
((int64_t)(vArr[6]) << 48) | //
((int64_t)(vArr[7]) << 56);
}

bool ct::IsIdEqualTo(const int64_t& vId, char vArr[8]) {
return (EncodeId(vArr) == vId);
}

::std::string ct::toStr(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
Expand Down

0 comments on commit 03042e9

Please sign in to comment.