-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringf.cpp
More file actions
936 lines (850 loc) · 43.4 KB
/
stringf.cpp
File metadata and controls
936 lines (850 loc) · 43.4 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
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
#include "stringf.hpp"
//#include "ssl.hpp"
#include "regular.hpp"
//#include "zip.hpp"
#include <stdarg.h>
#include <algorithm>
#include <fstream>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
#if BOOST_OS_WINDOWS
#include <Stringapiset.h>
#endif
//#include <locale>
//extern "C" {
// int compare_keywords(const void *a, const void *b) {
// //dbgv(*((const char **)b));//,*((const char **)b) );
// return (strcmp(*((const char **)a), *((const char **)b)));
// }
//}
using namespace boost;
std::string tail(std::string const& source, size_t const length) {
if (length >= source.size()) { return source; }
return source.substr(source.size() - length);
} // tail
void jsndeletearrayidx(jsn &jsna,int index){
jsn jsntemp=jsn::array();
for (int i = 0; i < jsna.as_array().size(); i++){
if(i!=index)jsntemp.push_back(jsna[i]);
}
jsna=jsntemp;
}
///jsnaddobject(mmc)["symb"]="test3";
jsn jsnaddobject(jsn &jsna){
jsn mmco=jsn::object();;
// mmco["symb"]="teste2";
jsna.as_array().push_back(mmco);
return mmco;
}
string fjson(string ss) {
string ns;
bool p=1,pn;
for ( int i=0;i<ss.size();i++ ) {
pn= ( ss[i] == ':' || ss[i] == ',' || ss[i] == '{' || ss[i]== '}' ||ss[i] == '[' ||ss[i] == ']' ||ss[i] == ':' ||ss[i] == ' ' );
if ((p && !pn) || !p && pn ){ ns+='\"';}
ns+=ss[i];
p=pn;
}
return string(ns);
}
// The main function that checks if two given strings
// match. The first string may contain wildcard characters
bool stringMatch(const char *first,const char * second) {
// If we reach at the end of both strings, we are done
if (*first == '\0' && *second == '\0')
return true;
// Make sure that the characters after '*' are present
// in second string. This function assumes that the first
// string will not contain two consecutive '*'
if (*first == '*' && *(first+1) != '\0' && *second == '\0')
return false;
// If the first string contains '?', or current characters
// of both strings match
if (*first == '?' || *first == *second)
return stringMatch(first+1, second+1);
// If there is *, then there are two possibilities
// a) We consider current character of second string
// b) We ignore current character of second string.
if (*first == '*')
return stringMatch(first+1, second) || stringMatch(first, second+1);
return false;
}
int removeChar(char *s, char c, int len) {
int newlen = len;
for ( char *s2=s; len>=0; s2++, len-- ) {
if ( *s2 == c ) --newlen; else *s++ = *s2;
}
return newlen;
}
FILE* fmemopen_ (void *buf, size_t size, const char *opentype){
FILE *f;
//assert(strcmp(opentype, "r") == 0);
f = tmpfile();
fwrite(buf, 1, size, f);
rewind(f);
return f;
}
#if BOOST_OS_WINDOWS
FILE* fmemopen (void *buf, size_t size, const char *opentype){
FILE *f;
//assert(strcmp(opentype, "r") == 0);
char* tmp_fname = _tempnam("%TMP%", "fmemopen");
f = fopen(tmp_fname, "wt");
fwrite(buf, 1, size, f);
fclose(f);
f = fopen(tmp_fname, "rt");
return f;
}
char* stringConv(const char* szU8,uint cpfrom,uint cpto){
int wcsLen = ::MultiByteToWideChar(cpfrom, NULL, szU8, strlen(szU8), NULL, 0);
wchar_t* wszString = new wchar_t[wcsLen + 1];
::MultiByteToWideChar(cpfrom, NULL, szU8, strlen(szU8), wszString, wcsLen);
wszString[wcsLen] = '\0';
int ansiLen = ::WideCharToMultiByte(cpto, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);
char* szAnsi = new char[ansiLen + 1];
::WideCharToMultiByte(cpto, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);
szAnsi[ansiLen] = '\0';
return szAnsi;
}
#endif
void trim(string &str,string what){
// str+=" ";
str.erase(0, str.find_first_not_of(what));
str.erase(str.find_last_not_of(what)+1);
}
void triml(string &str,string what){
str.erase(0, str.find_first_not_of(what));
}
void trimr(string &str,string what){
str.erase(str.find_last_not_of(what)+1);
}
int isComment(string &buffline){
int ret=-1;
lop(c,0,buffline.size()){
if(buffline[c]=='\t' || buffline[c]==' ')continue;
if(buffline.substr(c,2)=="//")ret=c;
}
return ret;
};
bool contains(string container,string containthis,bool matchcase){ if(!matchcase){container=toLower(container);containthis=toLower(containthis);} if (string(container).find(containthis)!=string::npos)return true; return false;}
bool containsAny(string container,vector <string> containAnyOfthis,bool matchcase){
for(int i=0;i<containAnyOfthis.size();i++)if(contains(container,containAnyOfthis[i],matchcase))return true;
return false;
}
vector<int> stringPositions(char * pbTarget, char * pbPattern, uint32_t cbTarget, uint32_t cbPattern){
vector<int> res;
char* stoken=pbTarget;
uint32_t nlen=cbTarget;
uint32_t dist;
while(stoken!=NULL){
char* stokenp=stoken;
stoken=Railgun_Swampshine_BailOut (stokenp, pbPattern, nlen, cbPattern);
if(stoken!=NULL){
int dist=distance(pbTarget,stoken);
nlen=cbTarget-dist-cbPattern;
stoken+=cbPattern;
res.push_back(dist);
}
}
return res;
};
#define NeedleThreshold2vs4swampLITE 9+10 // Should be bigger than 9. BMH2 works up to this value (inclusive), if bigger then BMH4 takes over.
char * Railgun_Swampshine_BailOut (char * pbTarget, char * pbPattern, uint32_t cbTarget, uint32_t cbPattern){
char * pbTargetMax = pbTarget + cbTarget;
register uint32_t ulHashPattern;
signed long count;
unsigned char bm_Horspool_Order2[256*256]; // Bitwise soon...
uint32_t i, Gulliver;
uint32_t PRIMALposition, PRIMALpositionCANDIDATE;
uint32_t PRIMALlength, PRIMALlengthCANDIDATE;
uint32_t j, FoundAtPosition;
if (cbPattern > cbTarget) return(NULL);
if ( cbPattern<4 ) {
// SSE2 i.e. 128bit Assembly rules here:
// ...
pbTarget = pbTarget+cbPattern;
ulHashPattern = ( (*(char *)(pbPattern))<<8 ) + *(pbPattern+(cbPattern-1));
if ( cbPattern==3 ) {
for ( ;; ) {
if ( ulHashPattern == ( (*(char *)(pbTarget-3))<<8 ) + *(pbTarget-1) ) {
if ( *(char *)(pbPattern+1) == *(char *)(pbTarget-2) ) return((pbTarget-3));
}
if ( (char)(ulHashPattern>>8) != *(pbTarget-2) ) {
pbTarget++;
if ( (char)(ulHashPattern>>8) != *(pbTarget-2) ) pbTarget++;
}
pbTarget++;
if (pbTarget > pbTargetMax) return(NULL);
}
} else {
}
for ( ;; ) {
if ( ulHashPattern == ( (*(char *)(pbTarget-2))<<8 ) + *(pbTarget-1) ) return((pbTarget-2));
if ( (char)(ulHashPattern>>8) != *(pbTarget-1) ) pbTarget++;
pbTarget++;
if (pbTarget > pbTargetMax) return(NULL);
}
} else { //if ( cbPattern<4 )
if ( cbPattern<=NeedleThreshold2vs4swampLITE ) {
// BMH order 2, needle should be >=4:
ulHashPattern = *(uint32_t *)(pbPattern); // First four bytes
for (i=0; i < 256*256; i++) {bm_Horspool_Order2[i]=0;}
for (i=0; i < cbPattern-1; i++) bm_Horspool_Order2[*(unsigned short *)(pbPattern+i)]=1;
i=0;
while (i <= cbTarget-cbPattern) {
Gulliver = 1; // 'Gulliver' is the skip
if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+cbPattern-1-1]] != 0 ) {
if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+cbPattern-1-1-2]] == 0 ) Gulliver = cbPattern-(2-1)-2; else {
if ( *(uint32_t *)&pbTarget[i] == ulHashPattern) { // This fast check ensures not missing a match (for remainder) when going under 0 in loop below:
count = cbPattern-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern+count-1) == *(uint32_t *)(&pbTarget[i]+(count-1)) )
count = count-4;
if ( count <= 0 ) return(pbTarget+i);
}
}
} else Gulliver = cbPattern-(2-1);
i = i + Gulliver;
//GlobalI++; // Comment it, it is only for stats.
}
return(NULL);
} else { // if ( cbPattern<=NeedleThreshold2vs4swampLITE )
// Swampwalker_BAILOUT heuristic order 4 (Needle should be bigger than 4) [
// Needle: 1234567890qwertyuiopasdfghjklzxcv PRIMALposition=01 PRIMALlength=33 '1234567890qwertyuiopasdfghjklzxcv'
// Needle: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv PRIMALposition=29 PRIMALlength=04 'vvvv'
// Needle: vvvvvvvvvvBOOMSHAKALAKAvvvvvvvvvv PRIMALposition=08 PRIMALlength=20 'vvvBOOMSHAKALAKAvvvv'
// Needle: Trollland PRIMALposition=01 PRIMALlength=09 'Trollland'
// Needle: Swampwalker PRIMALposition=01 PRIMALlength=11 'Swampwalker'
// Needle: licenselessness PRIMALposition=01 PRIMALlength=15 'licenselessness'
// Needle: alfalfa PRIMALposition=02 PRIMALlength=06 'lfalfa'
// Needle: Sandokan PRIMALposition=01 PRIMALlength=08 'Sandokan'
// Needle: shazamish PRIMALposition=01 PRIMALlength=09 'shazamish'
// Needle: Simplicius Simplicissimus PRIMALposition=06 PRIMALlength=20 'icius Simplicissimus'
// Needle: domilliaquadringenquattuorquinquagintillion PRIMALposition=01 PRIMALlength=32 'domilliaquadringenquattuorquinqu'
// Needle: boom-boom PRIMALposition=02 PRIMALlength=08 'oom-boom'
// Needle: vvvvv PRIMALposition=01 PRIMALlength=04 'vvvv'
// Needle: 12345 PRIMALposition=01 PRIMALlength=05 '12345'
// Needle: likey-likey PRIMALposition=03 PRIMALlength=09 'key-likey'
// Needle: BOOOOOM PRIMALposition=03 PRIMALlength=05 'OOOOM'
// Needle: aaaaaBOOOOOM PRIMALposition=02 PRIMALlength=09 'aaaaBOOOO'
// Needle: BOOOOOMaaaaa PRIMALposition=03 PRIMALlength=09 'OOOOMaaaa'
PRIMALlength=0;
for (i=0+(1); i < cbPattern-((4)-1)+(1)-(1); i++) { // -(1) because the last BB order 4 has no counterpart(s)
FoundAtPosition = cbPattern - ((4)-1) + 1;
PRIMALpositionCANDIDATE=i;
while ( PRIMALpositionCANDIDATE <= (FoundAtPosition-1) ) {
j = PRIMALpositionCANDIDATE + 1;
while ( j <= (FoundAtPosition-1) ) {
if ( *(uint32_t *)(pbPattern+PRIMALpositionCANDIDATE-(1)) == *(uint32_t *)(pbPattern+j-(1)) ) FoundAtPosition = j;
j++;
}
PRIMALpositionCANDIDATE++;
}
PRIMALlengthCANDIDATE = (FoundAtPosition-1)-i+1 +((4)-1);
if (PRIMALlengthCANDIDATE >= PRIMALlength) {PRIMALposition=i; PRIMALlength = PRIMALlengthCANDIDATE;}
if (cbPattern-i+1 <= PRIMALlength) break;
if (PRIMALlength > 128) break; // Bail Out for 129[+]
}
// Swampwalker_BAILOUT heuristic order 4 (Needle should be bigger than 4) ]
// Here we have 4 or bigger NewNeedle, apply order 2 for pbPattern[i+(PRIMALposition-1)] with length 'PRIMALlength' and compare the pbPattern[i] with length 'cbPattern':
PRIMALlengthCANDIDATE = cbPattern;
cbPattern = PRIMALlength;
pbPattern = pbPattern + (PRIMALposition-1);
// Revision 2 commented section [
/*
if (cbPattern-1 <= 255) {
// BMH Order 2 [
ulHashPattern = *(uint32_t *)(pbPattern); // First four bytes
for (i=0; i < 256*256; i++) {bm_Horspool_Order2[i]= cbPattern-1;} // cbPattern-(Order-1) for Horspool; 'memset' if not optimized
for (i=0; i < cbPattern-1; i++) bm_Horspool_Order2[*(unsigned short *)(pbPattern+i)]=i; // Rightmost appearance/position is needed
i=0;
while (i <= cbTarget-cbPattern) {
Gulliver = bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+cbPattern-1-1]];
if ( Gulliver != cbPattern-1 ) { // CASE #2: if equal means the pair (char order 2) is not found i.e. Gulliver remains intact, skip the whole pattern and fall back (Order-1) chars i.e. one char for Order 2
if ( Gulliver == cbPattern-2 ) { // CASE #1: means the pair (char order 2) is found
if ( *(uint32_t *)&pbTarget[i] == ulHashPattern) {
count = cbPattern-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern+count-1) == *(uint32_t *)(&pbTarget[i]+(count-1)) )
count = count-4;
// If we miss to hit then no need to compare the original: Needle
if ( count <= 0 ) {
// I have to add out-of-range checks...
// i-(PRIMALposition-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4
// FIX from 2014-Apr-27:
// Because (count-1) is negative, above fours are reduced to next twos:
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// The line below is BUGGY:
//if ( (i-(PRIMALposition-1) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) && (&pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4) ) {
// The line below is OKAY:
if ( ((signed int)(i-(PRIMALposition-1)+(count-1)) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) ) {
if ( *(uint32_t *)&pbTarget[i-(PRIMALposition-1)] == *(uint32_t *)(pbPattern-(PRIMALposition-1))) { // This fast check ensures not missing a match (for remainder) when going under 0 in loop below:
count = PRIMALlengthCANDIDATE-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern-(PRIMALposition-1)+count-1) == *(uint32_t *)(&pbTarget[i-(PRIMALposition-1)]+(count-1)) )
count = count-4;
if ( count <= 0 ) return(pbTarget+i-(PRIMALposition-1));
}
}
}
}
Gulliver = 1;
} else
Gulliver = cbPattern - Gulliver - 2; // CASE #3: the pair is found and not as suffix i.e. rightmost position
}
i = i + Gulliver;
//GlobalI++; // Comment it, it is only for stats.
}
return(NULL);
// BMH Order 2 ]
} else {
// BMH order 2, needle should be >=4:
ulHashPattern = *(uint32_t *)(pbPattern); // First four bytes
for (i=0; i < 256*256; i++) {bm_Horspool_Order2[i]=0;}
for (i=0; i < cbPattern-1; i++) bm_Horspool_Order2[*(unsigned short *)(pbPattern+i)]=1;
i=0;
while (i <= cbTarget-cbPattern) {
Gulliver = 1; // 'Gulliver' is the skip
if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+cbPattern-1-1]] != 0 ) {
if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+cbPattern-1-1-2]] == 0 ) Gulliver = cbPattern-(2-1)-2; else {
if ( *(uint32_t *)&pbTarget[i] == ulHashPattern) { // This fast check ensures not missing a match (for remainder) when going under 0 in loop below:
count = cbPattern-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern+count-1) == *(uint32_t *)(&pbTarget[i]+(count-1)) )
count = count-4;
// If we miss to hit then no need to compare the original: Needle
if ( count <= 0 ) {
// I have to add out-of-range checks...
// i-(PRIMALposition-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4
// FIX from 2014-Apr-27:
// Because (count-1) is negative, above fours are reduced to next twos:
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// The line below is BUGGY:
//if ( (i-(PRIMALposition-1) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) && (&pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4) ) {
// The line below is OKAY:
if ( ((signed int)(i-(PRIMALposition-1)+(count-1)) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) ) {
if ( *(uint32_t *)&pbTarget[i-(PRIMALposition-1)] == *(uint32_t *)(pbPattern-(PRIMALposition-1))) { // This fast check ensures not missing a match (for remainder) when going under 0 in loop below:
count = PRIMALlengthCANDIDATE-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern-(PRIMALposition-1)+count-1) == *(uint32_t *)(&pbTarget[i-(PRIMALposition-1)]+(count-1)) )
count = count-4;
if ( count <= 0 ) return(pbTarget+i-(PRIMALposition-1));
}
}
}
}
}
} else Gulliver = cbPattern-(2-1);
i = i + Gulliver;
//GlobalI++; // Comment it, it is only for stats.
}
return(NULL);
}
*/
// Revision 2 commented section ]
if ( cbPattern<=NeedleThreshold2vs4swampLITE ) {
// BMH order 2, needle should be >=4:
ulHashPattern = *(uint32_t *)(pbPattern); // First four bytes
for (i=0; i < 256*256; i++) {bm_Horspool_Order2[i]=0;}
for (i=0; i < cbPattern-1; i++) bm_Horspool_Order2[*(unsigned short *)(pbPattern+i)]=1;
i=0;
while (i <= cbTarget-cbPattern) {
Gulliver = 1; // 'Gulliver' is the skip
if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+cbPattern-1-1]] != 0 ) {
if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+cbPattern-1-1-2]] == 0 ) Gulliver = cbPattern-(2-1)-2; else {
if ( *(uint32_t *)&pbTarget[i] == ulHashPattern) { // This fast check ensures not missing a match (for remainder) when going under 0 in loop below:
count = cbPattern-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern+count-1) == *(uint32_t *)(&pbTarget[i]+(count-1)) )
count = count-4;
// If we miss to hit then no need to compare the original: Needle
if ( count <= 0 ) {
// I have to add out-of-range checks...
// i-(PRIMALposition-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4
// FIX from 2014-Apr-27:
// Because (count-1) is negative, above fours are reduced to next twos:
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// The line below is BUGGY:
//if ( (i-(PRIMALposition-1) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) && (&pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4) ) {
// The line below is OKAY:
if ( ((signed int)(i-(PRIMALposition-1)+(count-1)) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) ) {
if ( *(uint32_t *)&pbTarget[i-(PRIMALposition-1)] == *(uint32_t *)(pbPattern-(PRIMALposition-1))) { // This fast check ensures not missing a match (for remainder) when going under 0 in loop below:
count = PRIMALlengthCANDIDATE-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern-(PRIMALposition-1)+count-1) == *(uint32_t *)(&pbTarget[i-(PRIMALposition-1)]+(count-1)) )
count = count-4;
if ( count <= 0 ) return(pbTarget+i-(PRIMALposition-1));
}
}
}
}
}
} else Gulliver = cbPattern-(2-1);
i = i + Gulliver;
//GlobalI++; // Comment it, it is only for stats.
}
return(NULL);
} else { // if ( cbPattern<=NeedleThreshold2vs4swampLITE )
// BMH pseudo-order 4, needle should be >=8+2:
ulHashPattern = *(uint32_t *)(pbPattern); // First four bytes
for (i=0; i < 256*256; i++) {bm_Horspool_Order2[i]=0;}
// In line below we "hash" 4bytes to 2bytes i.e. 16bit table, how to compute TOTAL number of BBs, 'cbPattern - Order + 1' is the number of BBs for text 'cbPattern' bytes long, for example, for cbPattern=11 'fastest fox' and Order=4 we have BBs = 11-4+1=8:
//"fast"
//"aste"
//"stes"
//"test"
//"est "
//"st f"
//"t fo"
//" fox"
//for (i=0; i < cbPattern-4+1; i++) bm_Horspool_Order2[( *(unsigned short *)(pbPattern+i+0) + *(unsigned short *)(pbPattern+i+2) ) & ( (1<<16)-1 )]=1;
//for (i=0; i < cbPattern-4+1; i++) bm_Horspool_Order2[( (*(uint32_t *)(pbPattern+i+0)>>16)+(*(uint32_t *)(pbPattern+i+0)&0xFFFF) ) & ( (1<<16)-1 )]=1;
// Above line is replaced by next one with better hashing:
for (i=0; i < cbPattern-4+1; i++) bm_Horspool_Order2[( (*(uint32_t *)(pbPattern+i+0)>>(16-1))+(*(uint32_t *)(pbPattern+i+0)&0xFFFF) ) & ( (1<<16)-1 )]=1;
i=0;
while (i <= cbTarget-cbPattern) {
Gulliver = 1;
//if ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2]>>16)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2]&0xFFFF) ) & ( (1<<16)-1 )] != 0 ) { // DWORD #1
// Above line is replaced by next one with better hashing:
if ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2]>>(16-1))+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2]&0xFFFF) ) & ( (1<<16)-1 )] != 0 ) { // DWORD #1
//if ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]>>16)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) ) & ( (1<<16)-1 )] == 0 ) Gulliver = cbPattern-(2-1)-2-4; else {
// Above line is replaced in order to strengthen the skip by checking the middle DWORD,if the two DWORDs are 'ab' and 'cd' i.e. [2x][2a][2b][2c][2d] then the middle DWORD is 'bc'.
// The respective offsets (backwards) are: -10/-8/-6/-4 for 'xa'/'ab'/'bc'/'cd'.
//if ( ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]>>16)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]&0xFFFF) ) & ( (1<<16)-1 )] ) + ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]>>16)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) ) & ( (1<<16)-1 )] ) + ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]>>16)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]&0xFFFF) ) & ( (1<<16)-1 )] ) < 3 ) Gulliver = cbPattern-(2-1)-2-4-2; else {
// Above line is replaced by next one with better hashing:
// When using (16-1) right shifting instead of 16 we will have two different pairs (if they are equal), the highest bit being lost do the job especialy for ASCII texts with no symbols in range 128-255.
// Example for genomesque pair TT+TT being shifted by (16-1):
// T = 01010100
// TT = 01010100 01010100
// TTTT = 01010100 01010100 01010100 01010100
// TTTT>>16 = 00000000 00000000 01010100 01010100
// TTTT>>(16-1) = 00000000 00000000 10101000 10101000 <--- Due to the left shift by 1, the 8th bits of 1st and 2nd bytes are populated - usually they are 0 for English texts & 'ACGT' data.
//if ( ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]>>(16-1))+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]&0xFFFF) ) & ( (1<<16)-1 )] ) + ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]>>(16-1))+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) ) & ( (1<<16)-1 )] ) + ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]>>(16-1))+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]&0xFFFF) ) & ( (1<<16)-1 )] ) < 3 ) Gulliver = cbPattern-(2-1)-2-4-2; else {
// 'Maximus' uses branched 'if', again.
if ( \
( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6 +1]>>(16-1))+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6 +1]&0xFFFF) ) & ( (1<<16)-1 )] ) == 0 \
|| ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4 +1]>>(16-1))+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4 +1]&0xFFFF) ) & ( (1<<16)-1 )] ) == 0 \
) Gulliver = cbPattern-(2-1)-2-4-2 +1; else {
// Above line is not optimized (several a SHR are used), we have 5 non-overlapping WORDs, or 3 overlapping WORDs, within 4 overlapping DWORDs so:
// [2x][2a][2b][2c][2d]
// DWORD #4
// [2a] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]>>16) = !SHR to be avoided! <--
// [2x] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]&0xFFFF) = |
// DWORD #3 |
// [2b] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]>>16) = !SHR to be avoided! |<--
// [2a] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) = ------------------------ |
// DWORD #2 |
// [2c] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]>>16) = !SHR to be avoided! |<--
// [2b] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]&0xFFFF) = --------------------------- |
// DWORD #1 |
// [2d] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-0]>>16) = |
// [2c] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-0]&0xFFFF) = ------------------------------
//
// So in order to remove 3 SHR instructions the equal extractions are:
// DWORD #4
// [2a] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) = !SHR to be avoided! <--
// [2x] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]&0xFFFF) = |
// DWORD #3 |
// [2b] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]&0xFFFF) = !SHR to be avoided! |<--
// [2a] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) = ------------------------ |
// DWORD #2 |
// [2c] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-0]&0xFFFF) = !SHR to be avoided! |<--
// [2b] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]&0xFFFF) = --------------------------- |
// DWORD #1 |
// [2d] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-0]>>16) = |
// [2c] (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-0]&0xFFFF) = ------------------------------
//if ( ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-6]&0xFFFF) ) & ( (1<<16)-1 )] ) + ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]&0xFFFF)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) ) & ( (1<<16)-1 )] ) + ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-0]&0xFFFF)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-2]&0xFFFF) ) & ( (1<<16)-1 )] ) < 3 ) Gulliver = cbPattern-(2-1)-2-6; else {
// Since the above Decumanus mumbo-jumbo (3 overlapping lookups vs 2 non-overlapping lookups) is not fast enough we go DuoDecumanus or 3x4:
// [2y][2x][2a][2b][2c][2d]
// DWORD #3
// DWORD #2
// DWORD #1
//if ( ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]>>16)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-4]&0xFFFF) ) & ( (1<<16)-1 )] ) + ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-8]>>16)+(*(uint32_t *)&pbTarget[i+cbPattern-1-1-2-8]&0xFFFF) ) & ( (1<<16)-1 )] ) < 2 ) Gulliver = cbPattern-(2-1)-2-8; else {
if ( *(uint32_t *)&pbTarget[i] == ulHashPattern) {
// Order 4 [
// Let's try something "outrageous" like comparing with[out] overlap BBs 4bytes long instead of 1 byte back-to-back:
// Inhere we are using order 4, 'cbPattern - Order + 1' is the number of BBs for text 'cbPattern' bytes long, for example, for cbPattern=11 'fastest fox' and Order=4 we have BBs = 11-4+1=8:
//0:"fast" if the comparison failed here, 'count' is 1; 'Gulliver' is cbPattern-(4-1)-7
//1:"aste" if the comparison failed here, 'count' is 2; 'Gulliver' is cbPattern-(4-1)-6
//2:"stes" if the comparison failed here, 'count' is 3; 'Gulliver' is cbPattern-(4-1)-5
//3:"test" if the comparison failed here, 'count' is 4; 'Gulliver' is cbPattern-(4-1)-4
//4:"est " if the comparison failed here, 'count' is 5; 'Gulliver' is cbPattern-(4-1)-3
//5:"st f" if the comparison failed here, 'count' is 6; 'Gulliver' is cbPattern-(4-1)-2
//6:"t fo" if the comparison failed here, 'count' is 7; 'Gulliver' is cbPattern-(4-1)-1
//7:" fox" if the comparison failed here, 'count' is 8; 'Gulliver' is cbPattern-(4-1)
count = cbPattern-4+1;
// Below comparison is UNIdirectional:
while ( count > 0 && *(uint32_t *)(pbPattern+count-1) == *(uint32_t *)(&pbTarget[i]+(count-1)) )
count = count-4;
// count = cbPattern-4+1 = 23-4+1 = 20
// boomshakalakaZZZZZZ[ZZZZ] 20
// boomshakalakaZZ[ZZZZ]ZZZZ 20-4
// boomshakala[kaZZ]ZZZZZZZZ 20-8 = 12
// boomsha[kala]kaZZZZZZZZZZ 20-12 = 8
// boo[msha]kalakaZZZZZZZZZZ 20-16 = 4
// If we miss to hit then no need to compare the original: Needle
if ( count <= 0 ) {
// I have to add out-of-range checks...
// i-(PRIMALposition-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4
// FIX from 2014-Apr-27:
// Because (count-1) is negative, above fours are reduced to next twos:
// i-(PRIMALposition-1)+(count-1) >= 0
// &pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4
// The line below is BUGGY:
//if ( (i-(PRIMALposition-1) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) && (&pbTarget[i-(PRIMALposition-1)+(count-1)] <= pbTargetMax - 4) ) {
// The line below is OKAY:
if ( ((signed int)(i-(PRIMALposition-1)+(count-1)) >= 0) && (&pbTarget[i-(PRIMALposition-1)] <= pbTargetMax - 4) ) {
if ( *(uint32_t *)&pbTarget[i-(PRIMALposition-1)] == *(uint32_t *)(pbPattern-(PRIMALposition-1))) { // This fast check ensures not missing a match (for remainder) when going under 0 in loop below:
count = PRIMALlengthCANDIDATE-4+1;
while ( count > 0 && *(uint32_t *)(pbPattern-(PRIMALposition-1)+count-1) == *(uint32_t *)(&pbTarget[i-(PRIMALposition-1)]+(count-1)) )
count = count-4;
if ( count <= 0 ) return(pbTarget+i-(PRIMALposition-1));
}
}
}
// In order to avoid only-left or only-right WCS the memcmp should be done as left-to-right and right-to-left AT THE SAME TIME.
// Below comparison is BIdirectional. It pays off when needle is 8+++ long:
// for (count = cbPattern-4+1; count > 0; count = count-4) {
// if ( *(uint32_t *)(pbPattern+count-1) != *(uint32_t *)(&pbTarget[i]+(count-1)) ) {break;};
// if ( *(uint32_t *)(pbPattern+(cbPattern-4+1)-count) != *(uint32_t *)(&pbTarget[i]+(cbPattern-4+1)-count) ) {count = (cbPattern-4+1)-count +(1); break;} // +(1) because two lookups are implemented as one, also no danger of 'count' being 0 because of the fast check outwith the 'while': if ( *(uint32_t *)&pbTarget[i] == ulHashPattern)
// }
// if ( count <= 0 ) return(pbTarget+i);
// Checking the order 2 pairs in mismatched DWORD, all the 3:
//if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+count-1]] == 0 ) Gulliver = count; // 1 or bigger, as it should
//if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+count-1+1]] == 0 ) Gulliver = count+1; // 1 or bigger, as it should
//if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+count-1+1+1]] == 0 ) Gulliver = count+1+1; // 1 or bigger, as it should
// if ( bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+count-1]] + bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+count-1+1]] + bm_Horspool_Order2[*(unsigned short *)&pbTarget[i+count-1+1+1]] < 3 ) Gulliver = count; // 1 or bigger, as it should, THE MIN(count,count+1,count+1+1)
// Above compound 'if' guarantees not that Gulliver > 1, an example:
// Needle: fastest tax
// Window: ...fastast tax...
// After matching ' tax' vs ' tax' and 'fast' vs 'fast' the mismathced DWORD is 'test' vs 'tast':
// 'tast' when factorized down to order 2 yields: 'ta','as','st' - all the three when summed give 1+1+1=3 i.e. Gulliver remains 1.
// Roughly speaking, this attempt maybe has its place in worst-case scenarios but not in English text and even not in ACGT data, that's why I commented it in original 'Shockeroo'.
//if ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+count-1]>>16)+(*(uint32_t *)&pbTarget[i+count-1]&0xFFFF) ) & ( (1<<16)-1 )] == 0 ) Gulliver = count; // 1 or bigger, as it should
// Above line is replaced by next one with better hashing:
// if ( bm_Horspool_Order2[( (*(uint32_t *)&pbTarget[i+count-1]>>(16-1))+(*(uint32_t *)&pbTarget[i+count-1]&0xFFFF) ) & ( (1<<16)-1 )] == 0 ) Gulliver = count; // 1 or bigger, as it should
// Order 4 ]
}
}
} else Gulliver = cbPattern-(2-1)-2; // -2 because we check the 4 rightmost bytes not 2.
i = i + Gulliver;
//GlobalI++; // Comment it, it is only for stats.
}
return(NULL);
} // if ( cbPattern<=NeedleThreshold2vs4swampLITE )
} // if ( cbPattern<=NeedleThreshold2vs4swampLITE )
} //if ( cbPattern<4 )
};
string regexSearch(string st,string pattern,int idx){
vector <string> rg=regexSearch(st,pattern);
if(rg.size()>idx)return rg[idx]; else return "";
}
vector<string> regexSearch(string &st,string &pattern){
//dbgv(pattern);
//pattern="work";
regex ex(pattern);
//dbgv(3);
vector<string> res;
string::const_iterator start, end;
start = st.begin();
end = st.end();
match_results<std::string::const_iterator> what;
regex_constants::match_flag_type flags= regex_constants::match_default;
while(regex_search(start, end, what, ex, flags))
{
for (size_t i = 0; i < what.size(); ++i)
res.push_back(what[i].str() );
start = what[0].second;
}
return res;
}
vector<string> regexSearcher(string &st,string &pattern){
regex ex(pattern);
vector<string> res;
string::const_iterator start, end;
start = st.begin();
end = st.end();
match_results<std::string::const_iterator> what;
regex_constants::match_flag_type flags= regex_constants::match_default;
while(regex_search(start, end, what, ex, flags))
{
//capture only groups () ps:[] isnt show
for (size_t i = 1; i < what.size(); ++i){
res.push_back(what[i].str() );
//dbgv(i,res.back());
}
start = what[0].second;
}
return res;
}
#if !defined(_TRUNCATE)
#define _TRUNCATE ((size_t)-1)
#endif
int _snprintf_s( char *buffer, size_t sizeOfBuffer, size_t count, const char *format, ...){
int retval;
va_list ap;
if ((count != _TRUNCATE) && (count < sizeOfBuffer)) {
sizeOfBuffer = count;
}
va_start(ap, format);
retval = vsnprintf(buffer, sizeOfBuffer, format, ap);
va_end(ap);
if ((0 <= retval) && (sizeOfBuffer <= (size_t) retval)) {
retval = -1;
}
return retval;
}
string replaceAll( std::string const& original, std::string const before, std::string const& after ){
std::string retval;
std::string::const_iterator end = original.end();
std::string::const_iterator current = original.begin();
std::string::const_iterator next = std::search( current, end, before.begin(), before.end() );
while ( next != end ) {
retval.append( current, next );
retval.append( after );
current = next + before.size();
next = std::search( current, end, before.begin(), before.end() );
}
retval.append( current, next );
return retval;
}
//direct replaceAll
void subst( string & original, string before,int after ){
subst(original,before,to_string(after));
}
void subst( string & original, string before, std::string after ){
std::string retval;
std::string::const_iterator end = original.end();
std::string::const_iterator current = original.begin();
std::string::const_iterator next = std::search( current, end, before.begin(), before.end() );
while ( next != end ) {
retval.append( current, next );
retval.append( after );
current = next + before.size();
next = std::search( current, end, before.begin(), before.end() );
}
retval.append( current, next );
original=retval;
// return retval;
}
typedef struct ConvLetter {
char win1251;
int unicode;
} Letter;
static Letter g_letters[] = {
{0x82, 0x201A}, // SINGLE LOW-9 QUOTATION MARK
{0x83, 0x0453}, // CYRILLIC SMALL LETTER GJE
{0x84, 0x201E}, // DOUBLE LOW-9 QUOTATION MARK
{0x85, 0x2026}, // HORIZONTAL ELLIPSIS
{0x86, 0x2020}, // DAGGER
{0x87, 0x2021}, // DOUBLE DAGGER
{0x88, 0x20AC}, // EURO SIGN
{0x89, 0x2030}, // PER MILLE SIGN
{0x8A, 0x0409}, // CYRILLIC CAPITAL LETTER LJE
{0x8B, 0x2039}, // SINGLE LEFT-POINTING ANGLE QUOTATION MARK
{0x8C, 0x040A}, // CYRILLIC CAPITAL LETTER NJE
{0x8D, 0x040C}, // CYRILLIC CAPITAL LETTER KJE
{0x8E, 0x040B}, // CYRILLIC CAPITAL LETTER TSHE
{0x8F, 0x040F}, // CYRILLIC CAPITAL LETTER DZHE
{0x90, 0x0452}, // CYRILLIC SMALL LETTER DJE
{0x91, 0x2018}, // LEFT SINGLE QUOTATION MARK
{0x92, 0x2019}, // RIGHT SINGLE QUOTATION MARK
{0x93, 0x201C}, // LEFT DOUBLE QUOTATION MARK
{0x94, 0x201D}, // RIGHT DOUBLE QUOTATION MARK
{0x95, 0x2022}, // BULLET
{0x96, 0x2013}, // EN DASH
{0x97, 0x2014}, // EM DASH
{0x99, 0x2122}, // TRADE MARK SIGN
{0x9A, 0x0459}, // CYRILLIC SMALL LETTER LJE
{0x9B, 0x203A}, // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
{0x9C, 0x045A}, // CYRILLIC SMALL LETTER NJE
{0x9D, 0x045C}, // CYRILLIC SMALL LETTER KJE
{0x9E, 0x045B}, // CYRILLIC SMALL LETTER TSHE
{0x9F, 0x045F}, // CYRILLIC SMALL LETTER DZHE
{0xA0, 0x00A0}, // NO-BREAK SPACE
{0xA1, 0x040E}, // CYRILLIC CAPITAL LETTER SHORT U
{0xA2, 0x045E}, // CYRILLIC SMALL LETTER SHORT U
{0xA3, 0x0408}, // CYRILLIC CAPITAL LETTER JE
{0xA4, 0x00A4}, // CURRENCY SIGN
{0xA5, 0x0490}, // CYRILLIC CAPITAL LETTER GHE WITH UPTURN
{0xA6, 0x00A6}, // BROKEN BAR
{0xA7, 0x00A7}, // SECTION SIGN
{0xA8, 0x0401}, // CYRILLIC CAPITAL LETTER IO
{0xA9, 0x00A9}, // COPYRIGHT SIGN
{0xAA, 0x0404}, // CYRILLIC CAPITAL LETTER UKRAINIAN IE
{0xAB, 0x00AB}, // LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
{0xAC, 0x00AC}, // NOT SIGN
{0xAD, 0x00AD}, // SOFT HYPHEN
{0xAE, 0x00AE}, // REGISTERED SIGN
{0xAF, 0x0407}, // CYRILLIC CAPITAL LETTER YI
{0xB0, 0x00B0}, // DEGREE SIGN
{0xB1, 0x00B1}, // PLUS-MINUS SIGN
{0xB2, 0x0406}, // CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I
{0xB3, 0x0456}, // CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I
{0xB4, 0x0491}, // CYRILLIC SMALL LETTER GHE WITH UPTURN
{0xB5, 0x00B5}, // MICRO SIGN
{0xB6, 0x00B6}, // PILCROW SIGN
{0xB7, 0x00B7}, // MIDDLE DOT
{0xB8, 0x0451}, // CYRILLIC SMALL LETTER IO
{0xB9, 0x2116}, // NUMERO SIGN
{0xBA, 0x0454}, // CYRILLIC SMALL LETTER UKRAINIAN IE
{0xBB, 0x00BB}, // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
{0xBC, 0x0458}, // CYRILLIC SMALL LETTER JE
{0xBD, 0x0405}, // CYRILLIC CAPITAL LETTER DZE
{0xBE, 0x0455}, // CYRILLIC SMALL LETTER DZE
{0xBF, 0x0457} // CYRILLIC SMALL LETTER YI
};
int ConvertUtf8ToCp1251(const char* utf8, char* windows1251, size_t n){
int i = 0;
int j = 0;
for(; i < (int)n && utf8[i] != 0; ++i) {
char prefix = utf8[i];
char suffix = utf8[i+1];
if ((prefix & 0x80) == 0) {
windows1251[j] = (char)prefix;
++j;
} else if ((~prefix) & 0x20) {
int first5bit = prefix & 0x1F;
first5bit <<= 6;
int sec6bit = suffix & 0x3F;
int unicode_char = first5bit + sec6bit;
if ( unicode_char >= 0x410 && unicode_char <= 0x44F ) {
windows1251[j] = (char)(unicode_char - 0x350);
} else if (unicode_char >= 0x80 && unicode_char <= 0xFF) {
windows1251[j] = (char)(unicode_char);
} else if (unicode_char >= 0x402 && unicode_char <= 0x403) {
windows1251[j] = (char)(unicode_char - 0x382);
} else {
int count = sizeof(g_letters) / sizeof(Letter);
for (int k = 0; k < count; ++k) {
if (unicode_char == g_letters[k].unicode) {
windows1251[j] = g_letters[k].win1251;
goto NEXT_LETTER;
}
}
// can't convert this char
return 0;
}
NEXT_LETTER:
++i;
++j;
} else {
// can't convert this chars
return 0;
}
}
windows1251[j] = 0;
return 1;
}
void ConvertCp1251ToUtf8( char *out, const char *in) {
static const int table[128] = {
0x82D0,0x83D0,0x9A80E2,0x93D1,0x9E80E2,0xA680E2,0xA080E2,0xA180E2,
0xAC82E2,0xB080E2,0x89D0,0xB980E2,0x8AD0,0x8CD0,0x8BD0,0x8FD0,
0x92D1,0x9880E2,0x9980E2,0x9C80E2,0x9D80E2,0xA280E2,0x9380E2,0x9480E2,
0,0xA284E2,0x99D1,0xBA80E2,0x9AD1,0x9CD1,0x9BD1,0x9FD1,
0xA0C2,0x8ED0,0x9ED1,0x88D0,0xA4C2,0x90D2,0xA6C2,0xA7C2,
0x81D0,0xA9C2,0x84D0,0xABC2,0xACC2,0xADC2,0xAEC2,0x87D0,
0xB0C2,0xB1C2,0x86D0,0x96D1,0x91D2,0xB5C2,0xB6C2,0xB7C2,
0x91D1,0x9684E2,0x94D1,0xBBC2,0x98D1,0x85D0,0x95D1,0x97D1,
0x90D0,0x91D0,0x92D0,0x93D0,0x94D0,0x95D0,0x96D0,0x97D0,
0x98D0,0x99D0,0x9AD0,0x9BD0,0x9CD0,0x9DD0,0x9ED0,0x9FD0,
0xA0D0,0xA1D0,0xA2D0,0xA3D0,0xA4D0,0xA5D0,0xA6D0,0xA7D0,
0xA8D0,0xA9D0,0xAAD0,0xABD0,0xACD0,0xADD0,0xAED0,0xAFD0,
0xB0D0,0xB1D0,0xB2D0,0xB3D0,0xB4D0,0xB5D0,0xB6D0,0xB7D0,
0xB8D0,0xB9D0,0xBAD0,0xBBD0,0xBCD0,0xBDD0,0xBED0,0xBFD0,
0x80D1,0x81D1,0x82D1,0x83D1,0x84D1,0x85D1,0x86D1,0x87D1,
0x88D1,0x89D1,0x8AD1,0x8BD1,0x8CD1,0x8DD1,0x8ED1,0x8FD1
};
while (*in)
if (*in & 0x80) {
int v = table[(int)(0x7f & *in++)];
if (!v)
continue;
*out++ = (char)v;
*out++ = (char)(v >> 8);
if (v >>= 16)
*out++ = (char)v;
}
else
*out++ = *in++;
*out = 0;
}
bool stringCompare( const string &left, const string &right ){
for( string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
if( tolower( *lit ) < tolower( *rit ) )
return true;
else if( tolower( *lit ) > tolower( *rit ) )
return false;
if( left.size() < right.size() )
return true;
return false;
}
const char* toChar(int Number){ return to_string(Number).c_str();}
///format "%.2f"=2decimais sempre, "%.2g"=2 decimais quando tem
const char* toChar(float Number,char* format){ char rs[24]; sprintf(rs, format, Number);return rs;}
string to_string(float Number,char* format){ char rs[24]; sprintf(rs, format, Number);return string(rs);}
string toLower(string data){transform(data.begin(), data.end(), data.begin(), ::tolower);return data;}
string string_escape(string &in){
stringstream str;
for(int i=0;i<in.size();i++)
switch (in[i])
{
case '\0': str << "\\000"; break;
case '\a': str << "\\a"; break;
case '\b': str << "\\b"; break;
case '\f': str << "\\f"; break;
case '\v': str << "\\v"; break;
case '\"': str << "\\\""; break;
case '\'': str << "\\\'"; break;
case '\\': str << "\\\\"; break;
case '\n': str << "\\n"; break;
case '\r': str << "\\r"; break;
case '\t': str << "\\t"; break;
case '\?': str << "\\?"; break;
default: str << in[i];
}
return str.str();
};
void string_toHeaderFile(string name,string &toCompress){
string fn=name+".h";
ofstream m; m.open(fn,std::ios::binary );m<<"string ";m<<name;m<<"(\"";m<<string_escape(toCompress);m<<"\",";m<<toCompress.size();m<<");";m.close();
};
string base64_encode(const std::string &in) {
std::string out;
int val=0, valb=-6;
for (uchar c : in) {
val = (val<<8) + c;
valb += 8;
while (valb>=0) {
out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(val>>valb)&0x3F]);
valb-=6;
}
}
if (valb>-6) out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((val<<8)>>(valb+8))&0x3F]);
while (out.size()%4) out.push_back('=');
return out;
}
string base64_decode(const std::string &in) {
std::string out;
std::vector<int> T(256,-1);
for (int i=0; i<64; i++) T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
int val=0, valb=-8;
for (uchar c : in) {
if (T[c] == -1) break;
val = (val<<6) + T[c];
valb += 6;
if (valb>=0) {
out.push_back(char((val>>valb)&0xFF));
valb-=8;
}
}
return out;
}