forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cpp
More file actions
106 lines (99 loc) · 2.71 KB
/
Copy pathUtil.cpp
File metadata and controls
106 lines (99 loc) · 2.71 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
#include "PCH.h"
#include "Util.h"
#include "SObject.h"
#include <random>
#include <limits>
#include <sstream>
#include <iomanip>
#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;
}
bool IsValid(const SObject* obj)
{
if (obj == nullptr)
return false;
return !obj->IsPendingKill();
}
auto Util::GetElapsedTime(const std::function<void()>& func) -> std::chrono::milliseconds
{
auto start = std::chrono::high_resolution_clock::now();
func();
auto end = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
}
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);
}
}