-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.cpp
More file actions
286 lines (268 loc) · 6.58 KB
/
Copy pathUtil.cpp
File metadata and controls
286 lines (268 loc) · 6.58 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "Util.h"
#include "SObject.h"
#include <random>
#include <limits>
#include <sstream>
#include <iomanip>
#include <regex>
#ifdef max
#undef max
#endif
namespace sh::core {
auto Util::U8StringToWstring(const std::string& u8str) -> std::wstring
{
std::wstring result;
for (int i = 0; i < u8str.size();)
{
unsigned char c0 = u8str[i];
if (c0 >> 3 == 0b11110) //4byte utf
{
c0 = (c0 & 0b00000111);
char c1 = (u8str[i + 1] & 0b00111111);
char c2 = (u8str[i + 2] & 0b00111111);
char c3 = (u8str[i + 3] & 0b00111111);
int unicode = (c3 | c2 << 6) | ((c2 >> 2 | c1 << 4) << 8) | ((c1 >> 4 | c0 << 2) << 16);
//UTF-16 incoding
wchar_t high = 0xD800 + ((unicode - 0x10000) / 0x400);
wchar_t low = 0xDC00 + ((unicode - 0x10000) % 0x400);
result += high;
result += low;
i += 4;
}
else if (c0 >> 4 == 0b1110) //3byte utf
{
c0 = (c0 & 0b00001111);
char c1 = (u8str[i + 1] & 0b00111111);
char c2 = (u8str[i + 2] & 0b00111111);
result += (c2 | c1 << 6) | ((c1 >> 2 | c0 << 4) << 8);
i += 3;
}
else if (c0 >> 5 == 0b110) //2byte utf
{
c0 = (c0 & 0b00011111);
char c1 = (u8str[i + 1] & 0b00111111);
result += (c1 | c0 << 6) | (c0 >> 2 << 8);
i += 2;
}
else if (c0 >> 7 == 0b0) //1byte utf
{
result += c0;
++i;
}
}
return result;
}
auto Util::AlignTo(uint32_t value, uint32_t alignment) -> uint32_t
{
// ex) value = 20, alignment = 16
// value = 0001'0100
// alignment = 0001'0000
// alignment - 1 = 0000'1111
// ~(alignment - 1) = 1111'0000 - 끝 4비트가 0이면 16의 배수라는 뜻.
// 35 = 0010'0011
// 0010'0011 & 1111'0000 = 0010'0000 = 32
return (value + alignment - 1) & ~(alignment - 1);
}
SH_CORE_API auto Util::RandomRange(uint32_t min, uint32_t max) -> uint32_t
{
std::uniform_int_distribution<uint32_t> rnd{ min, max };
return rnd(gen);
}
SH_CORE_API auto Util::RandomRange(int min, int max) -> int
{
std::uniform_int_distribution<int> rnd{ min, max };
return rnd(gen);
}
SH_CORE_API auto Util::RandomRange(float min, float max) -> float
{
std::uniform_real_distribution<float> rnd{ min, max };
return rnd(gen);
}
SH_CORE_API auto Util::RandomRange(double min, double max) -> double
{
std::uniform_real_distribution<double> rnd{ min, max };
return rnd(gen);
}
SH_CORE_API auto Util::ReplaceSpaceString(const std::string& str) -> std::string
{
std::string result;
result.reserve(str.size());
for (char c : str)
{
if (std::isalnum(static_cast<unsigned char>(c)))
result.push_back(c);
else
result.push_back('_');
}
return result;
}
SH_CORE_API auto Util::ConvertByteToWord(const std::vector<uint8_t>& bytes) -> std::vector<uint32_t>
{
std::vector<uint32_t> result;
uint32_t word = 0;
for (int i = 0; i < bytes.size(); ++i)
{
word |= static_cast<uint32_t>(bytes[i]) << (i % 4) * 8;
if ((i + 1) % 4 == 0)
{
result.push_back(word);
word = 0;
}
}
if (bytes.size() % 4 != 0)
{
result.push_back(word);
}
return result;
}
SH_CORE_API auto Util::ConvertMat2ToMat4(const glm::mat2& mat) -> glm::mat4
{
return glm::mat4
{
mat[0][0], mat[0][1], 0.f, 0.f,
mat[1][0], mat[1][1], 0.f, 0.f,
0.f, 0.f, 0.f, 0.f,
0.f, 0.f, 0.f, 0.f
};
}
SH_CORE_API auto Util::ConvertMat4ToMat2(const glm::mat4& mat) -> glm::mat2
{
return glm::mat2
{
mat[0][0], mat[0][1],
mat[1][0], mat[1][1],
};
}
SH_CORE_API auto Util::ConvertMat3ToMat4(const glm::mat3& mat) -> glm::mat4
{
return glm::mat4
{
mat[0][0], mat[0][1], mat[0][2], 0.f,
mat[1][0], mat[1][1], mat[1][2], 0.f,
mat[2][0], mat[2][1], mat[2][2], 0.f,
0.f, 0.f, 0.f, 0.f
};
}
SH_CORE_API auto Util::ConvertMat4ToMat3(const glm::mat4& mat) -> glm::mat3
{
return glm::mat3
{
mat[0][0], mat[0][1], mat[0][2],
mat[1][0], mat[1][1], mat[1][2],
mat[2][0], mat[2][1], mat[2][2]
};
}
SH_CORE_API auto Util::ExtractUUIDs(const core::Json& json) -> std::vector<std::string>
{
std::unordered_set<std::string> uuids;
ExtractUUIDsHelper(uuids, json);
return std::vector<std::string>{ uuids.begin(), uuids.end() };
}
void Util::ExtractUUIDsHelper(std::unordered_set<std::string>& uuids, const core::Json& json)
{
static std::regex uuidRegex{ "^[0-9a-f]{32}$", std::regex::optimize };
if (json.is_object())
{
for (auto const& [key, val] : json.items())
{
ExtractUUIDsHelper(uuids, val);
}
}
else if (json.is_array())
{
for (const auto& item : json)
{
ExtractUUIDsHelper(uuids, item);
}
}
else if (json.is_string())
{
const std::string& value = json.get<std::string>();
if (std::regex_match(value, uuidRegex))
uuids.insert(value);
}
}
SH_CORE_API auto Util::UTF8ToUnicode(const char* start, const char* end, uint32_t& unicode) -> const char*
{
if (start >= end)
return end;
const unsigned char c0 = *(start++);
if (c0 < 0x80)
{
unicode = static_cast<uint32_t>(c0);
return start;
}
auto contFn = [&](unsigned char cx) -> bool { return (cx & 0b1100'0000) == 0b1000'0000; };
// 2byte 유니코드
if ((c0 & 0b1110'0000) == 0b1100'0000)
{
if (end - start < 1)
{
unicode = 0xFFFD;
return start;
}
const unsigned char c1 = *start;
if (!contFn(c1))
{
unicode = 0xFFFD;
return start;
}
const uint32_t v = ((c0 & 0b0001'1111) << 6) | (c1 & 0b0011'1111);
if (v < 0x80)
unicode = 0xFFFD;
else
++start;
unicode = v;
return start;
}
// 3byte 유니코드
if ((c0 & 0b1111'0000) == 0b1110'0000)
{
if (end - start < 2)
{
unicode = 0xFFFD;
return start;
}
const unsigned char c1 = start[0];
const unsigned char c2 = start[1];
if (!contFn(c1) || !contFn(c2))
{
unicode = 0xFFFD;
return start;
}
const uint32_t v = ((c0 & 0b0000'1111) << 12) | ((c1 & 0b0011'1111) << 6) | (c2 & 0b0011'1111);
if (v < 2048 || (v >= 55296 && v <= 57343))
unicode = 0xFFFD;
else
start += 2;
unicode = v;
return start;
}
// 4byte 유니코드
if ((c0 & 0b1111'0000) == 0b1110'0000)
{
if (end - start < 3)
{
unicode = 0xFFFD;
return start;
}
const unsigned char c1 = start[0];
const unsigned char c2 = start[1];
const unsigned char c3 = start[2];
if (!contFn(c1) || !contFn(c2) || !contFn(c3))
{
unicode = 0xFFFD;
return start;
}
const uint32_t v = ((c0 & 0b0000'0111) << 18) | ((c1 & 0b0011'1111) << 12) | ((c2 & 0b0011'1111) << 6) | (c3 & 0b0011'1111);
if (v < 65536 || v > 1114111)
unicode = 0xFFFD;
else
start += 3;
unicode = v;
return start;
}
unicode = 0xFFFD;
return start;
}
}//namespace