-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.h
95 lines (74 loc) · 2.02 KB
/
Utility.h
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
#pragma once
#include "pch.h"
namespace Utility
{
std::string GUIDGenerator()
{
srand(static_cast<unsigned int>(time(NULL)));
const std::string characters = "0123456789ABCDEF";
std::string guid;
guid += '{';
for (auto i = 0; i < 36; i++)
{
if (i == 8 || i == 13 || i == 18 || i == 23) {
guid += '-';
continue;
}
const auto index = rand() % characters.size();
guid += characters.at(index);
}
guid += '}';
return guid;
}
std::vector<std::string> SplitByDelimiters(const std::string& aString, std::vector<std::string> aDelimiters)
{
std::sort(aDelimiters.begin(), aDelimiters.end(),
[](const std::string& first, const std::string& second) {
return first.size() > second.size();
}
);
std::vector<std::string> output;
std::string stringCopy(aString);
size_t index{};
size_t delimSize{};
while (true)
{
size_t indexMin = std::numeric_limits<size_t>::max();
for (const auto& delimitator : aDelimiters)
{
index = stringCopy.find(delimitator);
if (index != std::string::npos && index < indexMin)
{
indexMin = index;
delimSize = delimitator.size();
}
}
if (indexMin == std::numeric_limits<size_t>::max())
{
output.push_back(stringCopy);
break;
}
output.push_back(stringCopy.substr(0, indexMin));
stringCopy = stringCopy.substr(indexMin + delimSize, stringCopy.size());
}
auto last = std::remove_if(output.begin(), output.end(),
[](const std::string& obj) {
return obj.size() == 0;
}
);
output = std::vector<std::string>(output.begin(), last);
return output;
}
template <typename MeasureUnit = std::chrono::milliseconds , typename Clock = std::chrono::high_resolution_clock>
struct perf_timer
{
template <typename F, typename... Args>
static MeasureUnit countDuration(F&& f, Args... args)
{
const auto start = Clock::now();
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
const auto end = Clock::now();
return std::chrono::duration_cast<MeasureUnit>(end - start);
}
};
}