-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
55 lines (45 loc) · 1.02 KB
/
utils.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
#ifndef UTILS_HPP__
#define UTILS_HPP__
#include <sstream>
#include <string>
#include <vector>
#include <cstddef>
#include <string.h>
template <class T>
std::string stringify(T&& t)
{
std::ostringstream stream;
stream << std::forward<T>(t);
return stream.str();
}
template <class T, class... U>
std::string stringify(T&& t, U&&... u)
{
std::ostringstream stream;
stream << std::forward<T>(t) << stringify(std::forward<U>(u)...);
return stream.str();
}
inline std::vector<std::string> string_split(const std::string &str, const std::string &delim)
{
std::vector<std::string> res;
auto tmp = str;
char *cookie;
const char *s = strtok_r(&tmp[0], delim.c_str(), &cookie);
while (s)
{
res.push_back(s);
s = strtok_r(nullptr, delim.c_str(), &cookie);
}
return res;
}
inline std::string string_join(const std::vector<std::string> &list, const std::string &delim)
{
std::string res;
for (auto &str : list)
{
res += str;
res += delim;
}
return res;
}
#endif