Skip to content

Commit 03042e9

Browse files
committed
[ADD] : add UTF8 <> wstring decode/encode functions and little refactor
1 parent 6f42626 commit 03042e9

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed

include/ctools/cTools.h

+12-16
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ using namespace cocos2d;
161161
namespace ct // cTools
162162
{
163163

164+
/////////////////////////////////////////////
165+
////// UTF8 <> WideString ///////////////////
166+
/////////////////////////////////////////////
167+
168+
// Convert a wide Unicode String to a UTF8 string
169+
CTOOLS_API std::string UTF8Encode(const std::wstring& wstr);
170+
171+
// Convert a UTF8 string to a wide Unicode String
172+
CTOOLS_API std::wstring UTF8Decode(const std::string& str);
173+
164174
/////////////////////////////////////////////
165175
/////////////////////////////////////////////
166176
/////////////////////////////////////////////
@@ -171,23 +181,9 @@ namespace ct // cTools
171181
// and if this is the good class, the magic_number will point to a random memory zone
172182
// so will not have the good value
173183

174-
inline int64_t EncodeId(const std::string& vArr) {
175-
if (vArr.empty() || vArr.size() != 8U) {
176-
return 0;
177-
}
178-
return vArr[0] | //
179-
(vArr[1] << 8) | //
180-
(vArr[2] << 16) | //
181-
(vArr[3] << 24) | //
182-
((int64_t)(vArr[4]) << 32) | //
183-
((int64_t)(vArr[5]) << 40) | //
184-
((int64_t)(vArr[6]) << 48) | //
185-
((int64_t)(vArr[7]) << 56);
186-
}
184+
CTOOLS_API int64_t EncodeId(const std::string& vArr);
187185

188-
inline bool IsIdEqualTo(const int64_t& vId, char vArr[8]) {
189-
return (EncodeId(vArr) == vId);
190-
}
186+
CTOOLS_API bool IsIdEqualTo(const int64_t& vId, char vArr[8]);
191187

192188
/////////////////////////////////////////////
193189
/////////////////////////////////////////////

src/cTools.cpp

+55-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ SOFTWARE.
2727

2828
#include <ctools/cTools.h>
2929

30-
#ifdef WIN32
30+
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN64__) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
3131
#include <Windows.h>
32-
#elif defined(LINUX) or defined(APPLE)
32+
#else
3333
#endif
3434

3535
#include <cstdarg> // For va_start, etc.
@@ -49,6 +49,59 @@ SOFTWARE.
4949
#include <cwchar>
5050
#endif
5151

52+
std::string ct::UTF8Encode(const std::wstring& wstr) {
53+
std::string res;
54+
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN64__) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
55+
if (!wstr.empty()) {
56+
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
57+
if (size_needed) {
58+
res = std::string(size_needed, 0);
59+
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &res[0], size_needed, NULL, NULL);
60+
}
61+
}
62+
#else
63+
// Suppress warnings from the compiler.
64+
(void)wstr;
65+
#endif // _IGFD_WIN_
66+
return res;
67+
}
68+
69+
// Convert an UTF8 string to a wide Unicode String
70+
std::wstring ct::UTF8Decode(const std::string& str) {
71+
std::wstring res;
72+
#if defined(__WIN32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN64__) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
73+
if (!str.empty()) {
74+
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
75+
if (size_needed) {
76+
res = std::wstring(size_needed, 0);
77+
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &res[0], size_needed);
78+
}
79+
}
80+
#else
81+
// Suppress warnings from the compiler.
82+
(void)str;
83+
#endif // _IGFD_WIN_
84+
return res;
85+
}
86+
87+
int64_t ct::EncodeId(const std::string& vArr) {
88+
if (vArr.empty() || vArr.size() != 8U) {
89+
return 0;
90+
}
91+
return vArr[0] | //
92+
(vArr[1] << 8) | //
93+
(vArr[2] << 16) | //
94+
(vArr[3] << 24) | //
95+
((int64_t)(vArr[4]) << 32) | //
96+
((int64_t)(vArr[5]) << 40) | //
97+
((int64_t)(vArr[6]) << 48) | //
98+
((int64_t)(vArr[7]) << 56);
99+
}
100+
101+
bool ct::IsIdEqualTo(const int64_t& vId, char vArr[8]) {
102+
return (EncodeId(vArr) == vId);
103+
}
104+
52105
::std::string ct::toStr(const char* fmt, ...) {
53106
va_list args;
54107
va_start(args, fmt);

0 commit comments

Comments
 (0)