-
Notifications
You must be signed in to change notification settings - Fork 13
/
stringhelpers.h
1449 lines (1372 loc) · 56.1 KB
/
stringhelpers.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
/*
* version 0.9.4
* general string-helper functions
* Note: currently, most up to date Version is in live!
*
* only depends on g++:
* -std=c++17 std:: standard headers
* on esyslog (from VDR)
* on "to_chars10.h"
*
* no other dependencies, so it can be easily included in any other header
*
*
*/
#ifndef __STRINGHELPERS_H
#define __STRINGHELPERS_H
#if !defined test_stringhelpers
#include "vdr/tools.h"
#endif
#include "to_chars10.h"
#include <cstdarg>
#include <string>
#include <string_view>
#include <string.h>
#include <vector>
#include <set>
#include <array>
#include <algorithm>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <locale>
#include <iostream>
#include <chrono>
// =========================================================
// =========================================================
// Chapter 0: **************************************
// methods for char *s, make sure that s==NULL is just an empty string
// =========================================================
// =========================================================
inline std::string charPointerToString(const char *s) {
return s?s:std::string();
}
inline std::string charPointerToString(const unsigned char *s) {
return s?reinterpret_cast<const char *>(s):std::string();
}
// challenge:
// method with importing parameter std::string_view called with const char * = nullptr
// undefined behavior, as std::string_view(nullptr) is undefined. In later c++ standard, it is even an abort
// solution:
// a) be very careful, check const char * for nullptr before calling a method with std::string_view as import parameter
// or:
// b) replace all std::string_view with cSv
// very small performance impact if such a method if called with cSv
// this will convert nullptr to empty cSv if called with const char *
// 2nd advantage of cSv: substr(pos) if pos > length: no dump, just an empty cSv as result
class utf8_iterator;
class cSv: public std::string_view {
friend class utf8_iterator;
public:
cSv(): std::string_view() {}
template<std::size_t N> cSv(const char (&s)[N]): std::string_view(s, N-1) {
// std::cout << "cSv const char (&s)[N] " << s << "\n";
}
template<typename T, std::enable_if_t<std::is_same_v<T, const char*>, bool> = true>
cSv(T s): std::string_view(charPointerToStringView(s)) {
// std::cout << "cSv const char *s " << (s?s:"nullptr") << "\n";
}
template<typename T, std::enable_if_t<std::is_same_v<T, char*>, bool> = true>
cSv(T s): std::string_view(charPointerToStringView(s)) {
// std::cout << "cSv char *s " << (s?s:"nullptr") << "\n";
}
cSv(const unsigned char *s): std::string_view(charPointerToStringView(reinterpret_cast<const char *>(s))) {}
cSv(const char *s, size_t l): std::string_view(s, l) {}
cSv(const unsigned char *s, size_t l): std::string_view(reinterpret_cast<const char *>(s), l) {}
cSv(std::string_view sv): std::string_view(sv) {}
cSv(const std::string &s): std::string_view(s) {}
cSv substr(size_t pos) const { return (length() > pos)?cSv(data() + pos, length() - pos):cSv(); }
cSv substr(size_t pos, size_t count) const { return (length() > pos)?cSv(data() + pos, std::min(length() - pos, count) ):cSv(); }
private:
static std::string_view charPointerToStringView(const char *s) {
return s?std::string_view(s, strlen(s)):std::string_view();
}
// =================================================
// ********* utf8 *****************
// =================================================
public:
int utf8CodepointIsValid(size_t pos) const {
// In case of invalid UTF8, return 0
// In case of invalid input, return -1 (pos >= sc.length())
// otherwise, return number of characters for this UTF codepoint
// note: pos + number of characters <= sc.length(), this is checked (otherwise 0 is returned)
if (pos >= length() ) return -1;
static const uint8_t LEN[] = {2,2,2,2,3,3,4,0};
int len = (((*this)[pos] & 0xC0) == 0xC0) * LEN[((*this)[pos] >> 3) & 7] + (((*this)[pos] | 0x7F) == 0x7F);
if (len == 1) return 1;
if (len + pos > length()) return 0;
for (size_t k= pos + 1; k < pos + len; k++) if (((*this)[k] & 0xC0) != 0x80) return 0;
return len;
}
private:
size_t utf8ParseBackwards(size_t pos) const {
// pos <= s.length()! this is not checked
// return position of character before pos
// see also https://stackoverflow.com/questions/22257486/iterate-backwards-through-a-utf8-multibyte-string
for (size_t i = pos; i > 0; ) {
--i;
if (((*this)[i] & 0xC0) != 0x80) return i;
// (s[i] & 0xC0) == 0x80 is true if bit 6 is clear and bit 7 is set
}
return 0;
}
public:
utf8_iterator utf8_begin() const;
utf8_iterator utf8_end() const;
int compareLowerCase(cSv other, const std::locale &loc);
};
inline wint_t Utf8ToUtf32(const char *p, int len);
// iterator for utf8
class utf8_iterator {
const cSv m_sv;
size_t m_pos;
mutable int m_len = -2;
int get_len() const {
if (m_len == -2) m_len = m_sv.utf8CodepointIsValid(m_pos);
return m_len;
}
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = wint_t;
using difference_type = int;
using pointer = const wint_t*;
using reference = const wint_t&;
explicit utf8_iterator(cSv sv, size_t pos): m_sv(sv) {
// note: if pos is not begin/end, pos will be moved back to a valid utf8 start point
// i.e. to an ASCII (bit 7 not set) or and utf8 start byte (bit 6&7 set)
if (pos == 0) { m_pos = 0; return; }
if (pos >= sv.length() ) { m_pos = sv.length(); return; }
// to avoid a position in the middle of utf8:
m_pos = sv.utf8ParseBackwards(pos+1);
}
utf8_iterator& operator++() {
if (m_pos >= m_sv.length() ) return *this;
int l = get_len();
if (l > 0) m_pos += l; else ++m_pos;
m_len = -2;
return *this;
}
utf8_iterator& operator--() {
if (m_pos == 0) return *this;
size_t new_pos = m_sv.utf8ParseBackwards(m_pos);
int new_len = m_sv.utf8CodepointIsValid(new_pos);
if (new_pos + new_len == m_pos || m_pos - new_pos == 1) {
m_pos = new_pos;
m_len = new_len;
} else {
// some invalid UTF8.
--m_pos;
m_len = -2;
}
return *this;
}
bool operator!=(utf8_iterator other) const { return m_pos != other.m_pos; }
bool operator==(utf8_iterator other) const { return m_pos == other.m_pos; }
wint_t codepoint() const {
if (m_pos >= m_sv.length() ) return 0;
int l = get_len();
if (l <= 0) return '?'; // invalid utf8
return Utf8ToUtf32(m_sv.data() + m_pos, l);
}
size_t pos() const {
// note: if this == end(), sv[m_pos] is invalid!!!
return m_pos;
}
const wint_t operator*() const {
return codepoint();
}
};
inline utf8_iterator cSv::utf8_begin() const { return utf8_iterator(*this, 0); }
inline utf8_iterator cSv::utf8_end() const { return utf8_iterator(*this, length() ); }
inline int cSv::compareLowerCase(cSv other, const std::locale &loc) {
// compare strings case-insensitive
utf8_iterator ls = utf8_begin();
utf8_iterator rs = other.utf8_begin();
for (; ls != utf8_end() && rs != other.utf8_end(); ++ls, ++rs) {
wint_t llc = std::tolower<wchar_t>(*ls, loc);
wint_t rlc = std::tolower<wchar_t>(*rs, loc);
if ( llc < rlc ) return -1;
if ( llc > rlc ) return 1;
}
if (rs != other.utf8_end() ) return -1;
if (ls != utf8_end() ) return 1;
return 0;
}
// =========================================================
// cStr: similar to cSv, but support c_str()
// =========================================================
class cStr {
public:
cStr() {}
cStr(const char *s) { if (s) m_s = s; }
cStr(const unsigned char *s) { if (s) m_s = reinterpret_cast<const char *>(s); }
cStr(const std::string &s): m_s(s.c_str()) {}
operator const char*() const { return m_s; }
const char *c_str() const { return m_s; }
char *data() { return (char *)m_s; }
size_t length() const { return strlen(m_s); }
operator cSv() const { return cSv(m_s, strlen(m_s)); }
private:
const char *m_s = "";
};
// =========================================================
// =========================================================
// Chapter 1: UTF8 string utilities ****************
// =========================================================
// =========================================================
inline void stringAppendUtfCodepoint(std::string &target, wint_t codepoint) {
if (codepoint <= 0x7F){
target.push_back( (char) (codepoint) );
return;
}
if (codepoint <= 0x07FF) {
target.push_back( (char) (0xC0 | (codepoint >> 6 ) ) );
target.push_back( (char) (0x80 | (codepoint & 0x3F)) );
return;
}
if (codepoint <= 0xFFFF) {
target.push_back( (char) (0xE0 | ( codepoint >> 12)) );
target.push_back( (char) (0x80 | ((codepoint >> 6) & 0x3F)) );
target.push_back( (char) (0x80 | ( codepoint & 0x3F)) );
return;
}
target.push_back( (char) (0xF0 | ((codepoint >> 18) & 0x07)) );
target.push_back( (char) (0x80 | ((codepoint >> 12) & 0x3F)) );
target.push_back( (char) (0x80 | ((codepoint >> 6) & 0x3F)) );
target.push_back( (char) (0x80 | ( codepoint & 0x3F)) );
return;
}
inline int utf8CodepointIsValid(const char *p) {
// p must be zero terminated
// In case of invalid UTF8, return 0
// otherwise, return number of characters for this UTF codepoint
static const uint8_t LEN[] = {2,2,2,2,3,3,4,0};
int len = ((*p & 0xC0) == 0xC0) * LEN[(*p >> 3) & 7] + ((*p | 0x7F) == 0x7F);
for (int k=1; k < len; k++) if ((p[k] & 0xC0) != 0x80) return 0;
return len;
}
inline wint_t Utf8ToUtf32(const char *p, int len) {
// assumes, that uft8 validity checks have already been done. len must be provided. call utf8CodepointIsValid first
static const uint8_t FF_MSK[] = {0xFF >>0, 0xFF >>0, 0xFF >>3, 0xFF >>4, 0xFF >>5, 0xFF >>0, 0xFF >>0, 0xFF >>0};
wint_t val = *p & FF_MSK[len];
for (int i = 1; i < len; i++) val = (val << 6) | (p[i] & 0x3F);
return val;
}
inline wint_t getNextUtfCodepoint(const char *&p) {
// p must be zero terminated
// get next codepoint, and increment p
// 0 is returned at end of string, and p will point to the end of the string (0)
if(!p || !*p) return 0;
int l = utf8CodepointIsValid(p);
if( l == 0 ) { p++; return '?'; }
wint_t result = Utf8ToUtf32(p, l);
p += l;
return result;
}
// =========================================================
// =========================================================
// Chapter 3: Parse char* / string_view / string
// =========================================================
// =========================================================
// =========================================================
// whitespace ==============================================
// =========================================================
inline bool my_isspace(char c) {
// fastest
return (c == ' ') || (c >= 0x09 && c <= 0x0d);
// (0x09, '\t'), (0x0a, '\n'), (0x0b, '\v'), (0x0c, '\f'), (0x0d, '\r')
}
inline cSv remove_trailing_whitespace(cSv sv) {
// return a string_view with trailing whitespace from sv removed
// for performance: see remove_leading_whitespace
for (size_t i = sv.length(); i > 0; ) {
i = sv.find_last_not_of(' ', i-1);
if (i == std::string_view::npos) return cSv(); // only ' '
if (sv[i] > 0x0d || sv[i] < 0x09) return sv.substr(0, i+1); // non whitespace found at i -> length i+1 !!!
}
return cSv();
}
inline cSv remove_leading_whitespace(cSv sv) {
// return a string_view with leading whitespace from sv removed
// for performance:
// avoid changing sv: cSv &sv is much slower than cSv sv
// don't use std::isspace or isspace: this is really slow ... 0.055 <-> 0.037
// also avoid find_first_not_of(" \t\f\v\n\r";): way too slow ...
// definition of whitespace:
// (0x20, ' '), (0x09, '\t'), (0x0a, '\n'), (0x0b, '\v'), (0x0c, '\f'), (0x0d, '\r')
// or: (c == ' ') || (c >= 0x09 && c <= 0x0d);
// best performance: use find_first_not_of for ' ':
for (size_t i = 0; i < sv.length(); ++i) {
i = sv.find_first_not_of(' ', i);
if (i == std::string_view::npos) return cSv(); // only ' '
if (sv[i] > 0x0d || sv[i] < 0x09) return sv.substr(i); // non whitespace found at i
}
return cSv();
}
// =========================================================
// parse string_view for int
// =========================================================
template<class T> inline T parse_unsigned_internal(cSv sv) {
T val = 0;
for (size_t start = 0; start < sv.length() && std::isdigit(sv[start]); ++start) val = val*10 + (sv[start]-'0');
return val;
}
template<class T> inline T parse_int(cSv sv) {
if (sv.empty() ) return 0;
if (!std::isdigit(sv[0]) && sv[0] != '-') {
sv = remove_leading_whitespace(sv);
if (sv.empty() ) return 0;
}
if (sv[0] != '-') return parse_unsigned_internal<T>(sv);
return -parse_unsigned_internal<T>(sv.substr(1));
}
template<class T> inline T parse_unsigned(cSv sv) {
if (sv.empty() ) return 0;
if (!std::isdigit(sv[0])) sv = remove_leading_whitespace(sv);
return parse_unsigned_internal<T>(sv);
}
template<class T> inline T parse_hex(cSv sv, size_t *num_digits = 0) {
static const signed char hex_values[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
T value = 0;
const unsigned char *data = reinterpret_cast<const unsigned char *>(sv.data());
const unsigned char *data_e = data + sv.length();
for (; data < data_e; ++data) {
signed char val = hex_values[*data];
if (val == -1) break;
value = value*16 + val;
}
if (num_digits) *num_digits = data - reinterpret_cast<const unsigned char *>(sv.data());
return value;
}
// =========================================================
// split string at delimiter in two parts
// =========================================================
inline bool splitString(cSv str, cSv delim, size_t minLengh, cSv &first, cSv &second) {
// true if delim is part of str, and length of first & second >= minLengh
for (std::size_t found = str.find(delim); found != std::string::npos; found = str.find(delim, found + 1)) {
cSv first_guess = remove_trailing_whitespace(str.substr(0, found));
if (first_guess.length() >= minLengh) {
// we found the first part. Is the second part long enough?
cSv second_guess = remove_leading_whitespace(str.substr(found + delim.length()));
if (second_guess.length() < minLengh) return false; // nothing found
first = first_guess;
second = second_guess;
return true;
}
}
return false; // nothing found
}
inline cSv SecondPart(cSv str, cSv delim, size_t minLengh) {
// return second part of split string if delim is part of str, and length of first & second >= minLengh
// otherwise, return ""
cSv first, second;
if (splitString(str, delim, minLengh, first, second)) return second;
else return cSv();
}
inline cSv SecondPart(cSv str, cSv delim) {
// if delim is not in str, return ""
// Otherwise, return part of str after first occurrence of delim
// remove leading blanks from result
size_t found = str.find(delim);
if (found == std::string::npos) return cSv();
std::size_t ssnd;
for(ssnd = found + delim.length(); ssnd < str.length() && str[ssnd] == ' '; ssnd++);
return str.substr(ssnd);
}
// =========================================================
// =========================================================
// Chapter 4: convert data to cSv:
// cToSv classes, with buffer containing text representation of data
// =========================================================
// =========================================================
// =========================================================
// integer and hex
// =========================================================
namespace stringhelpers_internal {
// ==== itoaN ===================================================================
// itoaN: Template for fixed number of characters, left fill with 0
// note: i must fit in N digits, this is not checked!
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N == 0, char*>::type itoaN(char *b, T i) {
return b;
}
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N == 1, char*>::type itoaN(char *b, T i) {
b[0] = i + '0';
return b+N;
}
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N == 2, char*>::type itoaN(char *b, T i) {
memcpy(b, to_chars10_internal::digits_100 + (i << 1), 2);
return b+N;
}
// max uint16_t 65535
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N == 3 || N == 4, char*>::type itoaN(char *b, T i) {
uint16_t q = ((uint32_t)i * 5243U) >> 19; // q = i/100; i < 43699
memcpy(b+N-2, to_chars10_internal::digits_100 + (((uint16_t)i - q*100) << 1), 2);
itoaN<N-2>(b, q);
return b+N;
}
// max uint32_t 4294967295
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N >= 5 && N <= 9, char*>::type itoaN(char *b, T i) {
uint32_t q = (uint32_t)i/100;
memcpy(b+N-2, to_chars10_internal::digits_100 + (((uint32_t)i - q*100) << 1), 2);
itoaN<N-2>(b, q);
return b+N;
}
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N >= 10 && N != 18, char*>::type itoaN(char *b, T i) {
T q = i/100000000;
b = itoaN<N-8>(b, q);
return itoaN<8>(b, i - q*100000000);
}
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N == 18, char*>::type itoaN(char *b, T i) {
T q = i/1000000000;
b = itoaN<N-9>(b, q);
return itoaN<9>(b, i - q*1000000000);
}
// ==== powN ===============================
template<uint8_t N>
inline typename std::enable_if<N == 0, uint64_t>::type powN() { return 1; }
template<uint8_t N>
inline typename std::enable_if<N <= 19 && N >= 1, uint64_t>::type powN() {
// return 10^N
return powN<N-1>() * 10;
}
// ==== itoa_min_width =====================
template<size_t N, typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
inline typename std::enable_if<N == 0, char*>::type itoa_min_width(char *b, T i) {
return to_chars10_internal::itoa(b, i);
}
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N >= 1 && N <= 19, char*>::type itoa_min_width(char *b, T i) {
if (i < powN<N>() ) return itoaN<N, T>(b, i);
T q = i/powN<N>();
b = to_chars10_internal::itoa(b, q);
return itoaN<N, T>(b, i - q*powN<N>() );
}
template<size_t N, typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline typename std::enable_if<N >= 20, char*>::type itoa_min_width(char *b, T i) {
// i < 10^20 is always true
memset(b, '0', N-20);
b += N-20;
return itoaN<20, T>(b, i);
}
template<size_t N, typename T, std::enable_if_t<std::is_signed_v<T>, bool> = true>
inline typename std::enable_if<N >= 1, char*>::type itoa_min_width(char *b, T i) {
typedef std::make_unsigned_t<T> TU;
if (i >= 0) return itoa_min_width<N, TU>(b, (TU)i);
*b = '-';
return itoa_min_width<N-1, TU>(b + 1, ~(TU(i)) + (TU)1);
}
// ==== addCharsHex ========================
template<typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline T addCharsHex(char *buffer, size_t num_chars, T value) {
// sizeof(buffer) must be >= num_chars. This is not checked !!!
// value must be >= 0. This is not checked !!!
// value is written with num_chars chars
// if value is too small -> left values filled with 0
// if value is too high -> the highest numbers are not written. This is not checked!
// but, you can check: if the returned value is != 0, some chars are not written
const char *hex_chars = "0123456789ABCDEF";
for (char *be = buffer + num_chars -1; be >= buffer; --be, value /= 16) *be = hex_chars[value%16];
return value;
}
}
class cToSv {
public:
cToSv() {}
// not intended for copy
// you can copy the cSv of this class (from operator cSv() )
cToSv(const cToSv&) = delete;
cToSv &operator= (const cToSv &) = delete;
// deleting this is good :)
// don't try to implement! Otherwise, users will expect something like
// a = a.substr(0,3);
// and similar to work. Which is possible, implementing lost's of different cases.
// it's just not worth the offert. For normal =, users can write
// a = a.erase(0).append(...)
virtual ~cToSv() {}
virtual operator cSv() const = 0;
};
inline std::ostream& operator<<(std::ostream& os, cToSv const& sv )
{
return os << cSv(sv);
}
template<std::size_t N>
class cToSvHex: public cToSv {
public:
template<typename T>
cToSvHex(const T &value) { *this << value; }
template<typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
cToSvHex &operator<<(T value) {
stringhelpers_internal::addCharsHex(m_buffer, N, value);
return *this;
}
operator cSv() const { return cSv(m_buffer, N); }
char m_buffer[N];
protected:
cToSvHex() { }
};
// read files
class cOpen {
public:
cOpen(const char *pathname, int flags) {
if (!pathname) return;
m_fd = open(pathname, flags);
checkError(pathname, errno);
}
cOpen(const char *pathname, int flags, mode_t mode) {
if (!pathname) return;
m_fd = open(pathname, flags, mode);
checkError(pathname, errno);
}
operator int() const { return m_fd; }
bool exists() const { return m_fd != -1; }
~cOpen() {
if (m_fd != -1) close(m_fd);
}
private:
void checkError(const char *pathname, int errno_l) {
if (m_fd == -1) {
// no message for errno == ENOENT, the file just does not exist
if (errno_l != ENOENT) esyslog("cOpen::checkError, ERROR: open fails, errno %d, filename %s\n", errno_l, pathname);
}
}
int m_fd = -1;
};
class cToSvFile: public cToSv {
public:
cToSvFile(cStr filename, size_t max_length = 0) { load(filename, max_length ); }
operator cSv() const { return m_result; }
char *data() { return m_s?m_s:m_empty; } // Is zero terminated
const char *c_str() const { return m_s?m_s:m_empty; } // Is zero terminated
operator cStr() const { return m_s; }
bool exists() const { return m_exists; }
~cToSvFile() { std::free(m_s); }
private:
void load(const char *filename, size_t max_length) {
for (int n_err = 0; n_err < 3; ++n_err) {
if (load_int(filename, max_length) ) return;
m_exists = false;
std::free(m_s);
m_s = nullptr;
if (n_err < 3) sleep(1);
}
esyslog("cToSvFile::load, ERROR: give up after 3 tries, filename %s", filename);
}
bool load_int(const char *filename, size_t max_length) {
// return false if an error occurred, and we should try again
cOpen fd(filename, O_RDONLY);
if (!fd.exists()) return true;
struct stat buffer;
if (fstat(fd, &buffer) != 0) {
if (errno == ENOENT) return false; // somehow strange, cOpen found the file, and fstat says it does not exist ... we try again
esyslog("cToSvFile::load, ERROR: in fstat, errno %d, filename %s\n", errno, filename);
return true;
}
// file exists, length buffer.st_size
m_exists = true;
if (buffer.st_size == 0) return true; // empty file
size_t length = buffer.st_size;
if (max_length != 0 && length > max_length) length = max_length;
m_s = (char *) malloc((length + 1) * sizeof(char)); // add one. So we can add the 0 string terminator
if (!m_s) {
esyslog("cToSvFile::load, ERROR out of memory, filename = %s, requested size = %zu\n", filename, length + 1);
return true;
}
size_t num_read = 0;
ssize_t num_read1 = 1;
for (int num_errors = 0; num_errors < 3 && num_read < length; num_read += num_read1) {
num_read1 = read(fd, m_s + num_read, length - num_read);
if (num_read1 == 0) ++num_errors; // should not happen, because fstat reported file size >= length
if (num_read1 == -1) {
if (errno == ENOENT) return false;
esyslog("cToSvFile::load, ERROR: read failed, errno %d, filename %s, file size = %zu, num_read = %zu\n", errno, filename, (size_t)buffer.st_size, num_read);
++num_errors;
num_read1 = 0;
if (num_errors < 3) sleep(1);
}
}
m_result = cSv(m_s, num_read);
m_s[num_read] = 0; // so data() returns a 0 terminated string
if (num_read != length) {
esyslog("cToSvFile::load, ERROR: num_read = %zu, length = %zu, filename %s\n", num_read, length, filename);
}
return true;
}
bool m_exists = false;
char *m_s = nullptr;
cSv m_result;
char m_empty[1] = "";
};
template<std::size_t N> class cToSvFileN: public cToSv {
// read up to N bytes from file
public:
cToSvFileN(cStr filename) { load(filename); }
operator cSv() const { return m_result; }
char *data() { return m_s; } // Is zero terminated
const char *c_str() { return m_s; } // Is zero terminated
operator cStr() const { return m_s; }
bool exists() const { return m_exists; }
private:
void load(const char *filename) {
cOpen fd(filename, O_RDONLY);
if (!fd.exists()) return;
m_exists = true;
ssize_t num_read = read(fd, m_s, N);
if (num_read == -1) {
esyslog("cToSvFile::load, ERROR: read fails, errno %d, filename %s\n", errno, filename);
num_read = 0;
}
m_result = cSv(m_s, num_read);
m_s[num_read] = 0; // so data returns a 0 terminated string
}
bool m_exists = false;
char m_s[N+1] = "";
cSv m_result;
};
// =========================================================
// cToSvConcat =============================================
// =========================================================
// N: number of bytes in buffer on stack
template<size_t N = 255>
class cToSvConcat: public cToSv {
public:
template<typename... Args> cToSvConcat(Args&&... args) {
concat(std::forward<Args>(args)...);
}
cToSvConcat &concat() { return *this; }
template<typename T, typename... Args>
cToSvConcat &concat(T &&n, Args&&... args) {
*this << n;
return concat(std::forward<Args>(args)...);
}
template<typename T>
cToSvConcat &operator+=(T &&n) { return *this << n; }
// ========================
// overloads for concat
cToSvConcat &operator<<(char ch) {
if (m_pos_for_append == m_be_data) ensure_free(1);
*(m_pos_for_append++) = ch;
return *this;
}
cToSvConcat &operator<<(cSv sv) { return append(sv); }
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
cToSvConcat &operator<<(T i) {
if (!to_chars10_internal::to_chars10_range_check(m_pos_for_append, m_be_data, i)) ensure_free(20);
m_pos_for_append = to_chars10_internal::itoa(m_pos_for_append, i);
return *this;
}
// ========================
// overloads for append. Should be compatible to std::string.append(...)
// ========================
cToSvConcat &append(cSv sv) {
if (sv.empty() ) return *this; // this check is required: documentation of std::memcpy: If either dest or src is an invalid or null pointer, the behavior is undefined, even if count is zero.
if (m_pos_for_append + sv.length() > m_be_data) ensure_free(sv.length() );
memcpy(m_pos_for_append, sv.data(), sv.length());
m_pos_for_append += sv.length();
return *this;
}
cToSvConcat &append(const char *s, size_t len) {
return append(cSv(s, len));
}
cToSvConcat &append(size_t count, char ch) {
if (m_pos_for_append + count > m_be_data) ensure_free(count);
memset(m_pos_for_append, ch, count);
m_pos_for_append += count;
return *this;
}
// =======================
// special appends
// =======================
// =======================
// appendInt: append integer (with some format options)
template<size_t M, typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
cToSvConcat &appendInt(T i) {
// append integer with min widh M. Left fill with 0, if required.
if (m_pos_for_append + std::max(M, (size_t)20) > m_be_data) ensure_free(std::max(M, (size_t)20));
m_pos_for_append = stringhelpers_internal::itoa_min_width<M, T>(m_pos_for_append, i);
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
cToSvConcat &appendHex(T value, int width = sizeof(T)*2) {
if (m_pos_for_append + width > m_be_data) ensure_free(width);
stringhelpers_internal::addCharsHex(m_pos_for_append, width, value);
m_pos_for_append += width;
return *this;
}
template<typename T, std::enable_if_t<sizeof(T) == 16, bool> = true>
cToSvConcat &appendHex(T value) {
*this << value;
return *this;
}
// =======================
// append_utf8: append utf8 codepoint
cToSvConcat &append_utf8(wint_t codepoint) {
if (m_pos_for_append + 4 > m_be_data) ensure_free(4);
if (codepoint <= 0x7F) {
*(m_pos_for_append++) = (char) (codepoint);
return *this;
}
if (codepoint <= 0x07FF) {
*(m_pos_for_append++) =( (char) (0xC0 | (codepoint >> 6 ) ) );
*(m_pos_for_append++) =( (char) (0x80 | (codepoint & 0x3F)) );
return *this;
}
if (codepoint <= 0xFFFF) {
*(m_pos_for_append++) =( (char) (0xE0 | ( codepoint >> 12)) );
*(m_pos_for_append++) =( (char) (0x80 | ((codepoint >> 6) & 0x3F)) );
*(m_pos_for_append++) =( (char) (0x80 | ( codepoint & 0x3F)) );
return *this;
}
*(m_pos_for_append++) =( (char) (0xF0 | ((codepoint >> 18) & 0x07)) );
*(m_pos_for_append++) =( (char) (0x80 | ((codepoint >> 12) & 0x3F)) );
*(m_pos_for_append++) =( (char) (0x80 | ((codepoint >> 6) & 0x3F)) );
*(m_pos_for_append++) =( (char) (0x80 | ( codepoint & 0x3F)) );
return *this;
}
// =======================
// appendToLower
cToSvConcat &appendToLower(cSv sv, const std::locale &loc) {
for (auto it = sv.utf8_begin(); it != sv.utf8_end(); ++it) {
append_utf8(std::tolower<wchar_t>(*it, loc));
}
return *this;
}
// apend text. Before appending, replace all occurrences of substring with replacement
cToSvConcat &appendReplace(cSv text, cSv substring, cSv replacement) {
size_t pos = 0, found;
while ( (found = text.find(substring, pos)) != std::string_view::npos) {
append(text.data()+pos, found-pos);
append(replacement);
pos = found + substring.length();
}
append(text.data()+pos, text.length()-pos);
return *this;
}
// Replaces the characters in the range [begin() + pos, begin() + std::min(pos + count, size())) with sv
cToSvConcat &replace(size_t pos, size_t count, cSv sv) {
if (pos >= length() ) return append(sv);
if (pos + count >= length() ) { m_pos_for_append = m_buffer + pos; return append(sv); }
if (sv.length() != count) {
if (sv.length() > count) ensure_free(sv.length() - count);
memmove(m_buffer+pos+sv.length(), m_buffer+pos+count, length() - (pos+count));
m_pos_for_append += sv.length() - count;
}
memcpy(m_buffer+pos, sv.data(), sv.length() );
return *this;
}
// Replaces all occurrences of substring after pos with replacement
cToSvConcat &replaceAll(cSv substring, cSv replacement, size_t pos = 0) {
while ( (pos = cSv(*this).find(substring, pos)) != std::string_view::npos) {
replace(pos, substring.length(), replacement);
pos += replacement.length();
}
return *this;
}
// =======================
// appendFormated append formatted
// __attribute__ ((format (printf, 2, 3))) can not be used, but should work starting with GCC 13.1
template<typename... Args> cToSvConcat &appendFormated(const char *fmt, Args&&... args) {
int needed = snprintf(m_pos_for_append, m_be_data - m_pos_for_append, fmt, std::forward<Args>(args)...);
if (needed < 0) {
esyslog("live: ERROR, cToScConcat::appendFormated needed = %d, fmt = %s", needed, fmt);
return *this; // error in snprintf
}
if (needed < m_be_data - m_pos_for_append) {
m_pos_for_append += needed;
return *this;
}
ensure_free(needed + 1);
needed = sprintf(m_pos_for_append, fmt, std::forward<Args>(args)...);
if (needed < 0) {
esyslog("live: ERROR, cToScConcat::appendFormated needed (2) = %d, fmt = %s", needed, fmt);
return *this; // error in sprintf
}
m_pos_for_append += needed;
return *this;
}
// =======================
// appendDateTime: append date/time formatted with strftime
cToSvConcat &appendDateTime(cStr fmt, const std::tm *tp) {
size_t needed = std::strftime(m_pos_for_append, m_be_data - m_pos_for_append, fmt.c_str(), tp);
if (needed == 0) {
ensure_free(1024);
needed = std::strftime(m_pos_for_append, m_be_data - m_pos_for_append, fmt.c_str(), tp);
if (needed == 0) {
esyslog("live: ERROR, cToSvConcat::appendDateTime needed = 0, fmt = %s", fmt.c_str());
return *this; // we did not expect to need more than 1024 chars for the formatted time ...
}
}
m_pos_for_append += needed;
return *this;
}
cToSvConcat &appendDateTime(cStr fmt, time_t time) {
if (!time) return *this;
struct std::tm tm_r;
if (localtime_r( &time, &tm_r ) == 0 ) {
esyslog("live: ERROR, cToSvConcat::appendDateTime localtime_r = 0, fmt = %s, time = %lld", fmt.c_str(), (long long)time);
return *this;
}
return appendDateTime(fmt, &tm_r);
}
// =======================
// appendUrlEscaped
cToSvConcat &appendUrlEscaped(cSv sv) {
const char* reserved = " !#$&'()*+,/:;=?@[]\"<>\n\r\t\\%";
// in addition to the reserved URI charaters as defined here https://en.wikipedia.org/wiki/Percent-encoding
// also escape html characters \"<>\n\r so no additional html-escaping is required
// \ is escaped for easy use in strings where \ has a special meaning
for (size_t pos = 0; pos < sv.length(); ++pos) {
char c = sv[pos];
if (strchr(reserved, c)) {
concat('%');
appendHex((unsigned char)c, 2);
} else if ((unsigned char)c < ' ' || c == 127) {
concat('?'); // replace control characters with ?
} else {
int l = sv.utf8CodepointIsValid(pos);
if (l == 0) concat('?'); // invalid utf
else {
append(sv.data() + pos, l);
pos += l-1;
}
}
}
return *this;
}
// ========================
// get data
operator cSv() const { return cSv(m_buffer, m_pos_for_append-m_buffer); }
char *data() const { *m_pos_for_append = 0; return m_buffer; }
size_t length() const { return m_pos_for_append-m_buffer; }
char *begin() const { return m_buffer; }
char *end() const { return m_pos_for_append; }
const char *c_str() const { *m_pos_for_append = 0; return m_buffer; }
char operator[](size_t i) const { return *(m_buffer + i); }
operator cStr() const { return this->c_str(); }
// ========================
// others
bool empty() const { return m_buffer == m_pos_for_append; }
void clear() { m_pos_for_append = m_buffer; }
cToSvConcat &erase(size_t index = 0) {
m_pos_for_append = std::min(m_pos_for_append, m_buffer + index);
return *this;
}
cToSvConcat &erase(size_t index, size_t count) {
size_t l_length = length();
if ((index >= l_length) | (count == 0) ) return *this;
if (index + count >= l_length) {
m_pos_for_append = m_buffer + index;
} else {
memmove(m_buffer+index, m_buffer+index + count, l_length - index - count);
m_pos_for_append -= count;
}
return *this;
}
void reserve(size_t r) const { m_reserve = r; }
~cToSvConcat() {
if (m_buffer_allocated) free (m_buffer_allocated);
}
private:
void ensure_free(size_t l) {
// make sure that l bytes can we written at m_pos_for_append
if (m_pos_for_append + l <= m_be_data) return;
size_t current_length = length();
size_t new_buffer_size = std::max(2*current_length + l + 200, m_reserve);
if (!m_buffer_allocated) {
m_buffer_allocated = (char *) std::malloc(new_buffer_size);
if (!m_buffer_allocated) throw std::bad_alloc();
memcpy(m_buffer_allocated, m_buffer_static, current_length);
} else {
m_buffer_allocated = (char *) std::realloc(m_buffer_allocated, new_buffer_size);
if (!m_buffer_allocated) throw std::bad_alloc();
}
m_be_data = m_buffer_allocated + new_buffer_size - 1;
m_buffer = m_buffer_allocated;
m_pos_for_append = m_buffer + current_length;
}
char m_buffer_static[N+1];
char *m_buffer_allocated = nullptr;
char *m_buffer = m_buffer_static;
protected:
char *m_pos_for_append = m_buffer;
char *m_be_data = m_buffer + sizeof(m_buffer_static) - 1; // [m_buffer, m_be_data) is available for data.
// It must be possible to write the 0 terminator to m_be_data: *m_be_data = 0.
// m_pos_for_append <= m_be_data: must be always ensured.
// m_be_data - m_pos_for_append: Number of bytes available for write
private:
mutable size_t m_reserve = 1024;
};
template<size_t N=0>
class cToSvInt: public cToSvConcat<std::max(N, (size_t)20)> {
public:
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
cToSvInt(T i) {
this->m_pos_for_append = stringhelpers_internal::itoa_min_width<N>(this->m_pos_for_append, i);
}
};
template<std::size_t N = 255>
class cToSvToLower: public cToSvConcat<N> {
public:
cToSvToLower(cSv sv, const std::locale &loc) {
this->reserve(sv.length() + 5);
this->appendToLower(sv, loc);
}
};
template<std::size_t N = 255>
class cToSvFormated: public cToSvConcat<N> {
public:
// __attribute__ ((format (printf, 2, 3))) can not be used, but should work starting with GCC 13.1
template<typename... Args> cToSvFormated(const char *fmt, Args&&... args) {
this->appendFormated(fmt, std::forward<Args>(args)...);
}
};
class cToSvDateTime: public cToSvConcat<255> {
public:
cToSvDateTime(cStr fmt, time_t time) {
this->appendDateTime(fmt, time);
}