-
Notifications
You must be signed in to change notification settings - Fork 84
/
cpp11addition.cpp
executable file
·687 lines (618 loc) · 17.8 KB
/
cpp11addition.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#include "cpp11addition.h"
#include <sstream>
#include <cassert>
#include <stdlib.h>
#include <limits.h>
#include <stdlib.h>
#include <iostream>
#include <chrono>
#include <cstring>
/// \todo, check number validity by http://en.cppreference.com/w/c/string/byte/strtol
/* to check: bool my_strtol(const std::string &str, long &v) {
char *end = nullptr;
v = strtol(str.c_str(), &end, 10);
return end != nullptr;
}*/
static const std::regex ishexa("^([0-9a-fA-F][0-9a-fA-F])+$",std::regex::optimize);
#if defined(_WIN32) || defined(CATCHCHALLENGER_EXTRA_CHECK)
static const std::regex regexseparators("[/\\\\]+",std::regex::optimize);
#endif
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
static const char* const lut = "0123456789ABCDEF";
std::size_t pairhash::operator()(const std::pair<uint8_t, uint8_t> &x) const
{
return (x.first << 8) + x.second;
}
std::size_t pairhash::operator()(const std::pair<uint16_t, uint16_t> &x) const
{
return (x.first << 16) + x.second;
}
std::string ltrim(const std::string& str)
{
return std::regex_replace(str, std::regex("^ +"), "");
}
std::string rtrim(const std::string& str)
{
return std::regex_replace(str, std::regex(" +$"), "");
}
std::string trim(const std::string& str)
{
return std::regex_replace(str, std::regex("^ +| +$"), "");
}
bool stringreplaceOne(std::string& str, const std::string& from, const std::string& to)
{
const size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
uint8_t stringreplaceAll(std::string& str, const std::string& from, const std::string& to)
{
if(from.empty())
return 0;
size_t start_pos = 0;
uint8_t count=0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
count++;
}
return count;
}
std::vector<std::string> stringregexsplit(const std::string& input, const std::regex& regex)
{
// passing -1 as the submatch index parameter performs splitting
std::sregex_token_iterator
first{input.begin(), input.end(), regex, -1},
last;
return {first, last};
}
std::vector<std::string> stringsplit(const std::string &s, char delim)
{
std::vector<std::string> elems;
std::string::size_type i = 0;
std::string::size_type j = s.find(delim);
if(j == std::string::npos)
{
if(!s.empty())
elems.push_back(s);
return elems;
}
else
{
while (j != std::string::npos) {
elems.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(delim, j);
if (j == std::string::npos)
elems.push_back(s.substr(i, s.length()));
}
return elems;
}
}
bool stringEndsWith(std::string const &fullString, std::string const &ending)
{
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
bool stringEndsWith(std::string const &fullString, char const &ending)
{
if (fullString.length()>0) {
return fullString[fullString.size()-1]==ending;
} else {
return false;
}
}
bool stringStartWith(std::string const &fullString, std::string const &starting)
{
if (fullString.length() >= starting.length()) {
return (fullString.substr(0,starting.length())==starting);
} else {
return false;
}
}
bool stringStartWith(std::string const &fullString, char const &starting)
{
if (fullString.length()>0) {
return fullString[0]==starting;
} else {
return false;
}
}
std::string& stringimplode(const std::vector<std::string>& elems, char delim, std::string& s)
{
for (std::vector<std::string>::const_iterator ii = elems.begin(); ii != elems.cend(); ++ii)
{
s += (*ii);
if ( ii + 1 != elems.end() ) {
s += delim;
}
}
return s;
}
std::string stringimplode(const std::vector<std::string>& elems, char delim)
{
std::string s;
return stringimplode(elems, delim, s);
}
std::string stringimplode(const std::queue<std::string>& elems, char delim)
{
std::string newString;
std::queue<std::string> copy=elems;
unsigned int count=0;
while(!copy.empty())
{
if(count>0)
newString+=delim;
newString+=copy.front();
copy.pop();
++count;
}
return newString;
}
std::string stringimplode(const std::vector<std::string>& elems, const std::string &delim)
{
std::string newString;
for (std::vector<std::string>::const_iterator ii = elems.begin(); ii != elems.cend(); ++ii)
{
newString += (*ii);
if ( ii + 1 != elems.end() ) {
newString += delim;
}
}
return newString;
}
std::string binarytoHexa(const std::vector<char> &data, bool *ok)
{
if(ok!=NULL)
*ok=true;
std::string output;
output.reserve(2*data.size());
for(size_t i=0;i<data.size();++i)
{
const unsigned char c = data[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
std::string binarytoHexa(const void * const data, const uint32_t &size, bool *ok)
{
return binarytoHexa(reinterpret_cast<const char *>(data),size,ok);
}
std::string binarytoHexa(const char * const data, const uint32_t &size, bool *ok)
{
if(size==0)
return std::string();
#ifdef CATCHCHALLENGER_EXTRA_CHECK
if(__builtin_expect((size>100000000),0))
{
std::cerr << "cpp11addition binarytoHexa() size>100000000, seam be a bug, dropped to empty string" << std::endl;
return std::string();
}
#endif
if(ok!=NULL)
*ok=true;
std::string output;
output.reserve(2*size);
for(size_t i=0;i<size;++i)
{
const unsigned char c = data[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
uint8_t hexToDecUnit(const std::string& data, bool *ok)
{
auto fromHex = [](char c, bool *ok)
{
if(ok!=NULL)
*ok=true;
if (isdigit(c)) return c - '0';
switch(c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
}
#ifdef CATCHCHALLENGER_EXTRA_CHECK
std::cerr << "Convertion failed and repported at " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
if(ok!=NULL)
*ok=false;
return 0;
};
return fromHex(data[0],ok) << 4 | fromHex(data[1],ok);
}
std::vector<char> hexatoBinary(const std::string &data,bool *ok)
{
if(data.size()%2!=0)
{
#ifdef CATCHCHALLENGER_EXTRA_CHECK
std::cerr << "Convertion failed and repported at " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
if(ok!=NULL)
*ok=false;
return std::vector<char>();
}
if(Q_LIKELY(std::regex_match(data,ishexa)))
{
bool ok2;
std::vector<char> out;
out.reserve(data.length()/2);
for(size_t i=0;i<data.length();i+=2)
{
const std::string &partpfchain=data.substr(i,2);
const uint8_t &x=hexToDecUnit(partpfchain,&ok2);
if(!ok2)
{
#ifdef CATCHCHALLENGER_EXTRA_CHECK
std::cerr << "Convertion failed and repported at " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
if(ok!=NULL)
*ok=false;
return std::vector<char>();
}
out.push_back(x);
}
if(ok!=NULL)
*ok=true;
return out;
}
else
{
#ifdef CATCHCHALLENGER_EXTRA_CHECK
std::cerr << "Convertion failed and repported at " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
if(ok!=NULL)
*ok=false;
return std::vector<char>();
}
}
void binaryAppend(std::vector<char> &data,const std::vector<char> &add)
{
if(add.empty())
return;
if(data.empty())
{
data=add;
return;
}
const int oldsize=data.size();
data.resize(oldsize+add.size());
memcpy(data.data()+oldsize,add.data(),add.size());
}
void binaryAppend(std::vector<char> &data,const char * const add,const uint32_t &addSize)
{
if(addSize==0)
return;
if(data.empty())
{
data.resize(addSize);
memcpy(data.data(),add,addSize);
return;
}
const int oldsize=data.size();
data.resize(oldsize+addSize);
memcpy(data.data()+oldsize,add,addSize);
}
std::vector<char> base64toBinary(const std::string &string)
{
int index=0;
int sub_index=0;
int encoded_string_remaining=string.size();
int encoded_string_pos=0;
unsigned char char_array_4[4], char_array_3[3];
std::vector<char> ret;
while(encoded_string_remaining-- && (string[encoded_string_pos]!='=') && is_base64(string[encoded_string_pos]))
{
char_array_4[index++]=string[encoded_string_pos];
encoded_string_pos++;
if(index==4)
{
for(index=0;index<4;index++)
char_array_4[index]=base64_chars.find(char_array_4[index]);
char_array_3[0]=(char_array_4[0]<<2) + ((char_array_4[1]&0x30)>>4);
char_array_3[1]=((char_array_4[1]&0xf)<<4) + ((char_array_4[2]&0x3c)>>2);
char_array_3[2]=((char_array_4[2]&0x3)<<6) + char_array_4[3];
for(index=0;(index<3);index++)
ret.push_back(char_array_3[index]);
index=0;
}
}
if(index)
{
for(sub_index=index;sub_index<4;sub_index++)
char_array_4[sub_index]=0;
for(sub_index=0;sub_index<4;sub_index++)
char_array_4[sub_index]=base64_chars.find(char_array_4[sub_index]);
char_array_3[0]=(char_array_4[0]<<2) + ((char_array_4[1]&0x30)>>4);
char_array_3[1]=((char_array_4[1]&0xf)<<4) + ((char_array_4[2]&0x3c)>>2);
char_array_3[2]=((char_array_4[2]&0x3)<<6) + char_array_4[3];
for (sub_index=0;(sub_index<index-1);sub_index++)
ret.push_back(char_array_3[sub_index]);
}
return ret;
}
std::string FSabsoluteFilePath(const std::string &string)
{
std::string newstring=string;
stringreplaceAll(newstring,"//","/");
#ifdef _WIN32
stringreplaceAll(newstring,"\\\\","\\");
std::vector<std::string> parts=stringregexsplit(newstring,regexseparators);
#else
std::vector<std::string> parts=stringsplit(newstring,'/');
#endif
#ifndef _WIN32
unsigned int index=1;
#else
unsigned int index=2;
#endif
while(index<parts.size())
{
if(parts.at(index)=="..")
{
parts.erase(parts.begin()+index);
#ifndef _WIN32
if(index>0 && (index>1 || !parts.at(index-1).empty()))
#else
if(index>1)
#endif
{
parts.erase(parts.begin()+index-1);
index--;
}
}
else
index++;
}
#ifndef _WIN32
if(parts.empty() || (parts.size()==1 && parts.at(0).empty()))
return "/";
#endif
return stringimplode(parts,'/');
}
std::string FSabsolutePath(const std::string &string)
{
const std::string &tempFile=FSabsoluteFilePath(string);
const std::size_t &found=tempFile.find_last_of("/\\");
if(found!=std::string::npos)
return tempFile.substr(0,found)+'/';
else
return tempFile;
}
std::wstring FSabsoluteFilePath(const std::wstring &string)
{
std::wstring newstring=string;
#ifdef _WIN32
stringreplaceAll(newstring,L"\\",L"/");
#endif
stringreplaceAll(newstring,L"//",L"/");
std::vector<std::wstring> parts=stringsplit(newstring,'/');
#ifndef _WIN32
unsigned int index=1;
#else
unsigned int index=2;
#endif
while(index<parts.size())
{
if(parts.at(index)==L"..")
{
parts.erase(parts.begin()+index);
#ifndef _WIN32
if(index>0 && (index>1 || !parts.at(index-1).empty()))
#else
if(index>1)
#endif
{
parts.erase(parts.begin()+index-1);
index--;
}
}
else
index++;
}
#ifndef _WIN32
if(parts.empty() || (parts.size()==1 && parts.at(0).empty()))
return L"/";
#endif
std::wstring newString;
std::vector<std::wstring> copy=parts;
unsigned int count=0;
while(!copy.empty())
{
if(count>0)
newString+=L'/';
newString+=copy.front();
copy.erase(copy.begin());
++count;
}
return newString;
}
std::wstring FSabsolutePath(const std::wstring &string)
{
const std::wstring &tempFile=FSabsoluteFilePath(string);
const std::size_t &found=tempFile.find_last_of(L"/\\");
if(found!=std::wstring::npos)
return tempFile.substr(0,found)+L'/';
else
return tempFile;
}
uint64_t msFrom1970()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
uint64_t sFrom1970()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()/1000;
}
std::string booltostring(const bool &value)
{
if(value)
return "true";
else
return "false";
}
std::vector<std::string> stringtostringlist(const std::string &string)
{
if(string.empty())
return std::vector<std::string>();
std::vector<std::string> returnedVar;
size_t start_pos = 0;
size_t firstChar = 0;
do
{
size_t s=start_pos = string.find(',', start_pos);
if(s == std::string::npos)
start_pos=string.size();
if(start_pos>=string.size() || string.at(start_pos+1)!=',')
{
std::string tempString=string.substr(firstChar,start_pos-firstChar);
stringreplaceAll(tempString,",,",",");
returnedVar.push_back(tempString);
start_pos++;
firstChar=start_pos;
}
else
start_pos+=2;
} while(firstChar<start_pos || start_pos<string.size());
return returnedVar;
}
std::string stringlisttostring(const std::vector<std::string> &stringlist)
{
std::string returnedString;
unsigned int index=0;
while(index<stringlist.size())
{
if(!returnedString.empty())
returnedString+=',';
std::string tempString=stringlist.at(index);
stringreplaceAll(tempString,",",",,");
returnedString+=tempString;
index++;
}
return returnedString;
}
bool stringreplaceOne(std::wstring& str, const std::wstring& from, const std::wstring& to)
{
const size_t start_pos = str.find(from);
if(start_pos == std::wstring::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
uint8_t stringreplaceAll(std::wstring& str, const std::wstring& from, const std::wstring& to)
{
if(from.empty())
return 0;
size_t start_pos = 0;
uint8_t count=0;
while((start_pos = str.find(from, start_pos)) != std::wstring::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
count++;
}
return count;
}
bool stringEndsWith(std::wstring const &fullString, std::wstring const &ending)
{
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
bool stringEndsWith(std::wstring const &fullString, char const &ending)
{
if (fullString.length()>0) {
return fullString[fullString.size()-1]==ending;
} else {
return false;
}
}
bool stringStartWith(std::wstring const &fullString, std::wstring const &starting)
{
if (fullString.length() >= starting.length()) {
return (fullString.substr(0,starting.length())==starting);
} else {
return false;
}
}
bool stringStartWith(std::wstring const &fullString, char const &starting)
{
if (fullString.length()>0) {
return fullString[0]==starting;
} else {
return false;
}
}
std::vector<std::wstring> stringsplit(const std::wstring &s, wchar_t delim)
{
std::vector<std::wstring> elems;
std::wstring::size_type i = 0;
std::wstring::size_type j = s.find(delim);
if(j == std::wstring::npos)
{
if(!s.empty())
elems.push_back(s);
return elems;
}
else
{
while (j != std::wstring::npos) {
elems.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(delim, j);
if (j == std::wstring::npos)
elems.push_back(s.substr(i, s.length()));
}
return elems;
}
}