-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringUtility.h
122 lines (111 loc) · 2.77 KB
/
StringUtility.h
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
#ifndef __STRINGUTILITY_H_CMC__
#define __STRINGUTILITY_H_CMC__
#include <string>
using namespace std;
inline bool hasStr (string str, string key)
{
size_t pos = str.find (key);
return !(pos == std::string::npos);
}
inline void eraseSubStr (string& mainStr, const string& toErase)
{
// Search for the substring in string
size_t pos = mainStr.find(toErase);
if (pos != string::npos)
{
// If found then erase it from string
mainStr.erase(pos, toErase.length());
}
}
// trim from left
inline void lstrip (std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(0, s.find_first_not_of(t));
}
// trim from right
inline void rstrip (std::string& s, const char* t = " \t\n\r\f\v")
{
s.erase(s.find_last_not_of(t) + 1);
}
// trim from left & right
inline void strip (std::string& s, const char* t = " \t\n\r\f\v")
{
rstrip(s, t);
lstrip(s, t);
}
template <typename T>
T convert_type (const string& str)
{
if constexpr (is_same_v <T, string>)
{
return str;
}
else
{
T x;
stringstream ss;
ss << str;
ss >> x;
return x;
}
}
template <typename T=string>
inline vector<T> splitStr (string str, const string& delimiter)
{
vector<T> re;
size_t pos = str.find (delimiter);
while (pos != string::npos)
{
T x = convert_type<T> (str.substr (0, pos));
re.push_back (x);
str = str.substr (pos + delimiter.size());
pos = str.find (delimiter);
}
T x = convert_type<T> (str);
re.push_back (x);
return re;
}
template <typename T>
vector<T> split_str (const string& str, int skip=0)
{
string str2 = str;
rstrip (str2);
vector<T> re;
std::istringstream iss (str2);
string drop;
for(int i = 0; i < skip; i++)
iss >> drop;
T a;
while (iss.good()) {
iss >> a;
re.push_back (a);
}
return re;
}
string raw_string (const string& str)
{
// s is our escaped output string
std::string s = "";
// loop through all characters
for(char c : str)
{
// check if a given character is printable
// the cast is necessary to avoid undefined behaviour
if(isprint((unsigned char)c))
s += c;
else
{
std::stringstream stream;
// if the character is not printable
// we'll convert it to a hex string using a stringstream
// note that since char is signed we have to cast it to unsigned first
stream << std::hex << (unsigned int)(unsigned char)(c);
std::string code = stream.str();
s += std::string("\\x")+(code.size()<2?"0":"")+code;
// alternatively for URL encodings:
//s += std::string("%")+(code.size()<2?"0":"")+code;
}
}
return s;
}
#endif