forked from hasherezade/pin_n_sieve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
90 lines (76 loc) · 2.02 KB
/
Util.cpp
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
#include "Util.h"
#include <algorithm>
#include <sstream>
size_t util::getAsciiLen(const char *inp, size_t maxInp)
{
size_t i = 0;
for (; i < maxInp; i++) {
const char c = inp[i];
if (c == '\0') return i; //end of string
if (!IS_PRINTABLE(c) && !IS_ENDLINE(c)) return 0;
}
return 0;
}
size_t util::getAsciiLenW(const wchar_t *inp, size_t maxInp)
{
size_t i = 0;
for (; i < maxInp; i++) {
const wchar_t w = inp[i];
if (w == 0) return i; //end of string
if (!IS_PRINTABLE(w) && !IS_ENDLINE(w)) return 0;
}
return 0;
}
std::string util::getDllName(const std::string& str)
{
std::size_t len = str.length();
std::size_t found = str.find_last_of("/\\");
std::size_t ext = str.find_last_of(".");
if (ext >= len) return "";
std::string name = str.substr(found + 1, ext - (found + 1));
std::transform(name.begin(), name.end(), name.begin(), std::tolower);
return name;
}
bool util::iequals(const std::string& a, const std::string& b)
{
size_t aLen = a.size();
if (b.size() != aLen) return false;
for (size_t i = 0; i < aLen; ++i) {
if (tolower(a[i]) != tolower(b[i])) return false;
}
return true;
}
size_t util::splitList(const std::string &sline, const char delimiter, std::vector<std::string> &args)
{
std::istringstream f(sline);
std::string s;
while (getline(f, s, delimiter)) {
args.push_back(s);
}
return args.size();
}
static inline void ltrim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
return !std::isspace(ch);
}));
}
static 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());
}
void util::trim(std::string &s)
{
ltrim(s);
rtrim(s);
}
int util::loadInt(const std::string &str)
{
int intVal = 0;
std::stringstream ss;
ss << std::dec << str;
ss >> intVal;
return intVal;
}