-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestd.h
1200 lines (1083 loc) · 36 KB
/
estd.h
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* > [2023/07/12 OpenSource]
* >
* > Project: Estd Header v9
* > Copyright: Lioncky(Personal)
* > WebSite: https://github.com/Lioncky/estd
* > Detail : Build in China, begin this at latest update 2024/10.29
* More information about sprintf series api
* https://learn.microsoft.com/zh-cn/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=msvc-170
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#ifndef _ESTD_H_
# define _ESTD_H_
# include <stdarg.h> // va_list
# include <malloc.h> // malloc
# include <stdlib.h> // atoi atoll
# include <stdio.h>// vsprintf
#ifndef esnull
# define esnull ((char*)"")
# define ealloc (char*)::malloc
# define esdo(x) extern void x(); x();
# define emss(x) # x // macro 2(to) string
# define enss(x) emss(x) // enum 2(to) string
# define efff(s,m) ((unsigned)&((s*)0)->m) /// eoffsetof
# define einfunc __FUNCTION__ "->" enss(__LINE__) ":" // MSVC
# define eoutf(func) __declspec(deprecated("Using " # func " instead..."))
# define epublic __declspec(dllexport)
# define eauto decltype(auto)
// estd typedefs [br-cstr]
// char-byte short-word int-uint lng-ulng eint/intr-esize_t/uintr
typedef bool br; typedef char* cstr; typedef const char* ccstr;
typedef unsigned long long uiint, ulng;
typedef unsigned short word;
typedef unsigned char byte;
typedef long long iint, lng;
typedef unsigned int uint;
#if !_M_X64
typedef long eint, intr;
typedef unsigned long esize_t, uintr;
#else
typedef long long eint, intr;
typedef unsigned long long esize_t, uintr;
#endif
# if _DEBUG && _WIN32
# define dbk(...) __debugbreak();
# define dbi(b,...) if(b) __debugbreak();
# else
# define dbk(...)
# define dbi
# endif
#endif // esnull
#ifndef __UWSTR__
# define __UWSTR__ // Unique (Wide) String
inline constexpr unsigned u_FNV_CA = 0x975563U;// 0x1000193;// 16777619U;
inline constexpr unsigned u_FNV_OS = 0x84664995U;// 0x811C9DC5; // 2166136261U;
constexpr unsigned _uxchg(const unsigned _u) { return ((_u >> 24)) | ((_u << 24)) | ((_u & 0xFF00) << 8) | ((_u >> 8) & 0xFF00); } // 0x12345678 >> 0x78563412 LE->BigEndian
constexpr unsigned _uu(const char* str) { unsigned u = *str; u |= *++str << 0x08; u |= *++str << 0x10; u |= *++str << 0x18; return u; }
constexpr unsigned _uw(const wchar_t* str) { auto v = u_FNV_CA; while (*str) v ^= u_FNV_OS * unsigned(*str++); return v ^ (v >> 16); }
constexpr unsigned _us(const char* str) { auto v = u_FNV_CA; while (*str) v ^= u_FNV_OS * unsigned(*str++); return v ^ (v >> 16); }
constexpr unsigned operator"" _uw(const wchar_t* str, size_t) { return _uw((wchar_t*)str); }
constexpr unsigned operator"" _uu(const char* str, size_t) { return _uu(str); }
constexpr unsigned operator"" _us(const char* str, size_t) { return _us(str); }
constexpr size_t operator"" _L(const wchar_t*, size_t wSize) { return wSize; }
constexpr size_t operator"" _l(const char*, size_t nSize) { return nSize; }
#define uu(x) (*(unsigned*)(x)) // _us == uhash _uint == _uu
#endif
// #i to len
// 'ad' = 2 'abcd' = 4
inline constexpr int eill(const int div) { if (!div) return 0; else if (div < 0x100) return 1; else if (div & (0xFF << 24)) return 4; else if (div & (0xFF << 16)) return 3; else return 2; }// if (div & (0xFF << 8))
inline size_t estrlen(const char* _Buf) {
const char* _Bak = _Buf;
if (_Buf) while (*_Buf)_Buf++;
return _Buf - _Bak;
}
inline bool ecmpi(char _a, char _b) {
constexpr char _delta = 'a' - 'A';
return _a == _b || _a + _delta == _b || _a - _delta == _b;
}
#define esame(x,y,z) (0 == ememcmp(x,y,z))
inline int ememcmp(void const* _Buf1, void const* _Buf2, size_t _Size) {
while (*(unsigned char*)_Buf1 == *(unsigned char*)_Buf2) {
if (--_Size == 0)
return 0;
*(size_t*)&_Buf1 += 1;
*(size_t*)&_Buf2 += 1;
}
return 1;
}
#define estrsame(x,y) (0 == estrcmp(x,y))
inline int estrcmp(const char* _Buf1, const char* _Buf2) {
if (_Buf1 && _Buf2) {
size_t _Size = estrlen(_Buf1) + 1;
return ememcmp(_Buf1, _Buf2, _Size);
}
return 1;
}
#define estrsamei(x,y) (0 == estrcmpi(x,y))
inline int estrcmpi(const char* _a, const char* _b) {
if (!_a || !_b)
return 1;
while (true) {
char a = *_a++;
char b = *_b++;
if (a) {
if (!(a == b || a + ' ' == b || a - ' ' == b))
return 1;
}
else {
if (!b)
return 0;
return -1;
}
}
}
#ifndef esprintf
# define esvprintf _esvprintf
inline char* _esvprintf(size_t * _Len, const char *_Format, va_list _ArgList) {
char* _s; size_t _u;
if ((_u = _vscprintf(_Format, _ArgList)) > 0) {
if (_s = (va_list)malloc(_u <<= 1 )) {
_vsnprintf(_s, _u, _Format, _ArgList);
if (_Len) *_Len = (_u >> 1);
return _s;
}
}
return nullptr;
}
#endif // esvprintf
#ifndef wcs2us
# define wcs2us _wcs2us
# define us2wcs _us2wcs
// 'utf16'->utf8
inline wchar_t* _us2wcs(
_In_z_ const char* ustr,
_Out_ size_t * opt = 0) {
unsigned u = 0; wchar_t* bak, *dst;
bak = dst = (wchar_t*)ealloc((strlen(ustr)+1) << 1);
if (!dst) return nullptr;
while (u = *ustr++) {
if ((u & 0x80) == 0x00);
else if ((u & 0xF0) == 0xE0) { // 3
u &= 0x0F; u <<= 6;
u |= (*ustr++ & 0x3F); u <<= 6;
u |= (*ustr++ & 0x3F);
}
else if ((u & 0xE0) == 0xC0) { // 2
u &= 0x0F; u <<= 6;
u |= (*ustr++ & 0x3F);
}
else if ((u & 0xF8) == 0xF0) { // 4
u &= 0x0F; u <<= 6;
u |= (*ustr++ & 0x3F); u <<= 6;
u |= (*ustr++ & 0x3F); u <<= 6;
u |= (*ustr++ & 0x3F);
}
*dst++ = (wchar_t)u;
} *dst = L'\0';
if (opt) *opt = dst - bak; *dst = '\0';
return bak;
}
// 'utf8'->'utf16'
inline char* _wcs2us(
_In_z_ const wchar_t* wbuf,
_Out_ size_t * opt = 0) {
unsigned lowSurrogate, u=0;
char* dst = ealloc((wcslen(wbuf) + 1) * 3), * bak = dst;
if(!dst) return nullptr;
while (u = *wbuf++) {
if (u < 0x80) *dst++ = u;
else if (u < 0x800) {
*dst++ = 0xC0 | ((u & 0x7C0) >> 6);
*dst++ = 0x80 | (u & 0x3F);
}
else if (u < 0xD800) {
_UTF_3:
*dst++ = 0xE0 | ((u & 0xF000) >> 12);
*dst++ = 0x80 | ((u & 0xFC0) >> 6);
*dst++ = 0x80 | ((u & 0x3F));
}
else if (u <= 0xDBFF) { // HighSurrogate
lowSurrogate = (*wbuf++ & 0xFFFF);
if (lowSurrogate >= 0xDC00 && lowSurrogate <= 0xDFFF) { // LowSurrogate
u = (((u - 0xD800) << 10) | (lowSurrogate - 0xDC00)) | 0x10000;
*dst++ = 0xF0 | ((u & 0x1C0000) >> 18);
*dst++ = 0x80 | ((u & 0x3F000) >> 12);
*dst++ = 0x80 | ((u & 0xFC0) >> 6);
*dst++ = 0x80 | (u & 0x3F);
} else dbk ("ERROR NO Surrogate");
}
else goto _UTF_3;
}
if (opt) *opt = dst - bak; *dst = '\0';
return bak;
}
#endif
//
// #estr_encrypter
//
template <size_t N, size_t M=(N%4)?(N/4+1):(N/4)>
class ecstr {
public:
constexpr ecstr(const wchar_t(&str)[N]) {
for (int i = 0, j = 0; i < M; i++) {
m_enc[i] = str[j++]; if (j == N)break;
m_enc[i] |= str[j++]<<8;if(j==N)break;
m_enc[i] |= str[j++]<<16;if(j==N)break;
m_enc[i] |= str[j++]<<24;if(j==N)break;
} } // operator char*() noexcept -- Can't for constexpr
operator char*() const noexcept { return (char*)m_enc; }
char* operator*() const noexcept { return (char*)m_enc; }
operator const void*() const noexcept { return (char*)m_enc; }
constexpr unsigned& operator[](_In_range_(0, M - 1) size_t _Pos) noexcept { return m_enc[_Pos]; }
constexpr const unsigned& operator[](_In_range_(0, M - 1) size_t _Pos) const noexcept { return m_enc[_Pos]; }
private: unsigned m_enc[M]{};
};
//
// #estr_asprinter
//
typedef class estr {
public:
auto& size() { return this->esize; }
bool fo() { return this->esize > 0; }
char* data() { return this->esdata; }
int lenth() { return (int)this->esize; }
bool empty() { return this->esize == 0; }
const char* c_str() { return this->esdata; }
unsigned length() { return (unsigned)this->esize; }
char* data_end() { return this->esdata + this->esize - 1; }
unsigned hash() { return this->esize ? _us(this->esdata) : ~0; }
unsigned length_utf8() { return this->_len_utf8(this->esdata); }
char* begin() { return this->esdata; } char* end() { return this->esdata + this->esize; }
template <typename T> T& to_this(int offset = 0) { return *reinterpret_cast<T*>(this->esdata + offset); }
// .ctor()
estr() { this->_reset_unsafe(); }
estr(const wchar_t* str) : esize(0) {
esdata = _wcs2us(str ? str : L"", &this->esize);
}
estr(const char* str) : esdata(0) { // boost for 0 and ""
(str && *str) ? (void)this->assign(str) : this->_reset_unsafe();
}
explicit estr(va_list va, const char* fmt) : esize(0) {
this->esdata = esvprintf(&this->esize, fmt, va); va_end(va);
}
explicit estr(size_t szf, const char* str, bool _unsafe = false) : esdata(0) {
if (!_unsafe) szf ? (void)this->assign(str, szf) : this->_reset_unsafe(); else this->unsafe_set(str, szf);
}
template <size_t N> estr(char(&str)[N]) : esdata(0){ this->assign(str);}
template <size_t N> estr(const char(&str)[N]) : esdata(0){ this->assign(str, N-1);}
template <size_t N> estr(const char(&fmt)[N], ...) : esdata(0) { va_list va;
#if !_M_X64
__asm lea eax,[fmt]
__asm add eax, 2+2
__asm mov [va], eax
#else
va_start(va, (char*)fmt); // x64 unsupport &fmt
#endif
this->esdata = esvprintf(&esize, fmt, va);
va_end(va); if (!this->esdata) this->_reset_unsafe();
}
estr(estr&& _) noexcept : esdata(_.esdata), esize(_.esize) { _.esdata = 0; } // move
estr(const estr& _) { this->_reset_unsafe(); if (_.esize > 0) this->assign(_.esdata, _.esize); } // copy
estr& operator=(estr&& _) noexcept { this->_free(); this->unsafe_set(_.esdata, _.esize); _.esdata = 0; return *this; } // move_copy
estr& operator=(const estr& _) noexcept { this->assign(_.esdata, _.esize); return *this; } // assign_copy
template <size_t N> estr& operator=(const char(&str)[N]) { this->assign(str, N - 1); return *this; }
inline char* unsafe_forget() { char* dat = this->esdata; this->esdata = esnull; return dat; } // #WARNAPI MANUAL
inline void unsafe_set(const char* ptr, size_t _) { this->esdata = (char*)ptr; this->esize = _; } // #WARNAPI MANUAL
bool clear() { this->_free(); this->_reset_unsafe(); return true; }
void fmts(const char* fmt, ...) { // fmt_s support self
va_list va; va_start(va, fmt);
cstr _s = esvprintf(&this->esize, fmt, va); va_end(va);
this->_free(); if (_s) this->esdata = _s; else this->_reset_unsafe();
}
bool assign(const char* src, size_t _ = ~0) {
if (!src) { this->clear(); return true; }
if (_ == ~0) _ = strlen(src);
this->_free();
if ( this->_alloc_data(_) ) {
*(this->esdata + _) = '\0';
memcpy(this->esdata, src, _);
return true;
} return false;
}
bool append(const char* src, size_t _ = ~0) {
if (!src) { return false; } if (_ == ~0) _ = strlen(src);
if (this->empty()) { this->assign(src, _); return true; }
cstr bak = this->unsafe_forget();
size_t szbak = this->esize;
if ( this->_alloc_data(this->esize + _) ) {
memcpy(this->esdata, bak, szbak);
memcpy(this->esdata + szbak, src, _);
*(this->esdata + this->esize) = '\0';
if( bak != esnull ) free(bak); return true;
} else { this->esdata = bak; this->esize = szbak; return false;}
}
void dump(estr ectr) {
this->_free();
this->esdata = ectr.esdata;
this->esize = ectr.esize;
ectr.esdata = esnull;
}
bool reserve(size_t szr) {
this->_free();
return this->_alloc_data(szr);
}
bool resize(size_t szr) {
if (szr >= this->esize) {
size_t ori = this->esize;
auto* ptr = this->esdata;
if (this->_alloc_data(szr + this->esize)) {
if (ori > 0) {
memcpy(this->esdata, ptr, ori + 2);
free(ptr);
}
return true;
}
this->esdata = ptr; this->esize = ori;
return false;
}
this->esize = szr;
this->endl();
return true;
}
// #SAFE cout && endl
void cout() { fwrite(this->esdata, this->esize, true, stdout); }
bool endl(int _c = '\0') { if (this->_valid()) { *(short*)(this->esdata + this->esize) = _c; return true; } return false; }
// #endby this->asize will add +1
// #endremove this->asize will sub -1
// WARN: only can be just use for once
bool endrm() { if (this->_valid()) { *(this->esdata + --this->esize) = '\0'; return true; } return false; }
bool endby(int str) { if (this->_valid()) { *(short*)(this->esdata + this->esize++) = (short)str; return true; } return false; }
void byi(int _v) { this->fmts("%d", _v); }
void byl(long long _v) { this->fmts("%lld", _v); }
void byhex(long long _v) { this->fmts("0x%llX", _v); }
void byf(double _v, int count = 2) { char fmt[8] = "%._f"; fmt[2] = count + '0'; this->fmts(fmt, _v); }
void byuuid(unsigned u, unsigned _v = 0) { _v = u * 0x84664995; u *= _v; this->fmts("%08X-%04X-%04X-%04X-%04X%08X", _v, u >> 16, u & 0xFFFF, (~u) >> 16, (~u) & 0xFFFF, ~_v); }
void bybin(const char* p, unsigned len) {
char* w, *bk; this->_free();
if ( p && this->_alloc_data(len * 3 - 1) ) {
unsigned char i, v = 0; bk = w = this->esdata; while (len--) {
v = *(unsigned char*)p++; i = v >> 4; v &= 0x0F;
*w++ = i < 10 ? i + '0' : i + ('A' - 10); // F5 15 5
*w++ = v < 10 ? v + '0' : v + ('A' - 10); *w++ = ' ';
} *--w = '\0'; this->esize = w - bk; // remove end of ' '
} else this->_reset_unsafe();
}
// #SAFE cvt_api
int toi() { return this->esdata ? atoi(this->esdata) : 0; }
double tof() { return this->esdata ? atof(this->esdata) : 0; }
long long tol() { return this->esdata ? atoll(this->esdata) : 0; }
// #SAFE contain_api
char* find(int _c) { return strchr(this->esdata, _c); }
char* rfind(int _c) { return strrchr(this->esdata, _c); }
char* find(const char* str) { return str ? strstr(this->esdata, str) : nullptr; }
char* rfind(const char* str) {
size_t ufind; char* endstr;
if (str && this->fo()) {
ufind = strlen(str);
endstr = this->esdata + this->esize;
do {
if (0==memcmp(--endstr, str, ufind))
return endstr;
} while (endstr != this->esdata);
}
return nullptr;
}
size_t occurs(int _c) { size_t uc = 0; char* _s; if (_s = this->esdata) while (_s = strchr(_s, _c)) { uc++; _s++; } return uc; }
size_t occurs(const char* str, size_t strl = ~0) {
size_t uc = 0; char* _s;
if (this->fo() && (_s = strstr(this->esdata, str)) ) {
if (strl == ~0) strl = strlen(str);
do { uc++; _s += strl; }
while (_s = strstr(_s, str));
}
return uc;
}
//
// #SAFE pick special part
//
template <size_t _M, size_t _N>
estr pickm(const char(&sleft)[_M], const char(&sright)[_N], bool bContain = false) { return taketry(trytake(sleft, sright, bContain)); }
estr pickl(const char* str) { str = this->find(str); return estr(str ? str - this->esdata : 0, this->esdata); }
estr pickr(const char* str) { if (char* _ = this->find(str)) return _ + strlen(str); return ""; }
//
// #SAFE take by index
// index start form 0 not 1
//
estr takem(size_t _l, size_t _r) { if (_l < _r && this->esize >= _r) return estr(_r - _l, this->esdata + _l); return ""; }
estr taker(size_t _c) { return estr(this->esize >= _c ? _c : 0, this->esdata + this->esize - _c); }
estr takel(size_t _c) { return estr(this->esize >= _c ? _c : 0, this->esdata); }
//
// #SAFE taketry() is used by trytake()'s result
//
estr taketry(uint tried) { return takem(tried >> 16, tried & 0xFFFF); }
template <size_t _M, size_t _N> // use for [take] -> return left pos << 16 | right pos
unsigned trytake(const char(&sleft)[_M], const char(&sright)[_N], bool bContain = false) {
constexpr size_t N = _N - 1; constexpr size_t M = _M - 1;
unsigned ucode = 0; char* p;
if (this->esize && (p = this->find(sleft)) ) {
if (!bContain) p += M;
ucode = (unsigned)(p - this->esdata) << 16;
if (p = strstr(p, sright)) {
if (bContain) p += N;
ucode |= ( (p - this->esdata) & 0xFFFF );
}
}
return ucode;
}
estr as_retn() {
estr _(this->esize, this->esdata, true);
this->_reset_unsafe();
return _;
}
// do { estr t = l.pop_line(); } while (l);
estr pop_line() {
if (this->esize > 0) {
char* p = this->find('\n');
if (p) {
estr r(
*(p - 1) == '\r' ? p - 1 - this->esdata : p - this->esdata,
this->esdata);
p++;
this->esize -= p - this->esdata;
memcpy(this->esdata, p, this->esize + 1);
return r;
}
else {
return this->as_retn();
}
}
return "";
}
//
// #SAFE rep_api
//
void rm(char _c) { return this->_rep(&_c, 1, "", 0); }
void remove(estr _) { return this->_rep(_.data(), _.size(), "", 0); }
void remove(const char *s, size_t l) { return this->_rep(s, l, "", 0); }
template <size_t N> void remove(const char(&_)[N]) { return this->_rep(_, N - 1, "", 0); }
void rep(int _c, int _to) {
if (!this->esize || !_to) return;
if (_c &= 0xFF) {
char* dat = this->esdata;
while (dat = strchr(dat, _c)) {
*dat++ = _to;
}
}
}
template <size_t N, size_t M>
void replace(const char(&_)[N], const char(&_to)[M]) {
return this->_rep(_, N - 1, _to, M - 1);
}
//
// #SAFE cvt url
//
void cvt_url() { // to utf8
char* _e, *ptr, *p; int w;
if (this->fo()) {
p = ptr = this->esdata; w = 0;
_e = this->esdata + this->esize;
while (p < _e) {
w = *p; if (w == '%') {
w = this->_hex_int(++p) << 4;
w |= this->_hex_int(++p);
} p++; *(unsigned char*)ptr++ = w;
}*ptr = '\0'; this->esize = ptr - this->esdata;
}
}
// cmp left and same remove
template <size_t _N, size_t N = _N - 1>
bool operator-=(const char(&sleft)[_N]) {
if (this->fo() && esame(sleft, this->esdata, N)) {
memcpy(this->esdata, this->esdata + N, this->esize - N + 2);
this->esize -= N;
return true;
} return false;
}
// defines of operator
operator bool() { return !!this->esize; }
operator char* () { return this->esdata; }
char* operator*() { return this->esdata; }
bool operator--() { return this->endrm(); }
unsigned operator-() { return this->hash(); }
operator wchar_t* () { return this->as_wstr(); } // DANGEROUS
operator const void* () { return (const char*)this->esdata; }
operator unsigned char* () { return (unsigned char*)this->esdata; }
bool operator!=(char* str) { return ! operator==(str); }
bool operator==(size_t _size) { return this->esize == _size; }
bool operator&=(const char *obj) { return this->_valid() && strstr(this->esdata, obj); }
bool operator==(const char* str) { return this->_valid() && esame(str, this->esdata, this->size() + 1); }
bool operator==(const estr& str) { return (this->_valid() && this->esize == str.esize) ? esame(str.esdata ? str.esdata : "", this->esdata ? this->esdata : "", this->esize) : false; }
template <size_t N> // cmp this left part
bool operator>=(const char(&str)[N]) { return this->_valid() && esame(str, this->esdata, N - 1); }
template <size_t N> // cmp obj left part
bool operator<=(const char(&obj)[N]) { return this->_valid() && esame(this->esdata,obj, this->esize); }
// Try to use += instead of + to reduce one copy
void operator+=(const char* str) { this->append(str); }
void operator+=(const estr& asr) { this->append(asr.esdata, asr.esize); }
estr operator+(const char* str) { estr tmp = *this; tmp.append(str); return tmp; } // add [asr = asr+"123"+"456"]
estr operator+(const estr& asr) { estr tmp; tmp.reserve(this->esize + asr.esize); memcpy(tmp.esdata, this->esdata, this->esize); memcpy(tmp.esdata + this->esize, asr.esdata, asr.esize); tmp.endl(); return tmp; }
//friend estr operator+(const char* str, estr&& asr) { estr tmp(str); tmp.append(asr.esdata,asr.esize); return tmp; } // "111" + estr("222")
friend estr operator+(const char* str, const estr& asr) { estr tmp(str); tmp.append(asr.esdata,asr.esize); return tmp; } // "111" + estr("222")
char& operator[](size_t N) const { return *(this->esdata + N); }
template <size_t N> // #SAFE xor api "\x99"
inline void operator^=(const char(&str)[N]) { for(size_t i=0;i<this->esize;i++) {for(size_t n=0;n<N-1;n++) {this->esdata[i]^=str[n];}}}
inline void operator^=(int _) { for (size_t i = 0; i < this->esize; i++) { this->esdata[i] ^= _; }}
~estr() { this->_free(); }
// static public support functions
static int _hex_int(const char* pCalc) { int iRet = *pCalc;// 'a'->0xa
if ('a' <= iRet && 'f' >= iRet) return iRet - 'a' + 10;
if ('A' <= iRet && 'F' >= iRet) return iRet - 'A' + 10;
if ('0' <= iRet && '9' >= iRet) return iRet - '0';
return 0;
};
static unsigned _len_utf8(const char* _) {
int _u = 0; auto* pbyte = (unsigned char*)_;
while (true) {
switch (*pbyte++) {
case 0: return _u; // return
case 0xE4ui8:case 0xE7ui8:
case 0xE5ui8:case 0xE8ui8:
case 0xE6ui8:case 0xE9ui8:
_u++; pbyte += 2;break;
default: _u++; break;
}
}
}
private:
char* esdata;
size_t esize;
inline bool _valid() { return !(this->esdata == esnull); }
inline void _reset_unsafe() { this->esize = 0; this->esdata = esnull; } // #UNSAFE_RESET
bool _same(char* ptr) { return ptr && esame(ptr, this->esdata, this->esize + 1); }
char* _alloc_data(size_t _) { if ( this->esdata = ealloc(_ < 8 ? 8 : sizeof(int) + (_ + _ % sizeof(int))) ) { this->esize = _; return this->esdata; } this->_reset_unsafe(); return nullptr; }
void _free() {
if (this->esdata)
if (this->esdata != esnull)
free(this->esdata);
}
void _rep(const char* _, size_t _u, const char* _to, size_t _ut) {
if (this->empty()) return;
char* p, * bak, * n, * n_; size_t _uc;
p = bak = this->esdata;
if (p = strstr(bak, _)) {
_uc = this->size() + 2;
if (_u < _ut) _uc += this->occurs(_, _u) * (_ut - _u);
if (n = n_ = ealloc(_uc)) {
do {
memcpy(n, bak, p - bak); n += (p - bak);
if (_ut) memcpy(n, _to, _ut), n += _ut;
bak = p + _u;
} while (p = strstr(bak, _));
_u = strlen(bak); memcpy(n, bak, _u + 1); n += _u;
}
this->_free();
this->esize = n - n_;
this->esdata = n_;
}
}
//
// #WARN This function is DANGEROUS
// Cvt this to utf16 and then unsupport this->size()
// Attention: Only for win32 api to pass this for one time
//
wchar_t* as_wstr() {
size_t u; wchar_t* w;
if ( !(this->esize == 0) ) {
if (w = us2wcs(this->esdata, &u)) {
this->_free();
this->esdata = (char*)w;
this->esize = 0;
return w;
}
}
return (wchar_t*)this->esdata;
}
} *estr_t;
typedef class estrs {
public:
template <size_t _N, size_t N = _N - 1>
estrs(const char* src, const char (&div)[_N]) :esarg(0), esargc(0) {
static_assert(_N - 1 != 0, "Invalid_div_data");
char* p, * s, * e = (char*)""; size_t u = 0;
if (!src) return; p = s = (char*)src;
// calc argv size
while (s = strstr(s, div)) { s += N; u++; };
this->esarg = new estr[++u];
this->esargc = u; s = p;
for (size_t i = 0; i < u; i++) {
auto& asp = this->esarg[i];
p = strstr(__notnull s, div);
asp.assign(s, p ? p - s : ~0);
s = p + N;
}
}
estr& operator[] (const size_t _) {
if (_ > this->sizes()) {
dbk(!this->sizes(), this);
return *this->esarg;
}
return this->esarg[_];
}
~estrs() {
if (this->esarg) {
delete[](this->esarg);
}
}
size_t sizes() { return this->esargc; }
operator size_t() { return this->esargc; }
// *********** STANDARD ITERATOR ************ //
class iterator {
public:
iterator(estr* t, size_t sz) : itptr(t), itpos(sz) {}
iterator& operator++() { this->itpos++; return *this; }
bool operator !=(iterator& it) { return this->itpos != it.itpos; }
estr& operator*() { return itptr[itpos]; }
private:
estr* itptr; size_t itpos;
};
iterator end() { return iterator(this->esarg, this->esargc); }
iterator begin() { return iterator(this->esarg, 0); };
private:
estr* esarg;
size_t esargc;
} *estrs_t;
struct ewatch {
~ewatch() { if (_fptr) free(_fptr); }
ewatch(void* _) :_fptr(_) {}
void* _fptr;
};
//
// #easyjson define
// Using '#define ej_type' to disable this
//
#ifndef ej_type
# define ej_type (1 << 24)
# define ej_value 0x00000000
# define ej_float 0x01000000
# define ej_bool 0x02000000
# define ej_string 0x04000000
# define ej_json 0x08000000
# define ej_new 0x10000000
# define ej_array 0x20000000
# define ej_null 0x80000000
# define ej_vals (ej_array | ej_value)
# define ej_flts (ej_array | ej_float)
# define ej_strs (ej_array | ej_string)
# define ej_brs (ej_array | ej_bool)
# define ej_jss (ej_array | ej_json)
# define ej_attr 0xFF000000
# define ej_size 0x00FFFFFF
//
// easyjson
// attention: not support parse float array yet
//
typedef class ejson {
public:
ejson() :emap(0), esori(0), ename(0), esttr(0), esuh(0) {}
template<size_t N> ejson(const char(&j_str)[N]) : ejson() { this->parse(j_str, N - 1); }
bool parse(const char* j_str, size_t j_size = ~0, cstr jstr = 0) {
if (this->esuh == 0) {
if (this->esttr & ej_json)
this->destory();
if ((jstr = zip_json(j_str, j_size))) {
this->parse_json(this, jstr, (cstr)"Obj", 1);
this->esori = jstr;
this->esuh = 0;
return true;
}
null_json(this);
}
return false;
}
bool has(const char* name) {
dbi(!this->is_json(), this);
auto i = this->count();
auto _u = _us(name);
while (i--) {
auto& js = ((ejson*)this->emap)[i];
if (js.esuh == _u)
return true;
}
return false;
}
ejson& operator[] (const char *name) {
dbi(!this->is_json(), this);
auto i = this->count();
auto _u = _us(name);
while (i--) {
auto& js = ((ejson*)this->emap)[i];
if (js.esuh == _u)
return js;
}
return *this; // no use for multi obj
}
ejson& operator[] (const size_t uId) {
if (this->is_ary()) {
if (uId < this->count()) {
return ((ejson*)this->emap)[uId];
}
dbk(!this->count());
}
dbk(!this->is_ary(), this);
return *this;
}
//
// replace with string will alloc new memory
//
ejson &operator= (const char* val) {
if (this->valid()) {
this->destory();
this->esori = _strdup(val);
this->esttr = ej_new | ej_string | (ej_size & strlen(val));
this->emap = (intptr_t)this->esori;
if (*val >= '0' && *val <= '9' || *val == '-') {
this->emap = intptr_t(atoll(val));
this->esttr ^= ej_string;
}
}
return *this;
}
//
// motify value will not change the origin string
//
ejson& operator=(const int& val) { return operator=((size_t)(val)); }
ejson& operator=(const size_t& val) {
if (this->is_value()) {
this->by(val);
}
return *this;
}
bool operator==(const char* str) { return this->esori ? 0 == strcmp(str, this->esori) : false; }
operator bool() { return this->valid(); }
operator char*() { return this->operator*(); }
char* operator*() { return (this->valid() && this->esori)? this->esori : (char*)""; }
#ifdef _WIN64
operator size_t () { return this->as<size_t>(); }
#endif
operator int () { return this->as<int>(); }
operator long () { return this->as<long>(); }
operator double () { return this->as<double>(); }
operator float () { return float(this->as<double>()); }
operator long long () { return this->as<long long>(); }
operator unsigned () { return this->as<unsigned>(); }
operator unsigned long () { return this->as<unsigned>(); }
operator unsigned char* () { return this->as<unsigned char*>(); }
bool valid() { return !this->isfather(); }
unsigned count() { return this->esttr & ej_size; }
~ejson() { this->destory(); }
private:
ccstr ename;
cstr esori;
iint emap;
uint esttr; // attri
uint esuh; // hash
template <typename T>
decltype(auto) as() {
return *(T*)&emap;
}
template <typename T>
void by(T val) {
dbi(sizeof T > sizeof emap, "what?");
if constexpr (sizeof T < sizeof emap) {
this->emap = 0;
} *(T*)&emap = val;
}
inline bool isfather() { return esuh == 0 && (esttr & ej_json); }
inline bool is_value() { return (esttr & ej_type) == ej_value; }
inline bool is_null() { return (esttr & ej_null) != 0; }
inline bool is_vals() { return !!(esttr & ej_vals); }
inline bool is_strs() { return !!(esttr & ej_strs); }
inline bool is_jss() { return !!(esttr & ej_jss); }
inline bool is_new() { return !!(esttr & ej_new); }
inline bool is_json() { return !!(esttr & ej_json); }
inline bool is_bool() { return !!(esttr & ej_bool); }
inline bool is_ary() { return !!(esttr & ej_array); }
inline bool is_str() { return !!(esttr & ej_string); }
void destory() {
if (this->isfather()) {
delete[](ejson*)emap;
::free(this->esori);
return;
}
if (this->is_null() || !this->emap) {}
else {
if ((this->esttr & ej_new)) {
if (this->esori) {
::free(this->esori);
}
}
if (this->esttr & ej_array) {
delete[](ejson*)emap;
}
}
}
static void null_json(ejson* jss) {
auto& js = *jss;
js.esori = (char*)"null";
js.esuh = "null"_us;
js.esttr = ej_null;
js.emap = ~0;
}
// 1. compress and check valid
// 2. try recursion parse the json
// 3. package memory for every instence
// compress the json and malloc
static
char* zip_json(const char* j_str, size_t j_sz = ~0) {
if (j_sz == ~0) j_sz = strlen(j_str);
if (j_sz == 0) {
dbk(j_str, j_sz);
return nullptr;
}
char* jw, * jbak;
unsigned ins = 0, _c = 0;
if (jw = ealloc(j_sz + 2)) {
jbak = jw;
while (*j_str) {
if (ins & 1) {
if (*j_str == '\\') {
if (*++j_str == '"') {
*jw++ = '\''; // '
j_str++;
continue;
}
}
else if (*j_str == '"') {
ins ^= 1;
}
*jw++ = *j_str++;
}
else {
// no ins kill space
switch (*j_str) {
case ':': _c++; goto _def;
case '"': ins^=1; goto _def;
case '[':case '{': ins ^=2; goto _def;
case ']':case '}': ins ^=2; goto _def;
case ' ':case '\t':case '\n':case '\r': j_str++; break;
default: _def:
*jw++ = *j_str++;
break;
}
}
}
if (ins == 0) {
*jw = '\0';
return jbak;
}
dbk(ins, "Json illegal");
::free(jbak);
}
return nullptr;
}
static
void array_json(ejson* jss, cstr p) {
ejson& js = *jss; cstr s;
uint u = calcary_json(p); dbi(u == ~0);
if (u > 0 && u != ~0) {
jss = new ejson[u];
js.emap = (intptr_t)jss;
js.esttr = ej_array | (u & ej_size);
if (*p == '[') p++;
if (*p == '{') {
js.esttr |= ej_json;
do {
s = next_json(p);if (s)*s ='\0';
parse_json(jss++, p, (cstr)"Obj", 1);
p = s + R"(},)"_l;
} while (*p == '{');
return;
}
if (*p == '"') {
js.esttr |= ej_string; p++;
do {
s = strstr(p, R"(",")"); if (s)*s ='\0';
auto& ss = *jss++; ss.esori = (cstr)p;
ss.emap = (iint)(p); ss.ename = "Ary";
ss.esttr = ej_string|(ej_size&(s?(s-p):strlen(p)-1)); // end of '"'
if (s) p = s + R"(",")"_l; else *(p + ss.count()) = '\0';
} while (s);
return;
}
if ((*p >= '0' && *p <= '9') || (*p == '-')) {