-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
78 lines (65 loc) · 1.54 KB
/
Copy pathutils.cpp
File metadata and controls
78 lines (65 loc) · 1.54 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
#include "utils.h"
#include <algorithm>
#include <regex>
inline void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch)
{ return !std::isspace(ch); }));
}
inline void rtrim(std::string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch)
{ return !std::isspace(ch); })
.base(),
s.end());
}
inline void trim(std::string &s)
{
rtrim(s);
ltrim(s);
}
std::vector<std::string> split(const std::string &str, const std::string &delimiter)
{
std::vector<std::string> tokens;
std::string token;
size_t start = 0;
size_t end = str.find(delimiter);
while (end != std::string::npos)
{
token = str.substr(start, end - start);
trim(token);
if (token.length() != 0)
{
tokens.push_back(token);
}
start = end + delimiter.length();
end = str.find(delimiter, start);
}
token = str.substr(start, str.length() - start);
trim(token);
if (token.length() != 0)
{
tokens.push_back(token);
}
return tokens;
}
bool try_parse_int(const std::string &str, int &value)
{
try
{
value = std::stoi(str);
return true;
}
catch (const std::invalid_argument &e)
{
return false;
}
catch (const std::out_of_range &e)
{
return false;
}
}
bool is_color(const std::string &str)
{
return std::regex_match(str, std::regex("^#\\w{6}$"));
}