-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.hpp
More file actions
69 lines (59 loc) · 1.33 KB
/
utils.hpp
File metadata and controls
69 lines (59 loc) · 1.33 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
#pragma once
#include "IRCserver.hpp"
template <typename T>
T initialized()
{
T tmp;
std::memset(&tmp, 0, sizeof(T));
return tmp;
}
template <typename T, typename U>
void insist(T ret, U err, const std::string &msg)
{
if (ret == err)
throw std::runtime_error(msg);
}
template <typename T>
std::string join(T begin, T end, std::string delim)
{
std::string joined;
for (T it = begin; it != end; it++)
{
joined += *it;
if (it + 1 != end)
joined += delim;
}
return joined;
}
template <typename T>
std::string to_string(T v)
{
std::stringstream ss;
ss << v;
return ss.str();
}
template <typename T>
T to_number(const std::string &str)
{
std::istringstream iss(str);
T ret;
iss >> ret;
if (iss.fail() || !iss.eof())
throw std::runtime_error("Invalid number");
return ret;
}
template <typename T>
T to_number_safe(const std::string &str)
{
try {
return to_number<T>(str);
} catch (std::exception &e) {
return 0;
}
}
std::string c(int code);
std::string escape(const std::string &str);
std::string trimstr(const std::string &str);
std::string join(const std::string arr[], size_t size, const std::string& separator);
#define JN(...) join((const std::string[]){__VA_ARGS__}, sizeof((const std::string[]){__VA_ARGS__})/sizeof(std::string), " ")
std::vector<std::string> split(const std::string &str, char delim, bool trim = false);