CVB++ 15.0
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 } // namespace Internal
133
134 CVB_END_INLINE_NS
135
136} // namespace Cvb
T back_inserter(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
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)