-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringTools.cpp
73 lines (61 loc) · 1.9 KB
/
StringTools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifdef _WIN32
#include <windows.h>
#endif
#include "StringTools.h"
namespace mvshyvk
{
// Conversion utility::string_t to std::string
std::string ToString(utility::string_t srcString)
{
#ifdef _WIN32
// Allocating buffer
auto size = WideCharToMultiByte(CP_ACP, NULL, srcString.c_str(), -1,
nullptr, 0, nullptr, nullptr);
auto convertedString = std::shared_ptr<char>(new char[size]);
// Converting Unicode to ASCII
WideCharToMultiByte(CP_ACP, NULL, srcString.c_str(), -1,
convertedString.get(), size, nullptr, nullptr);
return std::string(convertedString.get());
#else
return srcString;
#endif
}
// Conversion std::string to utility::string_t
utility::string_t FromString(std::string srcString)
{
#ifdef _WIN32
// Allocating buffer
auto size = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, srcString.c_str(), -1,
nullptr, 0);
auto wideStr = std::shared_ptr<WCHAR>(new WCHAR[size]);
// Converting to wide string
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, srcString.c_str(), -1,
wideStr.get(), size);
return utility::string_t(wideStr.get());
#else
return srcString;
#endif
}
// Conversion UTF std::string to platform/console specific std::string (UTF8 for linux or ASCII for Windows)
std::string FromUtf8String(std::string srcString)
{
#ifdef _WIN32
// UTF8 to wide char
int size = MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, srcString.c_str(), -1,
nullptr, 0);
auto wideStr = std::shared_ptr<WCHAR>(new WCHAR[size]);
MultiByteToWideChar(CP_UTF8, MB_PRECOMPOSED, srcString.c_str(), -1,
wideStr.get(), size);
// Wide char â ASCII
BOOL bUsedDefaultCharacter = FALSE;
size = WideCharToMultiByte(CP_ACP, 0, wideStr.get(), -1,
nullptr, 0, "?", &bUsedDefaultCharacter);
auto asciiStr = std::shared_ptr<char>(new char[size]);
WideCharToMultiByte(CP_ACP, 0, wideStr.get(), -1,
asciiStr.get(), size, "?", &bUsedDefaultCharacter);
return std::string(asciiStr.get());
#else
return srcString;
#endif
}
}