-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shared.hpp
127 lines (112 loc) · 3.01 KB
/
Shared.hpp
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
#pragma once
#ifndef SHARED_HPP
#define SHARED_HPP
#include <vector>
#include <string>
#include <algorithm>
#include <condition_variable>
#include <cctype>
#include <cstring>
#include <locale>
#include <iostream>
#include <fstream>
#include <format>
#include <memory>
#include <map>
#include <sstream>
#include <queue>
#include <list>
#include <cstdint>
#include <thread>
#include <mutex>
#include <format>
#include <tuple>
#include <numeric>
#include <functional>
using string = std::string;
using mutex = std::mutex;
using rmutex = std::recursive_mutex;
using sstream = std::stringstream;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
namespace StringExtension {
inline void LeftTrim(string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch)
{ return !std::isspace(ch); }));
}
inline void RightTrim(string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch)
{ return !std::isspace(ch); })
.base(),
s.end());
}
inline void Trim(string& s) {
RightTrim(s);
LeftTrim(s);
}
inline void ToLower(string& s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
{ return std::tolower(c); });
}
inline const std::vector<string> Split(const string& s, const string& separator) {
auto arguments = std::vector<string>();
if (separator == "")
{
auto symbols = std::vector<char>(s.begin(), s.end());
for (auto c : symbols)
arguments.push_back(string{ c });
return arguments;
}
u64 separatorLength = separator.length();
u64 startIndex = 0;
u64 separatorPosition = s.find(separator, startIndex);
while (separatorPosition != string::npos)
{
arguments.push_back(s.substr(startIndex, separatorPosition - startIndex));
startIndex = separatorPosition + separatorLength;
separatorPosition = s.find(separator, startIndex);
}
arguments.push_back(s.substr(startIndex));
return arguments;
}
inline std::vector<string> SplitStd(const string& s) {
auto arguments = std::vector<string>();
string word;
sstream buffer(s);
while (buffer >> word)
arguments.push_back(word);
return arguments;
}
}
inline void GetValue(std::istream& in, const string& value, bool print_to_output, i64 default_value, std::function<bool(string, i64&)> handler, i64& outValue) {
string line;
while (true)
{
if (print_to_output)
std::cout << std::format("\033[95mSet {} (nothing for default): \033[0m", value);
std::getline(in, line);
StringExtension::Trim(line);
if (line.empty())
outValue = default_value;
if (line.empty() or handler(line, outValue))
break;
else if (print_to_output)
std::cout << std::format("\033[91mSorry, it's not a correct {}\033[0m", value) << std::endl;
}
}
inline bool GetNumericValue(const string& value, i64& result, i64 min, i64 max) {
try {
result = std::stoll(value);
if (result > min and result < max)
return true;
}
catch (...) {}
return false;
}
#endif // !SHARED_HPP