-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStr.h
More file actions
1159 lines (1004 loc) · 57.3 KB
/
Str.h
File metadata and controls
1159 lines (1004 loc) · 57.3 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
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
#pragma once
#include "Common.h"
#include <sstream>
// Windows headers interfere with these definitions
#undef LoadString
// Forward declarations
class CStr8;
class CStrW;
class CStr16;
//#################################################################################################
class CStr8 final
{
private:
friend class CStrW;
friend class CStr16;
public:
enum EPrintfType : uint8_t
{
EPT_Printf = 0
};
enum EFormatType : uint8_t
{
EFT_Format = 0
};
static const size_t not_found;
static const size_t end;
// Constructs an empty string object
CStr8(void);
// Copy and move constructors
CStr8(const CStr8 &src);
CStr8(CStr8 &&src) noexcept;
// Constructs a string object set to the string/character
explicit CStr8(const CStrW &str);
explicit CStr8(const CStr16 &str);
explicit CStr8(const std::string &str);
explicit CStr8(const std::wstring &str);
explicit CStr8(PCSTR sz);
explicit CStr8(PCWSTR sz);
explicit CStr8(const char ch);
explicit CStr8(const wchar_t ch);
// Constructs a string object set to a substring, given the starting location and length
CStr8(const CStr8 &str, const size_t nStart, const size_t nLen = end);
CStr8(PCSTR sz, const size_t nStart, const size_t nLen = end);
// Constructs a formatted string using printf formatting; set 'eIgnored' to 'EPT_Printf' and 'szFormat' contains the string with format specifiers
CStr8(const EPrintfType eIgnored, PCSTR szFormat, ...);
// Constructs a formatted string using C++ variadic templates; set 'eIgnored' to 'EFT_Format' and 'szFormat' contains the string with format specifiers
template<typename ...ARGS>
CStr8(const EFormatType eIgnored, PCSTR szFormat, const ARGS&... args)
: m_sz((PSTR)g_szNull8),
m_nBufLen(0),
m_nStrLen(0)
{
UNUSED(eIgnored);
*this = VFormat(szFormat, args...);
}
~CStr8(void);
// Attaches to an existing null-terminated string buffer; buffer should have been allocated using 'malloc'
CStr8 &Attach(PSTR sz, const size_t nBufLen);
// Releases the internal string buffer, the caller is responsible for freeing the buffer with 'free'
PSTR Detach(void) noexcept;
// Overwrites the memory with all zeros to securely erase the string
void SecureErase(void);
// Returns the length in characters
size_t GetLength(const bool bIncludeNullTerm = false) const noexcept;
// Returns the count of characters (which may be different from length in UTF8 strings)
size_t GetCount(const bool bIncludeNullTerm = false) const;
// Returns the size in bytes
size_t GetSize(const bool bIncludeNullTerm = false) const noexcept;
// Returns whether the string is empty
bool IsEmpty(void) const noexcept;
// Empties the string, optionally freeing the memory
void Empty(const bool bFree = false);
// Returns the first character in the string, or '\0' if the string is empty
char GetFirst(void) const;
// Returns the last character in the string, or '\0' if the string is empty
char GetLast(void) const;
// Returns the character at the given index, or '\0' if the index is beyond the string
char GetAt(const size_t nIndex) const;
// Sets the character at the given index, so long as index is not beyond the string
void SetAt(const size_t nIndex, const char ch);
// Returns a pointer to the raw string data
operator PCSTR(void) const noexcept;
// Returns a UTF8 copy of the string
CStr8 AsUtf8(void) const;
// Returns a wchar_t copy of the string
CStrW AsWide(void) const;
CStrW AsNative(void) const;
// Copy and move assignment operators
CStr8 &operator=(const CStr8 &src);
CStr8 &operator=(CStr8 &&src) noexcept;
// Assigns the string object to the string/character
CStr8 &operator=(const CStrW &str);
CStr8 &operator=(const CStr16 &str);
CStr8 &operator=(const std::string &str);
CStr8 &operator=(const std::wstring &str);
CStr8 &operator=(PCSTR sz);
CStr8 &operator=(PCWSTR sz);
CStr8 &operator=(const char ch);
CStr8 &operator=(const wchar_t ch);
// Assigns the string object to the string/character
void Assign(const CStr8 &str);
void Assign(const CStrW &str);
void Assign(const CStr16 &str);
void Assign(const std::string &str);
void Assign(const std::wstring &str);
void Assign(PCSTR sz);
void Assign(PCWSTR sz);
void Assign(const char ch);
void Assign(const wchar_t ch);
// Assigns the string object to a substring, given the starting location and length
void Assign(const CStr8 &str, const size_t nStart, const size_t nLen = end);
void Assign(const CStrW &str, const size_t nStart, const size_t nLen = end);
void Assign(PCSTR sz, const size_t nStart, const size_t nLen = end);
void Assign(PCWSTR sz, const size_t nStart, const size_t nLen = end);
// Appends the string/character
CStr8 &operator+=(const CStr8 &str);
CStr8 &operator+=(const CStrW &str);
CStr8 &operator+=(PCSTR sz);
CStr8 &operator+=(PCWSTR sz);
CStr8 &operator+=(const char ch);
CStr8 &operator+=(const wchar_t ch);
// Prepends the string/character
void Prepend(const CStr8 &str);
void Prepend(const CStrW &str);
void Prepend(PCSTR sz);
void Prepend(PCWSTR sz);
void Prepend(const char ch);
void Prepend(const wchar_t ch);
// Prepends a substring, given the starting location and length
void Prepend(const CStr8 &str, const size_t nStart, const size_t nLen = end);
void Prepend(const CStrW &str, const size_t nStart, const size_t nLen = end);
void Prepend(PCSTR sz, const size_t nStart, const size_t nLen = end);
void Prepend(PCWSTR sz, const size_t nStart, const size_t nLen = end);
// Appends the string/character
void Append(const CStr8 &str);
void Append(const CStrW &str);
void Append(PCSTR sz);
void Append(PCWSTR sz);
void Append(const char ch);
void Append(const wchar_t ch);
// Appends a substring, given the starting location and length
void Append(const CStr8 &str, const size_t nStart, const size_t nLen = end);
void Append(const CStrW &str, const size_t nStart, const size_t nLen = end);
void Append(PCSTR sz, const size_t nStart, const size_t nLen = end);
void Append(PCWSTR sz, const size_t nStart, const size_t nLen = end);
friend std::ostream &operator<<(std::ostream &stream, const CStr8 &str);
// Tests for equal (case sensitive)
bool operator==(const CStr8 &str) const;
bool operator==(PCSTR sz) const;
bool operator==(const char ch) const;
// Tests for not equal (case sensitive)
bool operator!=(const CStr8 &str) const;
bool operator!=(PCSTR sz) const;
bool operator!=(const char ch) const;
// Tests for less than (case sensitive)
bool operator<(const CStr8 &str) const;
bool operator<(PCSTR sz) const;
bool operator<(const char ch) const;
// Tests for less than or equal (case sensitive)
bool operator<=(const CStr8 &str) const;
bool operator<=(PCSTR sz) const;
bool operator<=(const char ch) const;
// Tests for greater than (case sensitive)
bool operator>(const CStr8 &str) const;
bool operator>(PCSTR sz) const;
bool operator>(const char ch) const;
// Tests for greater than or equal (case sensitive)
bool operator>=(const CStr8 &str) const;
bool operator>=(PCSTR sz) const;
bool operator>=(const char ch) const;
// Compares the string object against the string/character, optionally case insensitive
int Compare(const CStr8 &str, const bool bCaseInsensitive = false) const;
int Compare(PCSTR sz, const bool bCaseInsensitive = false) const;
int Compare(const char ch, const bool bCaseInsensitive = false) const;
// Compares strings with embedded numbers, optionally case insensitive
int CompareAlphaNum(const CStr8 &str, const bool bCaseInsensitive = false) const;
// Compares a substring of the string object against the entire string/character, given the starting location and length, optionally case insensitive
int CompareMid(const CStr8 &str, const size_t nStart, const size_t nCount = end, const bool bCaseInsensitive = false) const;
int CompareMid(PCSTR sz, const size_t nStart, const size_t nCount = end, const bool bCaseInsensitive = false) const;
int CompareMid(const char ch, const size_t nStart, const bool bCaseInsensitive = false) const;
// Compares the leftmost characters of the string object against the string/character, up to 'nCount' characters, optionally case insensitive
int CompareLeft(const CStr8 &str, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareLeft(PCSTR sz, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareLeft(const char ch, const bool bCaseInsensitive = false) const;
// Compares the rightmost characters of the string object against the string/character, up to 'nCount' characters, optionally case insensitive
int CompareRight(const CStr8 &str, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareRight(PCSTR sz, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareRight(const char ch, const bool bCaseInsensitive = false) const;
// Returns the count of instances of a given character in the string object, optionally case insensitive
size_t Count(const char chFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the count of characters that the string object and provided string match starting at the left, optionally case insensitive
size_t CountCompare(const CStr8 &str, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t CountCompare(PCSTR sz, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the count of characters that the string object and provided string match starting at the right, optionally case insensitive
size_t ReverseCountCompare(const CStr8 &str, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t ReverseCountCompare(PCSTR sz, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Tests if the string/character is in the string object given the starting index, optionally case insensitive
bool Contains(const CStr8 &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
bool Contains(PCSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
bool Contains(const char chFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Tests for any instance of any character in the provided string given the starting index, optionally case insensitive
bool ContainsOneOf(const CStr8 &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
bool ContainsOneOf(PCSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the first instance of the string/character in the string object given the starting index, optionally case insensitive; returns 'not_found' if the string/character is not in the string object
size_t Find(const CStr8 &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t Find(PCSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t Find(const char chFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the last instance of the string/character in the string object given the starting index and working backwards, optionally case insensitive; returns 'not_found' if the string/character is not in the string object
size_t ReverseFind(const CStr8 &strFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t ReverseFind(PCSTR szFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t ReverseFind(const char chFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Returns the index of the first instance of any character in the provided string given the starting index, optionally case insensitive; returns 'not_found' if none of the characters in the string are in the string object
size_t FindFirstOf(const CStr8 &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t FindFirstOf(PCSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the first instance of any character not in the provided string given the starting index, optionally case insensitive; returns 'not_found' if all of the characters in the string object are in the provided string
size_t FindFirstNotOf(const CStr8 &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t FindFirstNotOf(PCSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the last instance of any character in the provided string given the starting index and working backwards, optionally case insensitive; returns 'not_found' if none of the characters in the string are in the string object
size_t FindLastOf(const CStr8 &strFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t FindLastOf(PCSTR szFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Returns the index of the last instance of any character not in the provided string given the starting index and working backwards, optionally case insensitive; returns 'not_found' if all of the characters in the string object are in the provided string
size_t FindLastNotOf(const CStr8 &strFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t FindLastNotOf(PCSTR szFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Returns a substring of the string object starting at the given index and going until the first character not found in the provided string, optionally case insensitive
CStr8 SpanIncluding(const CStr8 &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
CStr8 SpanIncluding(PCSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns a substring of the string object starting at the given index and going until the first character found in the provided string, optionally case insensitive
CStr8 SpanExcluding(const CStr8 &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
CStr8 SpanExcluding(PCSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Inserts the string/character at the given index, if the index is beyond the string the string/character is appended; returns the new string length
size_t Insert(const size_t nIndex, const CStr8 &strInsert);
size_t Insert(const size_t nIndex, PCSTR szInsert);
size_t Insert(const size_t nIndex, const char chInsert);
// Removes 'nCount' characters from the string object starting at the given index; returns the new string length
size_t Delete(const size_t nIndex, const size_t nCount = 1);
// Removes all instances of the string/character from the string object, optionally case insensitive; returns the number removed
size_t Remove(const CStr8 &strRemove, const bool bCaseInsensitive = false);
size_t Remove(PCSTR szRemove, const bool bCaseInsensitive = false);
size_t Remove(const char chRemove, const bool bCaseInsensitive = false);
// Removes any and all characters in the string from the string object, optionally case insensitive; returns the number removed
size_t RemoveAllOf(const CStr8 &strRemove, const bool bCaseInsensitive = false);
size_t RemoveAllOf(PCSTR szRemove, const bool bCaseInsensitive = false);
// Keeps any and all characters in the string from the string object, optionally case insensitive; returns the number kept
size_t KeepAllOf(const CStr8 &strKeep, const bool bCaseInsensitive = false);
size_t KeepAllOf(PCSTR szKeep, const bool bCaseInsensitive = false);
// Replaces all instances of the string/character in the string object with the new string/character, optionally case insensitive; returns the number replaced
size_t Replace(const CStr8 &strOld, const CStr8 &strNew, const bool bCaseInsensitive = false);
size_t Replace(PCSTR szOld, PCSTR szNew, const bool bCaseInsensitive = false);
size_t Replace(const char chOld, const char chNew, const bool bCaseInsensitive = false);
// Efficiently swap two strings without allocating new buffers
void Swap(CStr8 &src) noexcept;
// Returns a substring from the string object given the starting index and length
CStr8 GetMid(const size_t nStart, const size_t nCount = end) const;
// Returns a substring containing 'nCount' characters from the left of the string object
CStr8 GetLeft(const size_t nCount) const;
// Returns a substring containing 'nCount' characters from the right of the string object
CStr8 GetRight(const size_t nCount) const;
// Removes any and all characters in the provided string/character from both the left and right of the string object, optionally case insensitive
CStr8 &Trim(const CStr8 &strTrim, const bool bCaseInsensitive = false);
CStr8 &Trim(PCSTR szTrim, const bool bCaseInsensitive = false);
CStr8 &Trim(const char chTrim, const bool bCaseInsensitive = false);
// Removes any whitespace characters from both the left and right of the string object
CStr8 &Trim(void);
// Removes any and all characters in the provided string/character from the left of the string object, optionally case insensitive
CStr8 &TrimLeft(const CStr8 &strTrim, const bool bCaseInsensitive = false);
CStr8 &TrimLeft(PCSTR szTrim, const bool bCaseInsensitive = false);
CStr8 &TrimLeft(const char chTrim, const bool bCaseInsensitive = false);
// Removes any whitespace characters from the left of the string object
CStr8 &TrimLeft(void);
// Removes any and all characters in the provided string/character from the right of the string object, optionally case insensitive
CStr8 &TrimRight(const CStr8 &strTrim, const bool bCaseInsensitive = false);
CStr8 &TrimRight(PCSTR szTrim, const bool bCaseInsensitive = false);
CStr8 &TrimRight(const char chTrim, const bool bCaseInsensitive = false);
// Removes any whitespace characters from the right of the string object
CStr8 &TrimRight(void);
// Removes all characters from the string object except the middle 'nCount' characters beginning at 'nStart'
CStr8 &CropMid(const size_t nStart, const size_t nCount = end);
// Removes all characters from the string object except the leftmost 'nCount' characters
CStr8 &CropLeft(const size_t nCount);
// Removes all characters from the string object except the rightmost 'nCount' characters
CStr8 &CropRight(const size_t nCount);
// Returns an uppercase copy of the string
CStr8 GetUpper(void) const;
// Returns a lowercase copy of the string
CStr8 GetLower(void) const;
// Converts all characters in the string object to uppercase
CStr8 &MakeUpper(void);
// Converts all characters in the string object to lowercase
CStr8 &MakeLower(void);
// Reverses all characters in the string front to back
CStr8 &MakeReverse(void);
// Returns a substring of the string object starting at the given index and going until the first character in the provided string/character is found, optionally case insensitive
CStr8 Tokenize(const CStr8 &strTokens, size_t &nStart, const bool bCaseInsensitive = false) const;
CStr8 Tokenize(PCSTR szTokens, size_t &nStart, const bool bCaseInsensitive = false) const;
CStr8 Tokenize(const char chToken, size_t &nStart, const bool bCaseInsensitive = false) const;
// Formats the string using standard printf syntax
bool Printf(PCSTR szFormat, ...);
bool PrintfV(PCSTR szFormat, va_list vaArgs);
// Formats the string using variadic templates
template<typename ...ARGS>
void Format(PCSTR szFormat, const ARGS&... args)
{
*this = VFormat(szFormat, args...);
}
// Ensures the buffer is at least 'nBufLen' in length
bool ReserveBuffer(const size_t nBufLen);
// Returns the length of the buffer in characters
size_t GetBufferLength(void) const noexcept;
// Returns the size of the buffer in bytes
size_t GetBufferSize(void) const noexcept;
// Returns a pointer to the raw string data for direct buffer manipulation, ensuring the buffer is at least 'nMinBufLen' in length
PSTR GetBuffer(const size_t nMinBufLen = 0);
// Updates the string object after a call to GetBuffer(), and setting the null-terminator to 'nNewStrLen' or calculating the string length if 'end'
void ReleaseBuffer(const size_t nNewStrLen = end);
// Allocates a new string buffer only as long as the current string object, thus freeing any extra unused buffer
void FreeExtra(void);
void DebugPrint(const bool bAppendNewline = true, const bool bConsolePrint = false) const;
private:
PSTR m_sz; // String buffer
size_t m_nBufLen; // Length of the buffer in characters
size_t m_nStrLen; // Length of the string in characters
bool Alloc(const size_t nBufLen);
void Dealloc(void);
static size_t GetLength(PCSTR sz, const size_t nMaxLen);
static PSTR FindStrPtr(PCSTR sz, const size_t nStrLen, PCSTR szFind, const size_t nFindLen);
static PSTR FindStrPtrI(PCSTR sz, const size_t nStrLen, PCSTR szFind, const size_t nFindLen);
static size_t FindStrIndex(PCSTR sz, const size_t nStrLen, PCSTR szFind, const size_t nFindLen);
static size_t FindStrIndexI(PCSTR sz, const size_t nStrLen, PCSTR szFind, const size_t nFindLen);
static PCSTR memchri(PCSTR sz, const char ch, const size_t nLen);
static int memcmpi(PCSTR sz1, PCSTR sz2, const size_t nLen);
static size_t str2int(PCSTR sz);
template<typename T>
static std::string ToString(const T &val)
{
std::ostringstream stream;
stream << val;
return stream.str();
}
// VFormat is a function that provides a safe C++ alternative to C printf. It uses C++11 variadic templates
// The argument position is surrounded by curly braces. Extra curly braces are ignored, as are invalid positions inside the curly braces.
// Examples
// VFormat("Name: {0}, Age: {1}", "Brian", 42); "Name: Brian, Age: 42"
// VFormat("{1} == {0}", 3.14, "pi"); "pi == 3.14"
// VFormat("Test {{0}}", 15); "Test {15}"
// VFormat("String {a} {2}", 3); "String {a} {2}"
template<typename ...ARGS>
static CStr8 VFormat(PCSTR szFormat, const ARGS&... args)
{
std::ostringstream stream;
const std::string arrArgs[] = {ToString(args)...};
size_t nArgsCount = sizeof...(args);
PCSTR szScan = szFormat;
PCSTR szEnd = szScan + GetLength(szFormat, end);
while(szScan != szEnd)
{
if(*szScan == '{')
{
bool bValidIndex = true;
PCSTR szOpen = szScan;
PCSTR szLeft = ++szScan;
while(szScan != szEnd)
{
if(*szScan == '}')
break;
if(*szScan < '0' || *szScan > '9')
{
bValidIndex = false;
break;
}
++szScan;
}
PCSTR szRight = szScan;
if(szScan == szEnd || *szScan != '}' || szLeft == szRight)
bValidIndex = false;
if(bValidIndex)
{
CStr8 strIndex(szLeft, 0, szRight - szLeft);
size_t nIndex = str2int(strIndex);
if(nIndex < nArgsCount)
stream << arrArgs[nIndex];
else
{
stream << CStr8(szOpen, 0, szScan - szOpen);
continue;
}
}
else
{
stream << CStr8(szOpen, 0, szScan - szOpen);
continue;
}
}
else
stream << *szScan;
++szScan;
}
return CStr8(stream.str());
}
};
//#################################################################################################
class CStrW final
{
private:
friend class CStr8;
friend class CStr16;
public:
enum EPrintfType : uint8_t
{
EPT_Printf = 0
};
enum EFormatType : uint8_t
{
EFT_Format = 0
};
static const size_t not_found;
static const size_t end;
// Constructs an empty string object
CStrW(void);
// Copy and move constructors
CStrW(const CStrW &src);
CStrW(CStrW &&src) noexcept;
// Constructs a string object set to the string/character
explicit CStrW(const CStr8 &str);
explicit CStrW(const CStr16 &str);
explicit CStrW(const std::wstring &str);
explicit CStrW(const std::string &str);
explicit CStrW(PCWSTR sz);
explicit CStrW(PCSTR sz);
explicit CStrW(const wchar_t ch);
explicit CStrW(const char ch);
// Constructs a string object set to a substring, given the starting location and length
CStrW(const CStrW &str, const size_t nStart, const size_t nLen = end);
CStrW(PCWSTR sz, const size_t nStart, const size_t nLen = end);
// Constructs a formatted string using printf formatting; set 'eIgnored' to 'EPT_Printf' and 'szFormat' contains the string with format specifiers
CStrW(const EPrintfType eIgnored, PCWSTR szFormat, ...);
// Constructs a formatted string using C++ variadic templates; set 'eIgnored' to 'EFT_Format' and 'szFormat' contains the string with format specifiers
template<typename ...ARGS>
CStrW(const EFormatType eIgnored, PCWSTR szFormat, const ARGS&... args)
: m_sz((PWSTR)g_szNullW),
m_nBufLen(0),
m_nStrLen(0)
{
UNUSED(eIgnored);
*this = VFormat(szFormat, args...);
}
enum ELoadType : uint8_t
{
ELT_Load = 0
};
// Constructs a string by loading it from the string resources table for the current application module
CStrW(const ELoadType eIgnored, const int nId);
~CStrW(void);
// Attaches to an existing null-terminated string buffer; buffer should have been allocated using 'malloc'
CStrW &Attach(PWSTR sz, const size_t nBufLen);
// Releases the internal string buffer, the caller is responsible for freeing the buffer with 'free'
PWSTR Detach(void) noexcept;
// Overwrites the memory with all zeros to securely erase the string
void SecureErase(void);
// Returns the length in characters
size_t GetLength(const bool bIncludeNullTerm = false) const noexcept;
// Returns the count of characters
size_t GetCount(const bool bIncludeNullTerm = false) const noexcept;
// Returns the size in bytes
size_t GetSize(const bool bIncludeNullTerm = false) const noexcept;
// Returns whether the string is empty
bool IsEmpty(void) const noexcept;
// Empties the string, optionally freeing the memory
void Empty(const bool bFree = false);
// Returns the first character in the string, or '\0' if the string is empty
wchar_t GetFirst(void) const;
// Returns the last character in the string, or '\0' if the string is empty
wchar_t GetLast(void) const;
// Returns the character at the given index, or '\0' if the index is beyond the string
wchar_t GetAt(const size_t nIndex) const;
// Sets the character at the given index, so long as index is not beyond the string
void SetAt(const size_t nIndex, const wchar_t ch);
// Returns a pointer to the raw string data
operator PCWSTR(void) const noexcept;
// Returns a UTF8 copy of the string
CStr8 AsUtf8(void) const;
// Returns a wchar_t copy of the string
CStrW AsWide(void) const;
CStrW AsNative(void) const;
// Copy and move assignment operators
CStrW &operator=(const CStrW &src);
CStrW &operator=(CStrW &&src) noexcept;
// Assigns the string object to the string/character
CStrW &operator=(const CStr8 &str);
CStrW &operator=(const CStr16 &str);
CStrW &operator=(const std::wstring &str);
CStrW &operator=(const std::string &str);
CStrW &operator=(PCWSTR sz);
CStrW &operator=(PCSTR sz);
CStrW &operator=(const wchar_t ch);
CStrW &operator=(const char ch);
// Assigns the string object to the string/character
void Assign(const CStrW &str);
void Assign(const CStr8 &str);
void Assign(const CStr16 &str);
void Assign(const std::wstring &str);
void Assign(const std::string &str);
void Assign(PCWSTR sz);
void Assign(PCSTR sz);
void Assign(const wchar_t ch);
void Assign(const char ch);
// Assigns the string object to a substring, given the starting location and length
void Assign(const CStrW &str, const size_t nStart, const size_t nLen = end);
void Assign(const CStr8 &str, const size_t nStart, const size_t nLen = end);
void Assign(PCWSTR sz, const size_t nStart, const size_t nLen = end);
void Assign(PCSTR sz, const size_t nStart, const size_t nLen = end);
// Appends the string/character
CStrW &operator+=(const CStrW &str);
CStrW &operator+=(const CStr8 &str);
CStrW &operator+=(PCWSTR sz);
CStrW &operator+=(PCSTR sz);
CStrW &operator+=(const wchar_t ch);
CStrW &operator+=(const char ch);
// Prepends the string/character
void Prepend(const CStrW &str);
void Prepend(const CStr8 &str);
void Prepend(PCWSTR sz);
void Prepend(PCSTR sz);
void Prepend(const wchar_t ch);
void Prepend(const char ch);
// Prepends a substring, given the starting location and length
void Prepend(const CStrW &str, const size_t nStart, const size_t nLen = end);
void Prepend(const CStr8 &str, const size_t nStart, const size_t nLen = end);
void Prepend(PCWSTR sz, const size_t nStart, const size_t nLen = end);
void Prepend(PCSTR sz, const size_t nStart, const size_t nLen = end);
// Appends the string/character
void Append(const CStrW &str);
void Append(const CStr8 &str);
void Append(PCWSTR sz);
void Append(PCSTR sz);
void Append(const wchar_t ch);
void Append(const char ch);
// Appends a substring, given the starting location and length
void Append(const CStrW &str, const size_t nStart, const size_t nLen = end);
void Append(const CStr8 &str, const size_t nStart, const size_t nLen = end);
void Append(PCWSTR sz, const size_t nStart, const size_t nLen = end);
void Append(PCSTR sz, const size_t nStart, const size_t nLen = end);
// Returns the concatenation of two strings/characters
friend CStrW operator+(const CStrW &str1, const CStrW &str2);
friend CStrW operator+(const CStrW &str1, const CStr8 &str2);
friend CStrW operator+(const CStr8 &str1, const CStrW &str2);
friend CStrW operator+(const CStr8 &str1, const CStr8 &str2);
friend CStrW operator+(const CStrW &str1, PCWSTR sz2);
friend CStrW operator+(const CStr8 &str1, PCWSTR sz2);
friend CStrW operator+(const CStrW &str1, PCSTR sz2);
friend CStrW operator+(const CStr8 &str1, PCSTR sz2);
friend CStrW operator+(const CStrW &str1, const wchar_t ch2);
friend CStrW operator+(const CStr8 &str1, const wchar_t ch2);
friend CStrW operator+(const CStrW &str1, const char ch2);
friend CStrW operator+(const CStr8 &str1, const char ch2);
friend CStrW operator+(PCWSTR sz1, const CStrW &str2);
friend CStrW operator+(PCWSTR sz1, const CStr8 &str2);
friend CStrW operator+(PCSTR sz1, const CStrW &str2);
friend CStrW operator+(PCSTR sz1, const CStr8 &str2);
friend CStrW operator+(const wchar_t ch1, const CStrW &str2);
friend CStrW operator+(const wchar_t ch1, const CStr8 &str2);
friend CStrW operator+(const char ch1, const CStrW &str2);
friend CStrW operator+(const char ch1, const CStr8 &str2);
friend std::wostream &operator<<(std::wostream &stream, const CStrW &str);
// Tests for equal (case sensitive)
bool operator==(const CStrW &str) const;
bool operator==(PCWSTR sz) const;
bool operator==(const wchar_t ch) const;
// Tests for not equal (case sensitive)
bool operator!=(const CStrW &str) const;
bool operator!=(PCWSTR sz) const;
bool operator!=(const wchar_t ch) const;
// Tests for less than (case sensitive)
bool operator<(const CStrW &str) const;
bool operator<(PCWSTR sz) const;
bool operator<(const wchar_t ch) const;
// Tests for less than or equal (case sensitive)
bool operator<=(const CStrW &str) const;
bool operator<=(PCWSTR sz) const;
bool operator<=(const wchar_t ch) const;
// Tests for greater than (case sensitive)
bool operator>(const CStrW &str) const;
bool operator>(PCWSTR sz) const;
bool operator>(const wchar_t ch) const;
// Tests for greater than or equal (case sensitive)
bool operator>=(const CStrW &str) const;
bool operator>=(PCWSTR sz) const;
bool operator>=(const wchar_t ch) const;
// Compares the string object against the string/character, optionally case insensitive
int Compare(const CStrW &str, const bool bCaseInsensitive = false) const;
int Compare(PCWSTR sz, const bool bCaseInsensitive = false) const;
int Compare(const wchar_t ch, const bool bCaseInsensitive = false) const;
// Compares strings with embedded numbers, optionally case insensitive
int CompareAlphaNum(const CStrW &str, const bool bCaseInsensitive = false) const;
// Compares a substring of the string object against the entire string/character, given the starting location and length, optionally case insensitive
int CompareMid(const CStrW &str, const size_t nStart, const size_t nCount = end, const bool bCaseInsensitive = false) const;
int CompareMid(PCWSTR sz, const size_t nStart, const size_t nCount = end, const bool bCaseInsensitive = false) const;
int CompareMid(const wchar_t ch, const size_t nStart, const bool bCaseInsensitive = false) const;
// Compares the leftmost characters of the string object against the string/character, up to 'nCount' characters, optionally case insensitive
int CompareLeft(const CStrW &str, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareLeft(PCWSTR sz, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareLeft(const wchar_t ch, const bool bCaseInsensitive = false) const;
// Compares the rightmost characters of the string object against the string/character, up to 'nCount' characters, optionally case insensitive
int CompareRight(const CStrW &str, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareRight(PCWSTR sz, const size_t nCount, const bool bCaseInsensitive = false) const;
int CompareRight(const wchar_t ch, const bool bCaseInsensitive = false) const;
// Returns the count of instances of a given character in the string object, optionally case insensitive
size_t Count(const wchar_t chFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the count of characters that the string object and provided string match starting at the left, optionally case insensitive
size_t CountCompare(const CStrW &str, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t CountCompare(PCWSTR sz, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the count of characters that the string object and provided string match starting at the right, optionally case insensitive
size_t ReverseCountCompare(const CStrW &str, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t ReverseCountCompare(PCWSTR sz, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Tests if the string/character is in the string object given the starting index, optionally case insensitive
bool Contains(const CStrW &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
bool Contains(PCWSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
bool Contains(const wchar_t chFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Tests for any instance of any character in the provided string given the starting index, optionally case insensitive
bool ContainsOneOf(const CStrW &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
bool ContainsOneOf(PCWSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the first instance of the string/character in the string object given the starting index, optionally case insensitive; returns 'not_found' if the string/character is not in the string object
size_t Find(const CStrW &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t Find(PCWSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t Find(const wchar_t chFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the last instance of the string/character in the string object given the starting index and working backwards, optionally case insensitive; returns 'not_found' if the string/character is not in the string object
size_t ReverseFind(const CStrW &strFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t ReverseFind(PCWSTR szFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t ReverseFind(const wchar_t chFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Returns the index of the first instance of any character in the provided string given the starting index, optionally case insensitive; returns 'not_found' if none of the characters in the string are in the string object
size_t FindFirstOf(const CStrW &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t FindFirstOf(PCWSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the first instance of any character not in the provided string given the starting index, optionally case insensitive; returns 'not_found' if all of the characters in the string object are in the provided string
size_t FindFirstNotOf(const CStrW &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
size_t FindFirstNotOf(PCWSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns the index of the last instance of any character in the provided string given the starting index and working backwards, optionally case insensitive; returns 'not_found' if none of the characters in the string are in the string object
size_t FindLastOf(const CStrW &strFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t FindLastOf(PCWSTR szFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Returns the index of the last instance of any character not in the provided string given the starting index and working backwards, optionally case insensitive; returns 'not_found' if all of the characters in the string object are in the provided string
size_t FindLastNotOf(const CStrW &strFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
size_t FindLastNotOf(PCWSTR szFind, const size_t nStart = end, const bool bCaseInsensitive = false) const;
// Returns a substring of the string object starting at the given index and going until the first character not found in the provided string, optionally case insensitive
CStrW SpanIncluding(const CStrW &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
CStrW SpanIncluding(PCWSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Returns a substring of the string object starting at the given index and going until the first character found in the provided string, optionally case insensitive
CStrW SpanExcluding(const CStrW &strFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
CStrW SpanExcluding(PCWSTR szFind, const size_t nStart = 0, const bool bCaseInsensitive = false) const;
// Inserts the string/character at the given index, if the index is beyond the string the string/character is appended; returns the new string length
size_t Insert(const size_t nIndex, const CStrW &strInsert);
size_t Insert(const size_t nIndex, PCWSTR szInsert);
size_t Insert(const size_t nIndex, const wchar_t chInsert);
// Removes 'nCount' characters from the string object starting at the given index; returns the new string length
size_t Delete(const size_t nIndex, const size_t nCount = 1);
// Removes all instances of the string/character from the string object, optionally case insensitive; returns the number removed
size_t Remove(const CStrW &strRemove, const bool bCaseInsensitive = false);
size_t Remove(PCWSTR szRemove, const bool bCaseInsensitive = false);
size_t Remove(const wchar_t chRemove, const bool bCaseInsensitive = false);
// Removes any and all characters in the string from the string object, optionally case insensitive; returns the number removed
size_t RemoveAllOf(const CStrW &strRemove, const bool bCaseInsensitive = false);
size_t RemoveAllOf(PCWSTR szRemove, const bool bCaseInsensitive = false);
// Keeps any and all characters in the string from the string object, optionally case insensitive; returns the number kept
size_t KeepAllOf(const CStrW &strKeep, const bool bCaseInsensitive = false);
size_t KeepAllOf(PCWSTR szKeep, const bool bCaseInsensitive = false);
// Replaces all instances of the string/character in the string object with the new string/character, optionally case insensitive; returns the number replaced
size_t Replace(const CStrW &strOld, const CStrW &strNew, const bool bCaseInsensitive = false);
size_t Replace(PCWSTR szOld, PCWSTR szNew, const bool bCaseInsensitive = false);
size_t Replace(const wchar_t chOld, const wchar_t chNew, const bool bCaseInsensitive = false);
// Efficiently swap two strings without allocating new buffers
void Swap(CStrW &src) noexcept;
// Returns a substring from the string object given the starting index and length
CStrW GetMid(const size_t nStart, const size_t nCount = end) const;
// Returns a substring containing 'nCount' characters from the left of the string object
CStrW GetLeft(const size_t nCount) const;
// Returns a substring containing 'nCount' characters from the right of the string object
CStrW GetRight(const size_t nCount) const;
// Removes any and all characters in the provided string/character from both the left and right of the string object, optionally case insensitive
CStrW &Trim(const CStrW &strTrim, const bool bCaseInsensitive = false);
CStrW &Trim(PCWSTR szTrim, const bool bCaseInsensitive = false);
CStrW &Trim(const wchar_t chTrim, const bool bCaseInsensitive = false);
// Removes any whitespace characters from both the left and right of the string object
CStrW &Trim(void);
// Removes any and all characters in the provided string/character from the left of the string object, optionally case insensitive
CStrW &TrimLeft(const CStrW &strTrim, const bool bCaseInsensitive = false);
CStrW &TrimLeft(PCWSTR szTrim, const bool bCaseInsensitive = false);
CStrW &TrimLeft(const wchar_t chTrim, const bool bCaseInsensitive = false);
// Removes any whitespace characters from the left of the string object
CStrW &TrimLeft(void);
// Removes any and all characters in the provided string/character from the right of the string object, optionally case insensitive
CStrW &TrimRight(const CStrW &strTrim, const bool bCaseInsensitive = false);
CStrW &TrimRight(PCWSTR szTrim, const bool bCaseInsensitive = false);
CStrW &TrimRight(const wchar_t chTrim, const bool bCaseInsensitive = false);
// Removes any whitespace characters from the right of the string object
CStrW &TrimRight(void);
// Removes all characters from the string object except the middle 'nCount' characters beginning at 'nStart'
CStrW &CropMid(const size_t nStart, const size_t nCount = end);
// Removes all characters from the string object except the leftmost 'nCount' characters
CStrW &CropLeft(const size_t nCount);
// Removes all characters from the string object except the rightmost 'nCount' characters
CStrW &CropRight(const size_t nCount);
// Returns an uppercase copy of the string
CStrW GetUpper(void) const;
// Returns a lowercase copy of the string
CStrW GetLower(void) const;
// Converts all characters in the string object to uppercase
CStrW &MakeUpper(void);
// Converts all characters in the string object to lowercase
CStrW &MakeLower(void);
// Reverses all characters in the string front to back
CStrW &MakeReverse(void);
// Returns a substring of the string object starting at the given index and going until the first character in the provided string/character is found, optionally case insensitive
CStrW Tokenize(const CStrW &strTokens, size_t &nStart, const bool bCaseInsensitive = false) const;
CStrW Tokenize(PCWSTR szTokens, size_t &nStart, const bool bCaseInsensitive = false) const;
CStrW Tokenize(const wchar_t chToken, size_t &nStart, const bool bCaseInsensitive = false) const;
bool LoadString(const UINT nId);
// Formats the string using standard printf syntax
bool Printf(PCWSTR szFormat, ...);
bool PrintfV(PCWSTR szFormat, va_list vaArgs);
// Formats the string using variadic templates
template<typename ...ARGS>
void Format(PCWSTR szFormat, const ARGS&... args)
{
*this = VFormat(szFormat, args...);
}
// Ensures the buffer is at least 'nBufLen' in length
bool ReserveBuffer(const size_t nBufLen);
// Returns the length of the buffer in characters
size_t GetBufferLength(void) const noexcept;
// Returns the size of the buffer in bytes
size_t GetBufferSize(void) const noexcept;
// Returns a pointer to the raw string data for direct buffer manipulation, ensuring the buffer is at least 'nMinBufLen' in length
PWSTR GetBuffer(const size_t nMinBufLen = 0);
// Updates the string object after a call to GetBuffer(), and setting the null-terminator to 'nNewStrLen' or calculating the string length if 'end'
void ReleaseBuffer(const size_t nNewStrLen = end);
// Allocates a new string buffer only as long as the current string object, thus freeing any extra unused buffer
void FreeExtra(void);
void DebugPrint(const bool bAppendNewline = true, const bool bConsolePrint = false) const;
private:
PWSTR m_sz; // String buffer
size_t m_nBufLen; // Length of the buffer in characters
size_t m_nStrLen; // Length of the string in characters
bool Alloc(const size_t nBufLen);
void Dealloc(void);
static size_t GetLength(PCWSTR sz, const size_t nMaxLen);
static PWSTR FindStrPtr(PCWSTR sz, const size_t nStrLen, PCWSTR szFind, const size_t nFindLen);
static PWSTR FindStrPtrI(PCWSTR sz, const size_t nStrLen, PCWSTR szFind, const size_t nFindLen);
static size_t FindStrIndex(PCWSTR sz, const size_t nStrLen, PCWSTR szFind, const size_t nFindLen);
static size_t FindStrIndexI(PCWSTR sz, const size_t nStrLen, PCWSTR szFind, const size_t nFindLen);
static PCWSTR wmemchri(PCWSTR sz, const wchar_t ch, const size_t nLen);
static int wmemcmpi(PCWSTR sz1, PCWSTR sz2, const size_t nLen);
static size_t str2int(PCWSTR sz);
template<typename T>
static std::wstring ToString(const T &val)
{
std::wostringstream stream;
stream << val;
return stream.str();
}
// VFormat is a function that provides a safe C++ alternative to C printf. It uses C++11 variadic templates
// The argument position is surrounded by curly braces. Extra curly braces are ignored, as are invalid positions inside the curly braces.
// Examples
// VFormat("Name: {0}, Age: {1}", "Brian", 42); "Name: Brian, Age: 42"
// VFormat("{1} == {0}", 3.14, "pi"); "pi == 3.14"
// VFormat("Test {{0}}", 15); "Test {15}"
// VFormat("String {a} {2}", 3); "String {a} {2}"
template<typename ...ARGS>
static CStrW VFormat(PCWSTR szFormat, const ARGS&... args)
{
std::wostringstream stream;
const std::wstring arrArgs[] = {ToString(args)...};
size_t nArgsCount = sizeof...(args);
PCWSTR szScan = szFormat;
PCWSTR szEnd = szScan + GetLength(szFormat, end);
while(szScan != szEnd)
{
if(*szScan == L'{')
{
bool bValidIndex = true;
PCWSTR szOpen = szScan;
PCWSTR szLeft = ++szScan;
while(szScan != szEnd)
{
if(*szScan == L'}')
break;
if(*szScan < L'0' || *szScan > L'9')
{
bValidIndex = false;
break;
}
++szScan;
}
PCWSTR szRight = szScan;
if(szScan == szEnd || *szScan != L'}' || szLeft == szRight)
bValidIndex = false;
if(bValidIndex)
{
CStrW strIndex(szLeft, 0, szRight - szLeft);
size_t nIndex = str2int(strIndex);
if(nIndex < nArgsCount)
stream << arrArgs[nIndex];
else
{
stream << CStrW(szOpen, 0, szScan - szOpen);
continue;
}
}
else
{
stream << CStrW(szOpen, 0, szScan - szOpen);
continue;
}
}
else
stream << *szScan;
++szScan;
}
return CStrW(stream.str());
}
};
//#################################################################################################
class CStr16 final
{
private:
friend class CStr8;
friend class CStrW;
public:
static const size_t not_found;
static const size_t end;
// Constructs an empty string object
CStr16(void);
// Copy and move constructors
CStr16(const CStr16 &src);
CStr16(CStr16 &&src) noexcept;
// Constructs a string object set to the string/character
explicit CStr16(const CStr8 &str);
explicit CStr16(const CStrW &str);
explicit CStr16(const char16_t *sz);
explicit CStr16(const char16_t ch);
// Constructs a string object set to a substring, given the starting location and length
CStr16(const CStr16 &str, const size_t nStart, const size_t nLen = end);
CStr16(const char16_t *sz, const size_t nStart, const size_t nLen = end);
~CStr16(void);
// Attaches to an existing null-terminated string buffer; buffer should have been allocated using 'malloc'
CStr16 &Attach(char16_t *sz, const size_t nBufLen);
// Releases the internal string buffer, the caller is responsible for freeing the buffer with 'free'
char16_t *Detach(void) noexcept;
// Overwrites the memory with all zeros to securely erase the string
void SecureErase(void);
// Returns the length in characters
size_t GetLength(const bool bIncludeNullTerm = false) const noexcept;
// Returns the count of characters
size_t GetCount(const bool bIncludeNullTerm = false) const noexcept;
// Returns the size in bytes
size_t GetSize(const bool bIncludeNullTerm = false) const noexcept;
// Returns whether the string is empty
bool IsEmpty(void) const noexcept;
// Empties the string, optionally freeing the memory
void Empty(const bool bFree = false);
// Returns the first character in the string, or '\0' if the string is empty
char16_t GetFirst(void) const;
// Returns the last character in the string, or '\0' if the string is empty
char16_t GetLast(void) const;
// Returns the character at the given index, or '\0' if the index is beyond the string
char16_t GetAt(const size_t nIndex) const;
// Sets the character at the given index, so long as index is not beyond the string
void SetAt(const size_t nIndex, const char16_t ch);
// Returns a pointer to the raw string data
operator const char16_t*(void) const noexcept;
// Returns a UTF8 copy of the string
CStr8 AsUtf8(void) const;
// Returns a wchar_t copy of the string
CStrW AsWide(void) const;
CStrW AsNative(void) const;