-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.h
More file actions
executable file
·177 lines (151 loc) · 4.37 KB
/
StringUtils.h
File metadata and controls
executable file
·177 lines (151 loc) · 4.37 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <algorithm>
#ifndef STRING_UTILS_H
#define STRING_UTILS_H
namespace StringUtils{
static inline bool isBlank(char a){
if((a==' ')||(a=='\n')||(a=='\r')||(a=='\t')){
return true;
}
return false;
}
/**
* Checks if a string is whitespace or empty.
* e.g.
* "" => true
* " " => true
* "\t\n" => true
* " a" => false
*/
static inline bool isBlank(std::string str){
if(str.empty()){
return true;
}
for(auto i : str){
if(isBlank(i)){
return false;
}
}
return true;
}
template <class T>
static inline bool isNotBlank(T a){
return !isBlank(a);
}
/**
* convert a foundation type value to a string
* e.g.
* 1 to "1", 1.1 to "1.1"
*/
template <class T>
static inline std::string convert2str(T a){
std::string res;
std::stringstream ss;
ss<<a;
ss>>res;
return res;
}
/**
* convert a string to a other foundation type value
*/
template <class T>
static inline void convert2other(const std::string str, T &res){
std::stringstream ss;
ss<<str;
ss>>res;
}
/**
* count the number of subStr appearance in source string
*/
static inline size_t countSubStr(const std::string source, const std::string subStr){
int count=0;
int res = source.find(subStr);
while(res != std::string::npos){
count++;
res = source.find(subStr, ++res);
}
return count;
}
/**
* filter blank strings
*/
static inline std::vector<std::string> compact(const std::vector<std::string> &tokens){
std::vector<std::string> compacted;
for(auto i : tokens){
if(!isBlank(i)){
compacted.push_back(i);
}
}
return compacted;
}
static inline std::vector<std::string> split(const std::string &str, const std::string &delim, const bool trim_empty = false){
size_t pos, last_pos = 0, len;
std::vector<std::string> tokens;
while(true) {
pos = str.find(delim, last_pos);
if (pos == std::string::npos) {
pos = str.size();
}
len = pos-last_pos;
if ( !trim_empty || len != 0) {
tokens.push_back(str.substr(last_pos, len));
}
if (pos == str.size()) {
break;
} else {
last_pos = pos + delim.size();
}
}
return tokens;
}
static inline std::string join(const std::vector<std::string> &tokens, const std::string &delim, const bool trim_empty = false){
if(trim_empty) {
return join(compact(tokens), delim, false);
} else {
std::stringstream ss;
for(size_t i=0; i<tokens.size()-1; ++i) {
ss << tokens[i] << delim;
}
ss << tokens[tokens.size()-1];
return ss.str();
}
}
static inline std::string trim(const std::string &str){
size_t begin = 0, size = str.size();
while((begin<size)&&isBlank(str[begin])){
begin++;
}
if(begin==size){
return "";
}
size_t end = size-1;
while((end>begin)&&isBlank(str[end])){
end--;
}
return str.substr(begin, end-begin+1);
}
static inline std::string repeat(const std::string &str, size_t times){
std::stringstream ss;
for(size_t i=0; i<times; ++i) {
ss << str;
}
return ss.str();
}
static inline std::string replaceAll(const std::string &source, const std::string &target, const std::string &replacement){
return join(split(source, target, false), replacement, false);
}
static inline std::string toUpper(const std::string &str){
std::string s(str);
std::transform(s.begin(), s.end(), s.begin(), toupper);
return s;
}
static inline std::string toLower(const std::string &str){
std::string s(str);
std::transform(s.begin(), s.end(), s.begin(), tolower);
return s;
}
}
#endif