CVB++ 15.1
Loading...
Searching...
No Matches
string.hpp
1#pragma once
2
3#include <sstream>
4#include <string>
5#include <regex>
6#include <algorithm>
7#include <iterator>
8
9#ifdef _MSC_VER
10# ifndef NOMINMAX
11# define NOMINMAX
12# endif
13# include <Windows.h>
14#elif __GNUC__
15# include <codecvt>
16# include <locale>
17
18#endif
19
20#include "global.hpp"
21
22namespace Cvb
23{
24
26 CVB_BEGIN_INLINE_NS
28
29#if defined _WIN32 && defined(UNICODE)
30
31 using String = std::wstring;
32
33 using StringStream = std::wstringstream;
34
35 using Char = wchar_t;
36
37 using Regex = std::wregex;
38
39# define CVB_LIT(STR) L##STR
40#else
41
43
50
52
57
59
63 using Char = char;
64
66
71
73# define CVB_LIT(STR) STR
74
75#endif
76
77 namespace Internal
78 {
79
80 inline bool IsAccessToken(const String &provider)
81 {
82 return provider.front() == CVB_LIT('{') && provider.back() == CVB_LIT('}');
83 }
84
85 inline std::string CastToAscii(const String &native)
86 {
87 std::string ascii;
88 std::transform(native.begin(), native.end(), std::back_inserter(ascii),
89 [](String::value_type ch) { return static_cast<char>(ch); });
90 return ascii;
91 }
92
93 inline std::string WideCharStrToUTF8(const std::wstring &inWStr)
94 {
95 if (!inWStr.capacity())
96 return "";
97 std::string retval;
98
99#ifdef _WIN32
100 auto call = [&inWStr](char *str, int pos) -> int {
101 return WideCharToMultiByte(CP_UTF8, 0, inWStr.data(), -1, str, pos, nullptr, nullptr);
102 };
103 int n = call(nullptr, 0);
104 if (n <= 0)
105 {
106 // if error happens, do ugly returning of the raw content (encoding ignorant)
107 std::string str(inWStr.length(), 0);
108 std::transform(inWStr.begin(), inWStr.end(), str.begin(), [](wchar_t c) { return (char)c; });
109 return std::string("error in utf8-conversion of the following error message: ") + str
110 + std::string(" (unencoded)");
111 }
112
113 retval.resize(n - static_cast<int>(1));
114
115 if (call(&retval[0], n) == 0)
116 {
117 // if error happens, do ugly returning of the raw content (encoding ignorant)
118 std::string str(inWStr.length(), 0);
119 std::transform(inWStr.begin(), inWStr.end(), str.begin(), [](wchar_t c) { return (char)c; });
120 return std::string("error in utf8-conversion of the following error message: ") + str
121 + std::string(" (unencoded)");
122 }
123
124 return retval;
125
126#else
128 return converter.to_bytes(inWStr.data());
129#endif
130 }
131
132 inline bool IsASCII(const std::string &str)
133 {
134 for (const unsigned char& c : str)
135 {
136 if (c > 0x7F)
137 return false;
138 }
139 return true;
140 }
141
142 inline std::string ToUTF8(const std::string& str)
143 {
144#ifdef _WIN32
145 if (GetACP() == CP_UTF8 || IsASCII(str))
146 return str;
147
148 // Convert ANSI -> UTF-16 -> UTF-8
149 int wlen = MultiByteToWideChar(CP_ACP, 0, str.data(), static_cast<int>(str.size()), nullptr, 0);
150 if (wlen == 0)
151 return str; // to avoid throwing we return what we have -> may look ugly
152
153 std::wstring wstr(wlen, 0);
154 MultiByteToWideChar(CP_ACP, 0, str.data(), static_cast<int>(str.size()), &wstr[0], wlen);
155
156 return WideCharStrToUTF8(wstr);
157#else
158 return str;
159#endif
160 }
161
162 inline std::string ToUTF8(const std::wstring& str)
163 {
164 return WideCharStrToUTF8(str);
165 }
166
167 } // namespace Internal
168
169 CVB_END_INLINE_NS
170
171} // namespace Cvb
T back_inserter(T... args)
Root namespace for the Image Manager interface.
Definition version.hpp:11
char Char
Character type for wide characters or unicode characters.
Definition string.hpp:63
std::regex Regex
Regular expression for wide characters or unicode characters.
Definition string.hpp:70
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
std::stringstream StringStream
String stream for wide characters or unicode characters.
Definition string.hpp:56
T transform(T... args)