-
Notifications
You must be signed in to change notification settings - Fork 5
/
LBPString.cpp
2636 lines (2077 loc) · 49.7 KB
/
LBPString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
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
#include "stdafx.h"
#include "LBPString.h"
#include "LBPLibUtilities.h"
#include "LBP_CRC32.h"
//#define LBP_STRING_STATISTICS
#ifdef LBP_STRING_STATISTICS
static unsigned int g_iCount = 0;
static size_t g_TotalAllocated = 0;
#define INCREMENT_COUNT g_iCount++;
#else
#define INCREMENT_COUNT
#endif
wchar_t* CLBPString::m_wszEmpty = L"";
CLBPString::CLBPString()
:
m_wsz(m_wszEmpty),
m_dwLengthCache(0)
{
INCREMENT_COUNT
}
CLBPString::~CLBPString()
{
#ifdef LBP_STRING_STATISTICS
g_iCount--;
g_TotalAllocated -= m_dwSizeAllocation;
#endif
#ifdef _DEBUG
//Deliberately demolish the contents of the string to make sure
//out of scope errors are easy to see
if(!IsEmpty())
{
const size_t dwWideSize = wcslen(m_wsz);
for(size_t i=0;i<dwWideSize;i++)
{
m_wsz[i] = L'#';
}
m_wsz[dwWideSize] = 0;
}
if(m_sz)
{
const size_t dwMBCSSize = strlen(m_sz);
FillMemory(m_sz, dwMBCSSize, '#');
}
#endif
if (m_wszEmpty != m_wsz && NULL != m_wsz)
{
LBPFREE(m_wsz);
}
if (m_sz)
{
LBPFREE(m_sz);
}
}
CLBPString::CLBPString(const CLBPString& stringSrc)
{
INCREMENT_COUNT
*this = stringSrc;
}
CLBPString::CLBPString(const char* sz)
{
INCREMENT_COUNT
*this = sz;
}
CLBPString::CLBPString(const wchar_t* wsz)
{
INCREMENT_COUNT
*this = wsz;
}
CLBPString::CLBPString(wchar_t wch)
{
INCREMENT_COUNT
*this = wch;
}
#ifdef _NATIVE_WCHAR_T_DEFINED
CLBPString::CLBPString(const USHORT* wsz)
{
INCREMENT_COUNT
*this = wsz;
}
CLBPString::CLBPString(USHORT wch)
{
INCREMENT_COUNT
*this = wch;
}
CLBPString::CLBPString(const USHORT* wsz, DWORD dwCharacters)
{
INCREMENT_COUNT
Allocate(dwCharacters);
wcsncpy(m_wsz, (const wchar_t*)wsz, dwCharacters);
m_wsz[dwCharacters] = 0;
m_dwLengthCache = dwCharacters;
}
#endif
CLBPString::CLBPString(char ch)
{
INCREMENT_COUNT
*this = ch;
}
CLBPString::CLBPString(const wchar_t* wsz, DWORD dwCharacters)
{
INCREMENT_COUNT
Allocate(dwCharacters);
wcsncpy(m_wsz, wsz, dwCharacters);
m_wsz[dwCharacters] = 0;
m_dwLengthCache = dwCharacters; // John
}
void LBP_SetStringTestOptimizationsOn(void)
{
//g_bOptimizeStringGetLength = true;
}
DWORD CLBPString::CRC() const
{
if(IsEmpty())
return 0x1234;
return CLBP_CRC32::Calculate(m_wsz, GetLength() * sizeof(wchar_t));
}
char CLBPString::GetAtMultibyte(int nIndex) const
{
return ConvertWideCharToMBCSChar(m_wsz[nIndex]);
}
void CLBPString::SetAt(int nIndex, char ch)
{
SetAt(nIndex, ConvertMBCSCharToWideChar(ch));
}
const char* CLBPString::AsMBCSString() const
{
SyncMBCS();
return m_sz;
}
const char* CLBPString::Mbcs() const
{
SyncMBCS();
return m_sz;
}
wchar_t* CLBPString::AsNonConstWideString() const
{
return const_cast<wchar_t*>(AsWideString());
}
char* CLBPString::AsNonConstMBCSString() const
{
return const_cast<char*>(AsMBCSString());
}
#ifdef _UNICODE
wchar_t* CLBPString::AsNonConstTString() const
{
return AsNonConstWideString();
}
const wchar_t* CLBPString::AsTString() const
{
return AsWideString();
}
const wchar_t* CLBPString::T() const
{
return AsWideString();
}
#else
char* CLBPString::AsNonConstTString() const
{
return AsNonConstMBCSString();
}
const char* CLBPString::AsTString() const
{
return AsMBCSString();
}
const char* CLBPString::T() const
{
return AsMBCSString();
}
#endif
bool CLBPString::operator==(const wchar_t* wsz) const
{
return 0==CompareNoCase(wsz);
}
bool CLBPString::operator==(const char* sz) const
{
return 0==CompareNoCase(sz);
}
bool CLBPString::operator!=(const wchar_t* wsz) const
{
return !(*this==wsz);
}
bool CLBPString::operator!=(const char* sz) const
{
return !(*this==sz);
}
bool CLBPString::operator<(const CLBPString& s2) const
{
return (CompareNoCase(s2.Wide()) < 0) ? true : false;
}
bool CLBPString::operator>(const CLBPString& s2) const
{
return (CompareNoCase(s2.Wide()) > 0) ? true : false;
}
bool CLBPString::operator<=(const CLBPString& s2) const
{
return (CompareNoCase(s2.Wide()) <= 0) ? true : false;
}
bool CLBPString::operator>=(const CLBPString& s2) const
{
return (CompareNoCase(s2.Wide()) >= 0) ? true : false;
}
bool AFXAPI operator == (const wchar_t * wsz, const CLBPString& s)
{
return 0 == s.CompareNoCase(wsz);
}
bool AFXAPI operator != (const wchar_t * wsz, const CLBPString& s)
{
return 0 != s.CompareNoCase(wsz);
}
bool AFXAPI operator == (const char * wsz, const CLBPString& s)
{
return 0 == s.CompareNoCase(wsz);
}
bool AFXAPI operator != (const char * wsz, const CLBPString& s)
{
return 0 != s.CompareNoCase(wsz);
}
// overloaded assignment
/*const CLBPString& CLBPString::operator=(const CLBPString& stringSrc) const
{
m_wsz = stringSrc.m_wsz;
m_dwLengthCache = stringSrc.m_dwLengthCache;
m_sz = stringSrc.m_sz;
m_dwSizeAllocation = 0;
}*/
const CLBPString& CLBPString::operator=(const CLBPString& stringSrc)
{
//This is an optimization - we can't put the m_wszEmpty check in the header
//because it won't link, but I don't want to un-inline the IsEmpty call. This is the function
//that most needs the optimization - see also operator=(const wchar_t*)
if(stringSrc.m_wszEmpty == stringSrc.m_wsz || stringSrc.IsEmpty())
{
Empty();
return *this;
}
//Optimization for return by value
if (stringSrc.m_bAllowAutoReleaseOwnership)
{
const int iLength = stringSrc.GetLength();
wchar_t* p = const_cast<CLBPString&>(stringSrc).ReleaseOwnership();
TakeOwnershipOfBuffer(p, iLength);
//TakeOwnershipOfBuffer(p);
m_dwLengthCache = iLength;
}
else
{
const DWORD dwLen = stringSrc.GetLength()+1;
Allocate(dwLen);
memmove(m_wsz, stringSrc.m_wsz, dwLen*sizeof(wchar_t));
m_dwLengthCache = dwLen-1;
}
return *this;
}
const CLBPString& CLBPString::operator=(wchar_t ch)
{
Allocate(1);
m_wsz[0] = ch;
m_wsz[1] = 0;
m_dwLengthCache = 1;
return *this;
}
const CLBPString& CLBPString::operator=(char ch)
{
return *this = ConvertMBCSCharToWideChar(ch);
}
const CLBPString& CLBPString::operator=(const char* lpsz)
{
if(!lpsz)
{
Empty();
return *this;
}
size_t dwChars = strlen(lpsz);
Allocate(dwChars);
#ifdef LBPLIB_WINDOWS_SPECIFIC
MultiByteToWideChar(CP_ACP, 0, lpsz, -1, m_wsz, (int)GetAllocation());
#else
mbstowcs(m_wsz, lpsz, dwChars+1);
#endif
m_dwLengthCache = (int)dwChars;
return *this;
}
const CLBPString& CLBPString::operator=(const wchar_t* lpsz)
{
if(m_wszEmpty == lpsz || NULL == lpsz || lpsz[0] == 0)
{
Empty();
return *this;
}
if(m_wsz && wcscmp(m_wsz, lpsz) == 0)
{
return *this;
}
const DWORD dwLen = (DWORD)wcslen(lpsz)+1;
//#ifdef _DEBUG
// if(dwLen > 15)
// {
// Sleep(0); //Good place for a breakpoint.
// }
//#endif
Allocate(dwLen);
//wcscpy(m_wsz, lpsz);
//memmove should be a little faster - doesn't have to check for the terminator
memmove(m_wsz, lpsz, dwLen*sizeof(wchar_t));
m_dwLengthCache = dwLen-1;
return *this;
}
const CLBPString& CLBPString::Set(const wchar_t* lpsz, DWORD dwCharacters)
{
Allocate(dwCharacters);
memmove(m_wsz, lpsz, dwCharacters * sizeof(wchar_t));
m_wsz[dwCharacters] = 0;
m_dwLengthCache = dwCharacters;
return *this;
}
const CLBPString& CLBPString::Set(const char* lpsz, DWORD dwCharacters)
{
CLBPString sCopy = lpsz;
return *this = Set(sCopy.Wide(), dwCharacters);
}
const CLBPString& CLBPString::Set(double d)
{
const int iBufferSize = 50;
GetWideBuffer(iBufferSize);
#ifdef LBPRHLIB
ON_wString::FormatIntoBuffer(m_wsz, iBufferSize, L"%.15g", d);
#else
_swprintf_l(m_wsz, iBufferSize, L"%.15g", LBP_Locale(), d);
#endif
ReleaseWideBuffer();
return *this;
}
const CLBPString& CLBPString::Set(float f)
{
const int iBufferSize = 50;
GetWideBuffer(iBufferSize);
#ifdef LBPRHLIB
ON_wString::FormatIntoBuffer(m_wsz, iBufferSize, L"%.4f", f);
#else
_swprintf_l(m_wsz, iBufferSize, L"%.4f", LBP_Locale(), f);
#endif
ReleaseWideBuffer();
return *this;
}
const CLBPString& CLBPString::Set(int i)
{
const int iBufferSize = 20;
GetWideBuffer(iBufferSize);
#ifdef LBPRHLIB
ON_wString::FormatIntoBuffer(m_wsz, iBufferSize, L"%d", i);
#else
_swprintf_l(m_wsz, iBufferSize, L"%d", LBP_Locale(), i);
#endif
ReleaseWideBuffer();
return *this;
}
// string concatenation
// concatenate from another CLBPString
const CLBPString& CLBPString::operator+=(const CLBPString& string)
{
//return *this += string.AsWideString();
if (string.IsEmpty())
return *this;
return Append(string.Wide(), string.GetLength());
}
// concatenate a single character
const CLBPString& CLBPString::operator+=(char ch)
{
return *this += ConvertMBCSCharToWideChar(ch);
}
const CLBPString& CLBPString::operator+=(wchar_t ch)
{
const DWORD dwRequired = GetLength() + 1;
Allocate(dwRequired);
m_wsz[dwRequired-1] = ch;
m_wsz[dwRequired] = 0;
m_dwLengthCache = dwRequired;
return *this;
}
const CLBPString& CLBPString::operator+=(const char* lpsz)
{
return *this += CLBPString(lpsz);
}
const CLBPString& CLBPString::operator+=(const wchar_t* lpsz)
{
if(!lpsz) return *this;
if(0 == *lpsz) return *this;
const int iLengthIncoming = (int)wcslen(lpsz);
return Append(lpsz, iLengthIncoming);
}
const CLBPString& CLBPString::Append(const wchar_t* lpsz, int iCount)
{
if(!lpsz) return *this;
if(0 == *lpsz) return *this;
if (0==iCount)
return *this;
const int iLength = GetLength();
ASSERT(*(m_wsz+iLength) == 0);
CLBPString* pS = NULL;
const wchar_t* wszToUse = lpsz;
// Always do this check; we MUST copy the string if it's a subset of the current string.
if ((lpsz >= m_wsz) && (lpsz < m_wsz+iLength+1))
{
// lpsz is a subset of the current string - this means we have to copy it, unfortunately
pS = new CLBPString(lpsz);
wszToUse = pS->Wide();
#pragma message ("CLBPString: Subset optimisation")
}
const int iLengthIncoming = iCount;
const DWORD dwRequired = (DWORD)(iLength + iLengthIncoming + 1);
Allocate(dwRequired);
memmove(m_wsz+iLength, wszToUse, iLengthIncoming*sizeof(wchar_t));
*(m_wsz+iLength+iLengthIncoming) = 0;
m_dwLengthCache = dwRequired - 1;
delete pS;
return *this;
}
//These functions rely on automatic conversion - it's critical they don't use the BV form
#define LBPSTRING_IMPL_OPERATORADD CLBPStringBV s(string);s+=ch;return s
#define LBPSTRING_IMPL_OPERATORADD_PREFIX CLBPStringBV s(ch);s+=string;return s
CLBPStringBV AFXAPI operator+(const CLBPString& string1, const CLBPString& string2)
{
CLBPStringBV s(string1);
s+=string2;
return s;
}
CLBPStringBV AFXAPI operator+(const CLBPString& string, char ch) {LBPSTRING_IMPL_OPERATORADD;}
CLBPStringBV AFXAPI operator+(const CLBPString& string, wchar_t ch) {LBPSTRING_IMPL_OPERATORADD;}
CLBPStringBV AFXAPI operator+(const CLBPString& string, const char* ch) {LBPSTRING_IMPL_OPERATORADD;}
CLBPStringBV AFXAPI operator+(const CLBPString& string, const wchar_t* ch) {LBPSTRING_IMPL_OPERATORADD;}
CLBPStringBV AFXAPI operator+(char ch, const CLBPString& string) {LBPSTRING_IMPL_OPERATORADD_PREFIX;}
CLBPStringBV AFXAPI operator+(wchar_t ch, const CLBPString& string) {LBPSTRING_IMPL_OPERATORADD_PREFIX;}
CLBPStringBV AFXAPI operator+(const char* ch, const CLBPString& string) {LBPSTRING_IMPL_OPERATORADD_PREFIX;}
CLBPStringBV AFXAPI operator+(const wchar_t* ch, const CLBPString& string) {LBPSTRING_IMPL_OPERATORADD_PREFIX;}
#ifdef _NATIVE_WCHAR_T_DEFINED
CLBPStringBV AFXAPI operator+(const CLBPString& string, const USHORT* lpsz) { return operator+(string, (const wchar_t*)lpsz); }
CLBPStringBV AFXAPI operator+(const USHORT* lpsz, const CLBPString& string) { return operator+((const wchar_t*)lpsz, string); }
CLBPStringBV AFXAPI operator+(USHORT ch, const CLBPString& string) { return operator+((wchar_t)ch, string); }
CLBPStringBV AFXAPI operator+(const CLBPString& string, USHORT ch) { return operator+(string, (wchar_t)ch); }
#endif
// string comparison
int CLBPString::Compare(const wchar_t* wsz) const
{
if ((NULL == wsz) && IsEmpty())
return 0;
return wcscmp(m_wsz, (NULL == wsz) ? L"" : wsz);
}
int CLBPString::CompareNoCase(const wchar_t* wsz) const
{
if ((NULL == wsz) && IsEmpty())
return 0;
return _wcsicmp(m_wsz, (NULL == wsz) ? L"" : wsz);
}
int CLBPString::Compare(const char* wsz) const
{
return Compare(CLBPString(wsz).AsWideString());
}
int CLBPString::CompareNoCase(const char* wsz) const
{
return CompareNoCase(CLBPString(wsz).AsWideString());
}
// NLS aware comparison, case sensitive
int CLBPString::Collate(const wchar_t* wsz) const
{
if ((NULL == wsz) && IsEmpty())
return 0;
#ifdef LBPLIB_APPLE_SPECIFIC
return wcscoll(m_wsz, (NULL == wsz) ? L"" : wsz);
#else
return _wcsicoll(m_wsz, (NULL == wsz) ? L"" : wsz);
#endif
}
int CLBPString::CollateNoCase(const wchar_t* wsz) const
{
if ((NULL == wsz) && IsEmpty())
return 0;
return wcscoll(m_wsz, (NULL == wsz) ? L"" : wsz);
}
int CLBPString::Collate(const char* wsz) const
{
return Collate(CLBPString(wsz).AsWideString());
}
int CLBPString::CollateNoCase(const char* wsz) const
{
return CollateNoCase(CLBPString(wsz).AsWideString());
}
// simple sub-string extraction
CLBPStringBV CLBPString::Mid(int nFirst, int nCount) const
{
if (nFirst < 0) nFirst = 0;
if (nCount < 0) nCount = 0;
const int iLength = GetLength();
if (nFirst + nCount > iLength) nCount = iLength - nFirst;
if (nFirst > iLength) nCount = 0;
// optimize case of returning entire string
if (nFirst == 0 && nCount == iLength)
return *this;
//Cut the string so that the copy will only take the correct bit
wchar_t w = m_wsz[nFirst+nCount];
m_wsz[nFirst+nCount] = 0;
const CLBPString s(m_wsz+nFirst, nCount); //Copy the string from the start of the block
s.SetAllowAutoReleaseOwnership();
//Put the character back
m_wsz[nFirst+nCount] = w;
return s;
}
const wchar_t* CLBPString::Mid(int nFirst) const
{
if (nFirst < 0) nFirst = 0;
const wchar_t* p = m_wsz;
const int iLength = GetLength();
if (nFirst <= iLength)
p = m_wsz + nFirst;
#ifdef _DEBUG
#pragma message("CLBPString::Mid(int) - regression test included.\n")
ASSERT(Mid(nFirst, GetLength()-nFirst).Compare(p)==0);
#endif
return p;
}
CLBPStringBV CLBPString::Left(int nCount) const
{
return Mid(0, nCount);
}
void CLBPString::Truncate(int nCount)
{
if (nCount < 0) nCount = 0;
#ifdef _DEBUG
m_bAllowAutoReleaseOwnership = false;
CLBPString sCopy(*this);
#endif
if (0==nCount)
{
Empty();
}
else
{
const int iLength = GetLength();
if (nCount < iLength)
{
m_wsz[nCount] = 0;
m_dwLengthCache = nCount;
}
}
#ifdef _DEBUG
#pragma message("CLBPString::Truncate - regression test included.\n")
ASSERT(Compare(sCopy.Left(nCount).Wide())==0);
#endif
return;
}
void CLBPString::TruncateMid(int nPos)
{
if (nPos < 0) nPos = 0;
#ifdef _DEBUG
#pragma message("CLBPString::TruncateMid - regression test included.\n")
CLBPString s = Mid(nPos);
#endif
const int iLength = GetLength();
if (nPos <= iLength)
{
memmove(m_wsz, m_wsz+nPos, (iLength-nPos+1)*sizeof(wchar_t));
m_dwLengthCache = iLength-nPos;
}
ASSERT(s.Compare(m_wsz)==0);
}
const wchar_t* CLBPString::Right(int nCount) const
{
if (nCount < 0) nCount = 0;
const wchar_t* p = m_wsz;
const int iLength = GetLength();
if (nCount < iLength)
p = m_wsz + (iLength-nCount);
#ifdef _DEBUG
#pragma message("CLBPString::Right - regression test included.\n")
ASSERT(CLBPString(Mid(GetLength()-nCount)).Compare(p)==0);
#endif
return p;
}
// characters from beginning that are also in passed string
CLBPStringBV CLBPString::SpanIncluding(const char* lpszCharSet) const
{
return SpanIncluding(CLBPString(lpszCharSet).AsWideString());
}
CLBPStringBV CLBPString::SpanIncluding(const wchar_t* lpszCharSet) const
{
return Left((int)wcsspn(m_wsz, lpszCharSet));
}
CLBPStringBV CLBPString::SpanExcluding(const char* lpszCharSet) const
{
return SpanExcluding(CLBPString(lpszCharSet).AsWideString());
}
CLBPStringBV CLBPString::SpanExcluding(const wchar_t* lpszCharSet) const
{
return Left((int)wcscspn(m_wsz, lpszCharSet));
}
#ifdef _NATIVE_WCHAR_T_DEFINED
CLBPStringBV CLBPString::SpanIncluding(const USHORT* lpszCharSet) const { return SpanIncluding((const wchar_t*)lpszCharSet); }
CLBPStringBV CLBPString::SpanExcluding(const USHORT* lpszCharSet) const { return SpanExcluding((const wchar_t*)lpszCharSet); }
#endif
void CLBPString::MakeUpper()
{
if(IsEmpty()) return;
_wcsupr(m_wsz);
}
void CLBPString::MakeLower()
{
if(IsEmpty()) return;
_wcslwr(m_wsz);
}
void CLBPString::MakeReverse()
{
if(IsEmpty()) return;
_wcsrev(m_wsz);
}
void CLBPString::TrimRight()
{
const int iLength = GetLength();
for (int i=iLength-1; i>=0; i--)
{
if (iswspace(m_wsz[i]))
{
m_dwLengthCache--;
m_wsz[i] = 0;
}
else
{
break;
}
}
}
void CLBPString::TrimLeft()
{
wchar_t* lpsz = m_wsz;
int iCut = 0;
while (*lpsz != '\0')
{
if (!iswspace(*lpsz)) break;
lpsz++;
iCut++;
}
if (lpsz != m_wsz)
{
// fix up data and length
int nDataLength = (int)(GetLength() - (lpsz - m_wsz));
memmove(m_wsz, lpsz, (nDataLength+1)*sizeof(wchar_t));
}
m_dwLengthCache -= iCut;
}
// trimming anything (either side)
// remove continuous occurrences of chTarget starting from right
void CLBPString::TrimRight(char chTarget)
{
TrimRight(ConvertMBCSCharToWideChar(chTarget));
}
void CLBPString::TrimRight(const char* lpszTargets)
{
TrimRight(CLBPString(lpszTargets).AsWideString());
}
void CLBPString::TrimLeft(char chTarget)
{
TrimLeft(ConvertMBCSCharToWideChar(chTarget));
}
void CLBPString::TrimLeft(const char* lpszTargets)
{
TrimLeft(CLBPString(lpszTargets).AsWideString());
}
void CLBPString::TrimRight(wchar_t w)
{
//TrimRight(CLBPString(w).AsWideString());
const int iLength = GetLength();
for (int i=iLength-1; i>=0; i--)
{
if (m_wsz[i] == w)
{
m_dwLengthCache--;
m_wsz[i] = 0;
}
else
{
break;
}
}
}
static __forceinline bool is_char_in_list(wchar_t ch, const wchar_t* list, size_t count)
{
for (size_t i=0;i<count;i++)
{
if (ch == list[i])
{
return true;
}
}
return false;
}
void CLBPString::TrimRight(const wchar_t* lpszTargetList)
{
//CLBPString tl(lpszTargetList);
const size_t iTargetCount = wcslen(lpszTargetList);
for (int i=GetLength()-1; i>=0; i--)
{
//if (tl.Find(m_wsz[i]) != -1)
if (is_char_in_list(m_wsz[i], lpszTargetList, iTargetCount))
{
m_wsz[i] = 0;
m_dwLengthCache--;
}
else
{
break;
}
}
}
void CLBPString::TrimLeft(wchar_t chTarget)
{
//TrimLeft(CLBPString(chTarget).AsWideString());
wchar_t* lpsz = m_wsz;
int iCut = 0;
while (*lpsz != '\0')
{
//if (!iswspace(*lpsz)) break;
if (*lpsz != chTarget) break;
lpsz++;
iCut++;
}
if (lpsz != m_wsz)
{
// fix up data and length
int nDataLength = (int)(GetLength() - (lpsz - m_wsz));
memmove(m_wsz, lpsz, (nDataLength+1)*sizeof(wchar_t));
}
m_dwLengthCache -= iCut;
}
void CLBPString::TrimLeft(const wchar_t* lpszTargets)
{
if (wcslen(lpszTargets) == 0) return;
wchar_t* lpsz = m_wsz;
int iCut = 0;
while (*lpsz != '\0')
{
if (wcschr(lpszTargets, *lpsz) == NULL)
break;
lpsz++;
iCut++;
}
if (lpsz != m_wsz)
{
// fix up data and length
int nDataLength = (int)(GetLength() - (lpsz - m_wsz));
memmove(m_wsz, lpsz, (nDataLength+1)*sizeof(wchar_t));
}
m_dwLengthCache-=iCut;
}
// advanced manipulation
int CLBPString::Replace(char chOld, char chNew)
{
return Replace(ConvertMBCSCharToWideChar(chOld), ConvertMBCSCharToWideChar(chNew));
}
int CLBPString::Replace(wchar_t chOld, wchar_t chNew)
{
int nCount = 0;
const int iLength = GetLength();
if (chOld != chNew)
{
for(int i=0;i<iLength;i++)
{
if(m_wsz[i] == chOld)
{
m_wsz[i] = chNew;
nCount++;
}
}
}
//Just in case chNew was /0
m_dwLengthCache = -1;
return nCount;
}
int CLBPString::Replace(const char* lpszOld, const char* lpszNew)
{
CLBPString sOld(lpszOld);
CLBPString sNew(lpszNew);
return Replace(sOld.AsWideString(), sNew.AsWideString());
}
int CLBPString::Replace(const wchar_t* lpszOld, const wchar_t* lpszNew)
{
if(IsEmpty())