-
Notifications
You must be signed in to change notification settings - Fork 0
/
art_string.cpp
412 lines (353 loc) · 7.51 KB
/
art_string.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "art_string.h"
void ArtString::toLowerCase(string &str)
{
for(int i=0; i< str.length(); i++ )
{
char case_bias = 'A' - 'a';
if ( (str[i] >= 'A') && (str[i] <= 'Z') )
{
str[i]-=case_bias;
}
}
}
void ArtString::toLowerCase()
{
toLowerCase(*this);
}
void ArtString::toUpperCase(string &str)
{
for(int i=0; i< str.length(); i++ )
{
char case_bias = 'A' - 'a';
if ( (str[i] >= 'a') && (str[i] <= 'z') )
{
str[i]+=case_bias;
}
}
}
void ArtString::toUpperCase()
{
toUpperCase(*this);
}
//Deletes the set of symbols
void ArtString::delSymbol(string &str, const string &symb )
{
string temp_str;
bool symb_found = false;
for(int i=0; i< str.length(); i++ )
{
symb_found= false;
for (int j =0; j< symb.length(); j++)
{
if ( str[i] == symb[j] ) symb_found = true;
}
if(!symb_found) temp_str.push_back(str[i]);
}
if (temp_str.size())
{
str = temp_str;
}
}
void ArtString::delSymbol(const string &symb)
{
delSymbol(*this, symb);
}
//Delete given set of symbols in the beginning and in the end of the string
void ArtString::delEdges(string &str, const string &symb)
{
if(str.size() == 0 || symb.size() == 0)
return;
int begin_symb = str.find_first_not_of(symb);
int end_symb = str.find_last_not_of(symb);
if ( begin_symb == 0 && end_symb==(str.size() - 1) )
{
return;
}
if ( ( begin_symb == string::npos ) || (end_symb == string::npos) )
{
str.resize(0);
return;
}
str = str.substr(begin_symb, end_symb - begin_symb + 1);
}
void ArtString::delEdges (const string &symb)
{
delEdges(*this, symb);
}
//The function supports conversion from ASCII to symbols
//The function works in 2 modes:
// 1) No ASCII string mode
// 2) ASCII string mode. Begins after '<' symbol.
//In this mode spaces are considered to be deviders. The function analyse sequences of symbols between spaces
//and tries to convert them into ASCII. If the procedure fails the function adds this sequences AS IS.
//The mode is finished with '>' symbol.
//Examples:
//"aaa<< 98 98 aaa >" => {97 97 97 60 98 98 97 97 97} in other words, the result is "aaa<bbaaa"
string ArtString::convertASCIIcodes( string &str)
{
string num_str;
string temp_str;
string edges =" ";
bool begin = false;
bool error =false;
int num = -1;
for(int i=0; i< str.size(); i++)
{
switch(str[i])
{
case '<':
if (begin)
{
num_str.push_back(str[i]);
}
begin = true;
break;
case '>':
if (begin)
{
num = atoi( num_str.c_str() );
if ( num>0 && num<=127 ) temp_str.push_back(num);
else if (num == 0 && num_str.size()==1 && num_str[0]=='0') temp_str.push_back(num);
else if(num_str.size()) temp_str += num_str;
num_str.resize(0);
begin = false;
}
else
{
temp_str.push_back(str[i]);
}
break;
default:
if (begin)
{
switch (str[i])
{
case ' ':
num = atoi( num_str.c_str() );
if ( num>0 && num<=255 ) temp_str.push_back(num);
else if (num == 0 && num_str.size()==1 && num_str[0]=='0') temp_str.push_back(num);
else if(num_str.size())temp_str += num_str;
num_str.resize(0);
break;
default:
num_str.push_back(str[i]);
break;
}
}
else
{
temp_str.push_back(str[i]);
}
}
cout<<temp_str.size()<<" ";
}
num = atoi( num_str.c_str() );
if ( num>0 && num<=255 ) temp_str.push_back(num);
else if (num == 0 && num_str.size()==1 && num_str[0]=='0') temp_str.push_back(num);
else if(num_str.size()) temp_str += num_str;
return temp_str;
}
string ArtString::convertASCIIcodes( )
{
convertASCIIcodes( *this);
}
long int ArtString::toInt(const string &str_, char * flag_ , bool * conv_ok )
{
long int result = 0;
bool ok = true;
string flag;
const int MODE_NORM = 0;//Convert decimal
const int MODE_HEX = 1;
const int MODE_BIN = 2;
//const int MODE_BIN_SIGN = 3;
//const int MODE_BIN_COMP = 4;
int mode = MODE_NORM;
int scanres = 0;
ArtString str(str_);
str.delEdges(" ");
if (!str.length()) ok = false;
if (flag_) flag = flag_;
ArtString::toLowerCase(flag);
if (flag == "b") mode = MODE_BIN;
//else if (flag == "bs") mode = MODE_BIN_SIGN;
//else if (flat == "bc") mode = MODE_BIN_COMP;
if (str.length() > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X' ))
{
mode = MODE_HEX;
}
if (ok)
{
switch (mode)
{
case MODE_BIN:
ok = str.find_last_not_of("01") == string::npos;
if (ok)
{
result = 0;
long int mask = 1;
int offset = 0;
for (int i= str.length() - 1; i >= 0; i--)
{
if(str[i]=='1') result |= mask<<offset;
offset++;
}
}
break;
case MODE_HEX:
scanres = sscanf( str.c_str() , "%x", &result);
ok = (scanres != EOF) && scanres;
break;
case MODE_NORM:
default:
ok = (str.find_last_of( "+-") == 0 || str.find_last_of( "+-") == string::npos ) && (str.find_last_not_of("0123456789+-") == string::npos);
if (ok)
{
result = atoi( str.c_str() );
}
break;
}
}
if (conv_ok) *conv_ok = ok;
return result;
}
long int ArtString::toInt(char* flag, bool* conv_ok)
{
return toInt(*this, flag, conv_ok);
}
//Pattern for conversion: <+/-><num><.><num><e/E><+/-><num>, i.e.: +000.000e+000
double ArtString::toDouble( const string &str_, bool * conv_ok )
{
bool ok = true;
double result = 0;
int check_step = 0;
int sign = 1;
string mantissa;
double mantissa_num=0;
int exp_sign = 1;
string exponent;
double exp_num=0;
const int EXPONENT_STEP = 3;
ArtString str(str_);
str.delEdges(" ");
if (!str.length()) ok = false;
for (int i = 0; i< str.length(); i++)
{
if (!ok) break;
switch( check_step )
{
case 0:
if (str[i] == '+' )
{
sign = 1;
check_step++;
mantissa.push_back('0');
}
else if (str[i] == '-' )
{
sign = -1;
check_step++;
mantissa.push_back('0');
}
else if (str[i] >= '0' && str[i] <= '9' )
{
sign = 1;
check_step++;
mantissa.push_back(str[i]);
}
else if(str[i] == 'e' || str[i] == 'E')
{
check_step = EXPONENT_STEP;
}
else
{
ok = false;
}
break;
case 1:
if (str[i] >= '0' && str[i] <= '9' )
{
mantissa.push_back(str[i]);
}
else if (str[i] == '.')
{
mantissa.push_back(str[i]);
check_step++;
}
else if(str[i] == 'e' || str[i] == 'E')
{
check_step = EXPONENT_STEP;
}
else
{
ok = false;
}
break;
case 2:
if (str[i] >= '0' && str[i] <= '9' )
{
mantissa.push_back(str[i]);
}
else if(str[i] == 'e' || str[i] == 'E')
{
check_step = EXPONENT_STEP;
}
else
{
ok = false;
}
break;
case EXPONENT_STEP:
if (str[i] == '+' )
{
exp_sign = 1;
check_step++;
exponent.push_back('0');
}
else if (str[i] == '-' )
{
exp_sign = -1;
check_step++;
exponent.push_back('0');
}
else if (str[i] >= '0' && str[i] <= '9' )
{
exp_sign = 1;
check_step++;
exponent.push_back(str[i]);
}
else
{
ok = false;
}
break;
case (EXPONENT_STEP + 1):
if (str[i] >= '0' && str[i] <= '9' )
{
exponent.push_back(str[i]);
}
else
{
ok = false;
}
break;
}
}
if ( ok ) //Conversion
{
if (mantissa.length()) mantissa_num = sign * atof(mantissa.c_str());
else mantissa_num = 1;
if (exponent.length()) exp_num = atoi(exponent.c_str());
else exp_num = 0;
result = mantissa_num;
for (int i=0; i<exp_num; i++)
{
(exp_sign > 0) ? result *= 10 : result /= 10;
}
}
if (conv_ok) *conv_ok = ok;
return result;
}
double ArtString::toDouble(bool * conv_ok )
{
return toDouble(*this, conv_ok);
}