-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStrFunc.cpp
326 lines (285 loc) · 9.04 KB
/
StrFunc.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Implementations of the string-operation functions
*
* 2010 by Jian Yang <[email protected]>
*
* This file is distributed under the GNU General Public
* License, Version 2. Please see the file COPYING for more
* details
*/
#include "StrFunc.h"
int StrFunc::split_string(const string &str, vector<string> &vec_str, string separator)
{
if(str.empty()) return 0;
vec_str.clear();
int i=0;
bool look=false;
string str_buf;
string symbol_pool="`1234567890-=~!@#$%^&*()_+qwertyuiop[]\\asdfghjkl;'zxcvbnm,./QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>? \t\n";
string::size_type pos;
for(i=0; i<separator.size(); i++){
pos=symbol_pool.find(separator[i]);
if( pos!=string::npos ) symbol_pool.erase(symbol_pool.begin()+pos);
}
for(i=0; i<str.size(); i++){
if( symbol_pool.find(str[i])!=string::npos ){
if(!look) look=true;
str_buf += str[i];
}
else{
if(look){
look=false;
vec_str.push_back(str_buf);
str_buf.erase(str_buf.begin(), str_buf.end());
}
}
}
if(look) vec_str.push_back(str_buf);
return (int)vec_str.size();
}
string StrFunc::first_string(const string &str, const char separator)
{
int pos=(int)str.find(separator);
if(pos!=-1) return string(str.begin(), str.begin()+pos);
return string("");
}
string StrFunc::last_string(const string &str, const char separator)
{
int pos=(int)str.find_last_of(separator);
if(pos!=-1) return string(str.begin()+pos+1, str.end());
return string("");
}
void StrFunc::to_upper(char* str, int len)
{
int i=0;
for(i=0; i<len; i++){
if(str[i]>='a' && str[i]<='z') str[i]+='A'-'a';
}
}
void StrFunc::to_upper(string &str)
{
int i=0;
for(i=0; i<str.size(); i++){
if(str[i]>='a' && str[i]<='z') str[i]+='A'-'a';
}
}
void StrFunc::to_lower(string &str)
{
int i=0;
for(i=0; i<str.size(); i++){
if(str[i]>='A' && str[i]<='Z') str[i]-='A'-'a';
}
}
string StrFunc::get_sub_str(const string & rst, int pos)
{
vector<string> vs_buf;
StrFunc::split_string(rst, vs_buf);
return vs_buf[pos];
}
bool StrFunc::StrEqual(const string &StrA, const string &StrB, bool NoCaseSens)
{
if(!NoCaseSens) return StrA==StrB;
string StrBufA=StrA, StrBufB=StrB;
to_upper(StrBufA);
to_upper(StrBufB);
return StrBufA==StrBufB;
}
bool StrFunc::StrVecEqual(const vector<string> &VsBufA, const vector<string> &VsBufB, int Pos)
{
int SizeA=VsBufA.size(), SizeB=VsBufB.size();
if(SizeA!=SizeB) return false;
if(Pos>=SizeA) throw("Invalid Pos! StrFunc::StrVecEqual");
int i=0;
for(i=Pos; i<SizeA; i++){
if(VsBufA[i]!=VsBufB[i]) return false;
}
return true;
}
bool StrFunc::str_within_quto(const string &str, string &str_buf)
{
unsigned int begin=str.find_first_of("\"");
unsigned int end=str.find_last_of("\"");
if(begin==string::npos || end==string::npos || begin==end) return false;
str_buf="";
str_buf.insert(str_buf.begin(), str.begin()+begin+1, str.begin()+end);
return true;
}
vector<string>::iterator StrFunc::find(vector<string> &target_vs, const string &target_str)
{
string str_buf=target_str;
vector<string> vs_buf=target_vs;
int i=0;
for(i=0; i<vs_buf.size(); i++) to_upper(vs_buf[i]);
to_upper(str_buf);
return target_vs.begin()+(std::find(vs_buf.begin(), vs_buf.end(), str_buf)-vs_buf.begin());
}
string::iterator StrFunc::find(string &target_str, const char target_ch)
{
char ch_buf=target_ch;
string str_buf=target_str;
to_upper(str_buf);
if(ch_buf>'a' && ch_buf<'z') ch_buf+='A'-'a';
return target_str.begin()+(std::find(str_buf.begin(), str_buf.end(), ch_buf)-str_buf.begin());
}
bool StrFunc::goto_str(std::istream &in_file, const string &str)
{
string str_buf;
string query_str=str;
vector<string> vs_buf;
StrFunc::to_upper(query_str);
while(in_file>>str_buf){
if( StrFunc::split_string(str_buf, vs_buf)>0 ) str_buf=vs_buf[0];
else continue;
StrFunc::to_upper(str_buf);
if(str_buf=="#") { getline(in_file, str_buf); continue; }
if(str_buf==query_str) return true;
}
return false;
}
void StrFunc::rewind_if(std::istream &in_file)
{
in_file.clear(ios::goodbit);
in_file.seekg(ios::beg);
}
void StrFunc::getUnique(vector<string> &a)
{
sort(a.begin(),a.end());
vector<string> ::iterator it=unique(a.begin(),a.end());
a.erase(it,a.end());
}
void StrFunc::match(const vector<string> &VecA, const vector<string> &VecB, vector<int> &VecC)
{
int i=0;
map<string, int> id_map;
map<string, int>::iterator iter;
VecC.clear();
for(i=0; i<VecB.size(); i++) id_map.insert(pair<string,int>(VecB[i], i));
for(i=0; i<VecA.size(); i++){
iter=id_map.find(VecA[i]);
if(iter==id_map.end()) VecC.push_back(-9);
else VecC.push_back(iter->second);
}
}
void StrFunc::match_only(const vector<string> &VecA, const vector<string> &VecB, vector<uint32_t> &VecC)
{
int i = 0;
map<string, int> id_map;
map<string, int>::iterator iter;
VecC.clear();
for (i = 0; i<VecB.size(); i++) id_map.insert(pair<string, int>(VecB[i], i));
for (i = 0; i<VecA.size(); i++){
iter = id_map.find(VecA[i]);
if (iter != id_map.end()) VecC.push_back(iter->second);
}
}
void StrFunc::match_only(const vector<string> &VecA, const vector<string> &VecB, vector<int> &VecC)
{
int i = 0;
map<string, int> id_map;
map<string, int>::iterator iter;
VecC.clear();
for (i = 0; i<VecB.size(); i++) id_map.insert(pair<string, int>(VecB[i], i));
for (i = 0; i<VecA.size(); i++){
iter = id_map.find(VecA[i]);
if (iter != id_map.end()) VecC.push_back(iter->second);
}
}
void StrFunc::set_complement(const vector<string> &VecA, const vector<string> &VecB,const vector<int> &tmp, vector<int> &VecC)
{
VecC.clear();
for (int i = 0; i<VecB.size(); i++)
if(find(VecA.begin(),VecA.end(),VecB[i])==VecA.end()) VecC.push_back(tmp[i]);
}
void StrFunc::set_complement(const vector<string> &VecA, const vector<string> &VecB,const vector<int> &tmp, vector<uint32_t> &VecC)
{
VecC.clear();
for (int i = 0; i<VecB.size(); i++)
if(find(VecA.begin(),VecA.end(),VecB[i])==VecA.end()) VecC.push_back(tmp[i]);
}
int StrFunc::split_string_skip(const string &str, vector<string> &vec_str, string separator, int num2skip) //form head
{
if(str.empty()) return 0;
vec_str.clear();
bool look=false;
string str_buf;
int count=0;
for(int i=0; i<str.size(); i++)
{
if( separator.find(str[i])==string::npos )
{
if(!look) look=true;
str_buf += str[i];
}
else
{
if(look)
{
look=false;
if(++count>num2skip) vec_str.push_back(str_buf);
str_buf.erase(str_buf.begin(), str_buf.end());
}
}
}
if(look) vec_str.push_back(str_buf);
return vec_str.size();
}
bool StrFunc::has_suffix(const std::string &str, const std::string &suffix)
{
return str.size() >= suffix.size() &&
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}
void StrFunc::set_intersect(const vector<string> &VecA, const vector<string> &VecB, vector<string> &VecC)
{
int i = 0;
map<string, int> id_map;
map<string, int>::iterator iter;
VecC.clear();
for (i = 0; i<VecB.size(); i++) id_map.insert(pair<string, int>(VecB[i], i));
for (i = 0; i<VecA.size(); i++){
iter = id_map.find(VecA[i]);
if (iter != id_map.end()) VecC.push_back(iter->first);
}
}
// vector C returns string in vector A but not in vector B
void StrFunc::set_difference(const vector<string> &VecA, const vector<string> &VecB, vector<string> &VecC)
{
int i = 0;
map<string, int> id_map;
map<string, int>::iterator iter;
VecC.clear();
for (i = 0; i<VecB.size(); i++) id_map.insert(pair<string, int>(VecB[i], i));
for (i = 0; i<VecA.size(); i++){
iter = id_map.find(VecA[i]);
if (iter == id_map.end()) VecC.push_back(VecA[i]);
}
}
void StrFunc::set_intersect(const vector<int> &VecA, const vector<int> &VecB, vector<int> &VecC)
{
int i = 0;
map<int, int> id_map;
map<int, int>::iterator iter;
VecC.clear();
for (i = 0; i<VecB.size(); i++) id_map.insert(pair<int, int>(VecB[i], i));
for (i = 0; i<VecA.size(); i++){
iter = id_map.find(VecA[i]);
if (iter != id_map.end()) VecC.push_back(iter->first);
}
}
void StrFunc::set_complement(const vector<int> &toRm, const vector<int> &source, vector<int> &VecC)
{
int i = 0;
map<int, int> id_map;
map<int, int>::iterator iter;
VecC.clear();
for (i = 0; i<toRm.size(); i++) id_map.insert(pair<int, int>(toRm[i], i));
for (i = 0; i<source.size(); i++){
iter = id_map.find(source[i]);
if (iter == id_map.end()) VecC.push_back(source[i]);
}
}
bool StrFunc::stringNumCheck(string a, int num)
{
vector<string> b;
int number = split_string(a, b, ", \t\n");
return(number==num);
}