My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
class Nuller;

#if defined(CPU_X86) && (defined(COMPILER_MSC) || defined(COMPILER_GCC))
#define FAST_STRING_COMPARE
#endif

int wstrlen(const wchar *s);

#ifdef PLATFORM_POSIX
inline int stricmp(const char *a, const char *b) { return strcasecmp(a, b); }
inline int strnicmp(const char *a, const char *b, int n) { return strncasecmp(a, b, n); }
#endif

#ifdef PLATFORM_WINCE
inline int stricmp(const char *a, const char *b) { return _stricmp(a, b); }
inline int strnicmp(const char *a, const char *b, int n) { return _strnicmp(a, b, n); }
#endif

force_inline int strlen__(const char *s) { return s ? (int)strlen(s) : 0; }

inline int strlen__(const wchar *s) { return s ? (int)wstrlen(s) : 0; }

inline int cmpval__(char x) { return (byte)x; }
inline int cmpval__(wchar x) { return (word)x; }

class String;
class WString;
class StringBuffer;
class WStringBuffer;

class Stream;

template <class B>
class AString : public B {
typedef typename B::tchar tchar;
typedef typename B::bchar bchar;
typedef typename B::Buffer buffer;
typedef typename B::String String;

public:
void Clear() { B::Free(); B::Zero(); }
int GetLength() const { return B::GetCount(); }
bool IsEmpty() const { return B::GetCount() == 0; }

const tchar *End() const { return B::Begin() + GetLength(); }
const tchar *Last() const { return End() - !!B::GetCount(); }
const tchar *GetIter(int i) const { ASSERT(i >= 0 && i <= B::GetCount()); return B::Begin() + i; }

int operator*() const { return *B::Begin(); }
int operator[](int i) const { ASSERT(i >= 0 && i <= B::GetCount()); return B::Begin()[i]; }

operator const tchar *() const { return B::Begin(); }
const tchar *operator~() const { return B::Begin(); }
operator const bchar *() const { return (bchar *)B::Begin(); }
operator const void *() const { return B::Begin(); }

void Insert(int pos, int c) { *B::Insert(pos, 1, NULL) = c; }
void Insert(int pos, const tchar *s, int count) { B::Insert(pos, count, s); }
void Insert(int pos, const String& s) { Insert(pos, s, s.GetCount()); }
void Insert(int pos, const char *s);

void Cat(int c) { B::Cat(c); }
void Cat(const tchar *s, int len) { B::Cat(s, len); }
void Cat(const tchar *s);
void Cat(const String& s) { Cat(~s, s.GetLength()); }
void Cat(int c, int count);
void Cat(const tchar *s, const tchar *lim) { ASSERT(s <= lim); Cat(s, int(lim - s)); }
void Cat(const String& s, int len) { B::Cat(~s, len); }
void Cat(const bchar *s, int len) { Cat((const tchar *) s, len); }

String& Cat() { return *(String *)this; }

int Compare(const String& s) const { return B::Compare(s); }
int Compare(const tchar *s) const;

bool IsEqual(const String& s) const { return B::IsEqual(s); }
bool IsEqual(const tchar *s) const { return Compare(s) == 0; }

String Mid(int pos, int length) const;
String Mid(int pos) const { return Mid(pos, GetLength() - pos); }
String Right(int count) const { return Mid(GetLength() - count); }
String Left(int count) const { return Mid(0, count); }

int Find(int chr, int from = 0) const;
int ReverseFind(int chr, int from) const;
int ReverseFind(int chr) const;

int Find(int len, const tchar *s, int from) const;
int Find(const tchar *s, int from = 0) const;
int Find(const String& s, int from = 0) const { return Find(s.GetCount(), ~s, from); }

int ReverseFind(int len, const tchar *s, int from) const;
int ReverseFind(const tchar *s, int from) const;
int ReverseFind(const String& s, int from) const { return ReverseFind(s.GetCount(), ~s, from); }
int ReverseFind(const tchar *s) const { return ReverseFind(s, GetLength()-1);}
int ReverseFind(const String& s) const { return ReverseFind(s, GetLength()-1);}

void Replace(const tchar *find, int findlen, const tchar *replace, int replacelen);
void Replace(const String& find, const String& replace);
void Replace(const tchar *find, const tchar *replace);
void Replace(const String& find, const tchar *replace);
void Replace(const tchar *find, const String& replace);

bool StartsWith(const tchar *s, int len) const;
bool StartsWith(const tchar *s) const;
bool StartsWith(const String& s) const { return StartsWith(~s, s.GetLength()); }

bool EndsWith(const tchar *s, int len) const;
bool EndsWith(const tchar *s) const;
bool EndsWith(const String& s) const { return EndsWith(~s, s.GetLength()); }

int FindFirstOf(int len, const tchar *s, int from) const;
int FindFirstOf(const tchar *s, int from = 0) const;
int FindFirstOf(const String& s, int from = 0) const { return FindFirstOf(s.GetCount(), ~s, from); }

friend bool operator<(const String& a, const String& b) { return a.Compare(b) < 0; }
friend bool operator<(const String& a, const tchar *b) { return a.Compare(b) < 0; }
friend bool operator<(const tchar *a, const String& b) { return b.Compare(a) > 0; }

friend bool operator<=(const String& a, const String& b) { return a.Compare(b) <= 0; }
friend bool operator<=(const String& a, const tchar *b) { return a.Compare(b) <= 0; }
friend bool operator<=(const tchar *a, const String& b) { return b.Compare(a) >= 0; }

friend bool operator>(const String& a, const String& b) { return a.Compare(b) > 0; }
friend bool operator>(const String& a, const tchar *b) { return a.Compare(b) > 0; }
friend bool operator>(const tchar *a, const String& b) { return b.Compare(a) < 0; }

friend bool operator>=(const String& a, const String& b) { return a.Compare(b) >= 0; }
friend bool operator>=(const String& a, const tchar *b) { return a.Compare(b) >= 0; }
friend bool operator>=(const tchar *a, const String& b) { return b.Compare(a) <= 0; }

friend bool operator==(const String& a, const String& b) { return a.IsEqual(b); }
friend bool operator!=(const String& a, const String& b) { return !a.IsEqual(b); }
friend bool operator==(const String& a, const tchar *b) { return a.Compare(b) == 0; }
friend bool operator==(const tchar *a, const String& b) { return b.Compare(a) == 0; }
friend bool operator!=(const String& a, const tchar *b) { return a.Compare(b) != 0; }
friend bool operator!=(const tchar *a, const String& b) { return b.Compare(a) != 0; }

friend String operator+(const String& a, const String& b) { String c(a); c += b; return c; }
friend String operator+(const String& a, const tchar *b) { String c(a); c += b; return c; }
friend String operator+(const tchar *a, const String& b) { String c(a); c += b; return c; }
friend String operator+(const String& a, int b) { String c(a); c += b; return c; }
friend String operator+(int a, const String& b) { String c(a, 1); c += b; return c; }
friend String operator+(const String& a, tchar b) { String c(a); c += b; return c; }
friend String operator+(tchar a, const String& b) { String c(a, 1); c += b; return c; }
};

class String0 : Moveable<String0> {
enum { SMALL = 0, MEDIUM = 31 }; // SMALL has to be 0 because of GetSpecial and because is it ending zero
enum { KIND = 14, SLEN = 15, LLEN = 2, SPECIAL = 13 };

struct Rc {
Atomic refcount;
int alloc;

char *GetPtr() const { return (char*)(this + 1); }
void Release();
void Retain();
};

union {
char chr[16];
char *ptr;
dword *wptr;
qword *qptr;
word v[8];
dword w[4];
qword q[2];
};


#if defined(_DEBUG) && defined(COMPILER_GCC)
int len;
const char *s;
#endif

#ifdef _DEBUG
void Dsyn();
#else
void Dsyn() {}
#endif

char& SLen() { return chr[SLEN]; }
char SLen() const { return chr[SLEN]; }
dword& LLen() { return w[LLEN]; }
dword LLen() const { return w[LLEN]; }
bool IsSmall() const { return chr[KIND] == SMALL; }
bool IsLarge() const { return chr[KIND] != SMALL; }
bool IsMedium() const { return chr[KIND] == MEDIUM; }
bool IsRef() const { return (byte)chr[KIND] > MEDIUM; }
Rc *Ref() const { return (Rc *)ptr - 1; }
bool IsShared() const { return Ref()->refcount != 1; }
bool IsSharedRef() const { return IsRef() && IsShared(); }
int LAlloc() const { int b = (byte)chr[KIND]; return b == 255 ? Ref()->alloc : b; }
dword LEqual(const String0& s) const;
int LCompare(const String0& s) const;

void LSet(const String0& s);
void LFree();
void LCat(int c);
unsigned LHashValue() const;

void UnShare();
void SetSLen(int l);

char *Ptr() { return IsSmall() ? chr : ptr; }
char *Alloc(int count, char& kind);

static String0::Rc voidptr[2];

void Swap(String0& b) { UPP::Swap(q[0], b.q[0]); UPP::Swap(q[1], b.q[1]); Dsyn(); b.Dsyn(); }

void SetSpecial0(byte st) { w[3] = MAKE4B(0, st, 0, 0); }
void SetSpecial(byte st) { ASSERT(IsSmall() && GetCount() == 0); SetSpecial0(st); }
byte GetSpecial() const { return (chr[SLEN] | chr[KIND]) == 0 ? chr[SPECIAL] : 0; }
byte GetSt() const { return chr[SPECIAL]; }
bool IsSpecial() const { return !v[7] && v[6]; }
bool IsSpecial(byte st) const { return w[3] == MAKE4B(0, st, 0, 0); }

friend class String;
friend class StringBuffer;
friend class Value;

protected:
void Zero() { q[0] = q[1] = 0; Dsyn(); }
void Free() { if(IsLarge()) LFree(); }
void SetSmall(const String0& s) { q[0] = s.q[0]; q[1] = s.q[1]; }
void Set(const String0& s) {
if(s.IsSmall()) SetSmall(s); else LSet(s);
Dsyn();
}
void Assign(const String0& s) {
if(s.IsSmall()) {
Free();
SetSmall(s);
}
else
if(this != &s) {
Free();
LSet(s);
}
Dsyn();
}
void Set(const char *s, int len);
char *Insert(int pos, int count, const char *str);

public: // should be protected, bug in gcc 3.4
typedef char tchar;
typedef byte bchar;
typedef StringBuffer Buffer;
typedef Upp::String String;

public:
bool IsEqual(const String0& s) const {
return (chr[KIND] | s.chr[KIND] ? LEqual(s) :
#ifdef CPU_64
((q[0] ^ s.q[0]) | (q[1] ^ s.q[1]))
#else
((w[0] ^ s.w[0]) | (w[1] ^ s.w[1]) | (w[2] ^ s.w[2]) | (w[3] ^ s.w[3]))
#endif
) == 0;
}

int Compare(const String0& s) const;

unsigned GetHashValue() const {
return chr[KIND] ? LHashValue() : (unsigned)CombineHash(w[0], w[1], w[2], w[3]);
}

void Cat(int c) {
if(SLen() < 14)
chr[int(SLen()++)] = c;
else
LCat(c);
Dsyn();
}

void Cat(const char *s, int len);

void Set(int i, int chr);
void Trim(int pos);

const char *Begin() const { return IsSmall() ? chr : ptr; }
const char *End() const { return Begin() + GetLength(); }

int operator[](int i) const { ASSERT(i >= 0 && i <= GetCount()); return Begin()[i]; }

operator const char *() const { return Begin(); }
const char *operator~() const { return Begin(); }

void Remove(int pos, int count = 1);
void Clear() { Free(); Zero(); }

int GetCount() const { return IsSmall() ? chr[SLEN] : w[LLEN]; }
int GetLength() const { return GetCount(); }
int GetAlloc() const { return IsSmall() ? 14 : LAlloc(); }

void Reserve(int r);

String0& operator=(const String0& s) { Free(); Set(s); return *this; }

String0() {}
~String0() { Free(); }
};

class String : public Moveable<String, AString<String0> > {
void Swap(String& b) { String0::Swap(b); }

#ifdef _DEBUG
#ifndef COMPILER_GCC
int len;
const char *s;
#endif
friend class String0;
#endif

void AssignLen(const char *s, int slen);

enum SSPECIAL { SPECIAL };

template <class T>
String(const T& x, byte st, SSPECIAL) {
*(T*)chr = x;
SetSpecial0(st);
}
String(SSPECIAL) {}

friend class Value;

public:
const String& operator+=(char c) { Cat(c); return *this; }
const String& operator+=(const char *s) { Cat(s); return *this; }
const String& operator+=(const String& s) { Cat(s); return *this; }

String& operator=(const char *s);
String& operator=(const String& s) { String0::Assign(s); return *this; }
String& operator=(StringBuffer& b) { *this = String(b); return *this; }
String& operator<<=(const String& s) { if(this != &s) { String0::Free(); String0::Set(s, s.GetCount()); } return *this; }

void Shrink() { *this = String(Begin(), GetLength()); }
int GetCharCount() const;

String() { Zero(); }
String(const Nuller&) { Zero(); }
String(const String& s) { String0::Set(s); }
String(const char *s);
String(const String& s, int n) { ASSERT(n >= 0 && n <= s.GetLength()); String0::Set(~s, n); }
String(const char *s, int n) { String0::Set(s, n); }
String(const byte *s, int n) { String0::Set((const char *)s, n); }
String(const char *s, const char *lim) { String0::Set(s, (int)(lim - s)); }
String(int chr, int count) { String0::Zero(); Cat(chr, count); }
String(StringBuffer& b);

WString ToWString() const;
const String& ToString() const { return *this; }

static String GetVoid();
bool IsVoid() const;

friend void Swap(String& a, String& b) { a.Swap(b); }

String(const std::string& s) { String0::Set(s.c_str(), (int)s.length()); }
operator std::string() const { return std::string(Begin(), End()); }
};

inline std::string to_string(const String& s) { return std::string(s.Begin(), s.End()); }

class StringBuffer : NoCopy {
char *begin;
char *end;
char *limit;
char buffer[256];

friend class String;

typedef String0::Rc Rc;

char *Alloc(int len, int& alloc);
void Realloc(int n, const char *cat = NULL, int l = 0);
void Expand();
void Zero() { begin = end = buffer; limit = begin + 255; }
void Free();
void Set(String& s);

public:
char *Begin() { *end = '\0'; return begin; }
char *End() { *end = '\0'; return end; }

operator char*() { return Begin(); }
char *operator~() { return Begin(); }

void SetLength(int l);
void SetCount(int l) { SetLength(l); }
int GetLength() const { return (int)(end - begin); }
int GetCount() const { return GetLength(); }
void Strlen();
void Clear() { Free(); Zero(); }
void Reserve(int r) { int l = GetLength(); SetLength(l + r); SetLength(l); }

void Cat(int c) { if(end >= limit) Expand(); *end++ = c; }
void Cat(int c, int count);
void Cat(const char *s, int l);
void Cat(const char *s, const char *e) { Cat(s, int(e - s)); }
void Cat(const char *s);
void Cat(const String& s) { Cat(s, s.GetLength()); }

int GetAlloc() const { return (int)(limit - begin); }

void operator=(String& s) { Free(); Set(s); }

StringBuffer() { Zero(); }
StringBuffer(String& s) { Set(s); }
StringBuffer(int len) { Zero(); SetLength(len); }
~StringBuffer() { if(begin != buffer) Free(); }
};

inline bool IsEmpty(const String& s) { return s.IsEmpty(); }

String FormatPtr(const void *p);

template <class T>
inline String AsString(const T& x)
{
return x.ToString();
}

template <class T>
inline String AsString(T *x)
{
return FormatPtr(x);
}

force_inline String& operator<<(String& s, const char *x)
{
s.Cat(x, strlen__(x));
return s;
}

force_inline String& operator<<(String& s, char *x)
{
s.Cat(x);
return s;
}

inline String& operator<<(String& s, const String &x)
{
s.Cat(x);
return s;
}

inline String& operator<<(String& s, char x)
{
s.Cat((int) x);
return s;
}

inline String& operator<<(String& s, const void *x)
{
s << FormatPtr(x);
return s;
}

inline String& operator<<(String& s, void *x)
{
s << FormatPtr(x);
return s;
}

template <class T>
inline String& operator<<(String& s, const T& x)
{
s.Cat(AsString(x));
return s;
}

template<>
inline String& operator<<(String& s, const char * const &x)
{
s.Cat(x);
return s;
}

template<>
inline String& operator<<(String& s, const String &x)
{
s.Cat(x);
return s;
}

template<>
inline String& operator<<(String& s, const char& x)
{
s.Cat(x);
return s;
}

template<>
inline bool IsNull(const String& s) { return s.IsEmpty(); }

template<>
inline String AsString(const String& s) { return s; }

template<>
inline unsigned GetHashValue(const String& s) { return s.GetHashValue(); }

int CompareNoCase(const String& a, const String& b, byte encoding = 0);
int CompareNoCase(const String& a, const char *b, byte encoding = 0);

inline
int CompareNoCase(const char *a, const String& b, byte encoding = 0) {
return -CompareNoCase(b, a, encoding);
}

String TrimLeft(const String& s);
String TrimRight(const String& s);
String TrimBoth(const String& s);

inline StringBuffer& operator<<(StringBuffer& s, const char *x)
{
s.Cat(x);
return s;
}

inline StringBuffer& operator<<(StringBuffer& s, char *x)
{
s.Cat(x);
return s;
}

inline StringBuffer& operator<<(StringBuffer& s, const String &x)
{
s.Cat(x);
return s;
}

inline StringBuffer& operator<<(StringBuffer& s, char x)
{
s.Cat((int) x);
return s;
}

inline StringBuffer& operator<<(StringBuffer& s, const void *x)
{
s << FormatPtr(x);
return s;
}

inline StringBuffer& operator<<(StringBuffer& s, void *x)
{
s << FormatPtr(x);
return s;
}

template <class T>
inline StringBuffer& operator<<(StringBuffer& s, const T& x)
{
s.Cat(AsString(x));
return s;
}

template<>
inline StringBuffer& operator<<(StringBuffer& s, const char * const &x)
{
s.Cat(x);
return s;
}

template<>
inline StringBuffer& operator<<(StringBuffer& s, const String &x)
{
s.Cat(x);
return s;
}

template<>
inline StringBuffer& operator<<(StringBuffer& s, const char& x)
{
s.Cat(x);
return s;
}

template <class T>
void RawCat(String& s, const T& x)
{
s.Cat((const char *)&x, sizeof(x));
}

template <class T>
void RawCat(StringBuffer& s, const T& x)
{
s.Cat((const char *)&x, sizeof(x));
}

class WString0 {
enum { SMALL = 23 };

wchar *ptr;
int length;
int alloc;

#ifdef _DEBUG
void Dsyn();
#else
void Dsyn() {}
#endif

static Atomic voidptr[2];

bool IsRc() const { return alloc > SMALL; }
Atomic& Rc() { return *((Atomic *)ptr - 1); }
bool IsShared() { return IsRc() && Rc() > 1; }

wchar *Alloc(int& count);
void LCat(int c);
void UnShare();

friend class WStringBuffer;
friend class WString;

public: // should be protected, bug in GCC 3.4
typedef wchar tchar;
typedef int16 bchar;
typedef WStringBuffer Buffer;
typedef WString String;

protected:
void Zero() { static wchar e[2]; length = alloc = 0; ptr = e; Dsyn(); ASSERT(*ptr == 0); }
void Set(const wchar *s, int length);
void Set(const WString0& s);
void Free();
void Swap(WString0& b) { Upp::Swap(ptr, b.ptr); Upp::Swap(length, b.length); Upp::Swap(alloc, b.alloc); Dsyn(); b.Dsyn(); }
wchar *Insert(int pos, int count, const wchar *data);

public:
const wchar *Begin() const { return ptr; }
const wchar *End() const { return Begin() + GetLength(); }
int operator[](int i) const { return ptr[i]; }

operator const wchar *() const { return Begin(); }
const wchar *operator~() const { return Begin(); }

void Cat(int c) { if(!IsRc() && length < alloc) { ptr[length++] = c; ptr[length] = 0; } else LCat(c); Dsyn(); }
void Cat(const wchar *s, int length);

int GetCount() const { return length; }
int GetLength() const { return length; }
int GetAlloc() const { return alloc; }

unsigned GetHashValue() const { return memhash(ptr, length * sizeof(wchar)); }
bool IsEqual(const WString0& s) const { return s.length == length && memcmp(ptr, s.ptr, length * sizeof(wchar)) == 0; }
int Compare(const WString0& s) const;

void Remove(int pos, int count = 1);
void Insert(int pos, const wchar *s, int count);
void Clear() { Free(); Zero(); }

void Set(int pos, int ch);
void Trim(int pos);

WString0() { Zero(); }
~WString0() { Free(); }
};

class WString : public Moveable<WString, AString<WString0> >
{
void Swap(WString& b) { WString0::Swap(b); }

#ifdef _DEBUG
int len;
const wchar *s;
friend class WString0;
#endif

public:
UPP::String ToString() const;

const WString& operator+=(wchar c) { Cat(c); return *this; }
const WString& operator+=(const wchar *s) { Cat(s); return *this; }
const WString& operator+=(const WString& s) { Cat(s); return *this; }

WString& operator<<(wchar c) { Cat(c); return *this; }
WString& operator<<(const WString& s) { Cat(s); return *this; }
WString& operator<<(const wchar *s) { Cat(s); return *this; }

WString& operator=(const wchar *s);
WString& operator=(const WString& s) { if(this != &s) { WString0::Free(); WString0::Set(s); } return *this; }
WString& operator=(WStringBuffer& b) { *this = WString(b); return *this; }
WString& operator<<=(const WString& s) { if(this != &s) { WString0::Free(); WString0::Set(s, s.GetCount()); } return *this; }

void Shrink() { *this = WString(Begin(), GetLength()); }

WString() {}
WString(const Nuller&) {}
WString(const WString& s) { WString0::Set(s); }
WString(const wchar *s) { WString0::Set(s, strlen__(s)); }
WString(const WString& s, int n) { ASSERT(n >= 0 && n <= s.GetLength()); WString0::Set(~s, n); }
WString(const wchar *s, int n) { WString0::Set(s, n); }
WString(const wchar *s, const wchar *lim) { WString0::Set(s, (int)(lim - s)); }
WString(int chr, int count) { WString0::Zero(); Cat(chr, count); }
WString(WStringBuffer& b);

WString(const char *s);
WString(const char *s, int n);
WString(const char *s, const char *lim);

static WString GetVoid();
bool IsVoid() const { return alloc < 0; }

friend void Swap(WString& a, WString& b) { a.Swap(b); }
friend WString operator+(const WString& a, char b) { WString c(a); c += b; return c; }
friend WString operator+(char a, const WString& b) { WString c(a, 1); c += b; return c; }

#ifndef _HAVE_NO_STDWSTRING
WString(const std::wstring& s);
operator std::wstring() const;
#endif

};

#ifndef _HAVE_NO_STDWSTRING
inline std::wstring to_string(const WString& s) { return std::wstring(s.Begin(), s.End()); }
#endif

class WStringBuffer : NoCopy {
wchar *begin;
wchar *end;
wchar *limit;

friend class WString;

wchar *Alloc(int len, int& alloc);
void Expand(int n, const wchar *cat = NULL, int l = 0);
void Expand();
void Zero();
void Free();
void Set(WString& s);

public:
wchar *Begin() { *end = '\0'; return begin; }
wchar *End() { *end = '\0'; return end; }

operator wchar*() { return Begin(); }
wchar *operator~() { return Begin(); }

void SetLength(int l);
void SetCount(int l) { SetLength(l); }
int GetLength() const { return (int)(end - begin); }
int GetCount() const { return GetLength(); }
void Strlen() { SetLength(wstrlen(begin)); }
void Clear() { Free(); Zero(); }
void Reserve(int r) { int l = GetLength(); SetLength(l + r); SetLength(l); }

void Cat(int c) { if(end >= limit) Expand(); *end++ = c; }
void Cat(int c, int count);
void Cat(const wchar *s, int l);
void Cat(const wchar *s, const wchar *e) { Cat(s, int(e - s)); }
void Cat(const wchar *s) { Cat(s, wstrlen(s)); }
void Cat(const WString& s) { Cat(s, s.GetLength()); }
void Cat(const char *s) { Cat(WString(s)); }

int GetAlloc() const { return (int)(limit - begin); }

void operator=(WString& s) { Free(); Set(s); }

WStringBuffer() { Zero(); }
WStringBuffer(WString& s) { Set(s); }
WStringBuffer(int len) { Zero(); SetLength(len); }
~WStringBuffer() { Free(); }
};

inline bool IsEmpty(const WString& s) { return s.IsEmpty(); }

template<>
inline bool IsNull(const WString& s) { return s.IsEmpty(); }

//template<>
//inline String AsString(const WString& s) { return s; }

template<>
inline unsigned GetHashValue(const WString& s) { return memhash(~s, 2 * s.GetLength()); }

WString TrimLeft(const WString& str);
WString TrimRight(const WString& s);

int CompareNoCase(const WString& a, const WString& b);
int CompareNoCase(const WString& a, const wchar *b);

inline
int CompareNoCase(const wchar *a, const WString& b) {
return -CompareNoCase(b, a);
}

template<> inline String AsString(const char * const &s) { return s; }
template<> inline String AsString(char * const &s) { return s; }
template<> inline String AsString(const char *s) { return s; }
template<> inline String AsString(char *s) { return s; }
template<> inline String AsString(const char& a) { return String(a, 1); }
template<> inline String AsString(const signed char& a) { return String(a, 1); }
template<> inline String AsString(const unsigned char& a) { return String(a, 1); }
template<> inline String AsString(const bool& a) { return a ? "true" : "false"; }

unsigned ctoi(int c);

typedef int (*CharFilter)(int);

int CharFilterAscii(int c);
int CharFilterAscii128(int c);
int CharFilterUnicode(int c);
int CharFilterDigit(int c);
int CharFilterWhitespace(int c);
int CharFilterNotWhitespace(int c);
int CharFilterAlpha(int c);
int CharFilterAlphaToUpper(int c);
int CharFilterAlphaToLower(int c);
int CharFilterInt(int c);
int CharFilterDouble(int c);
int CharFilterDefaultToUpperAscii(int c);
int CharFilterCrLf(int c);
int CharFilterNoCrLf(int c);

String Filter(const char *s, int (*filter)(int));
String FilterWhile(const char *s, int (*filter)(int));

WString Filter(const wchar *s, int (*filter)(int));
WString FilterWhile(const wchar *s, int (*filter)(int));

#include "AString.hpp"

Change log

r4970 by cxl on May 20, 2012   Diff
Core: Inet fixes, PCRE: more options
support (thanks dolik)
Go to: 
Project members, sign in to write a code review

Older revisions

r4938 by cxl on May 12, 2012   Diff
Core: Dropping support for MSC7.1,
AsString(T *ptr) added
r4569 by cxl on Feb 10, 2012   Diff
Core: String::Replace now supports all
4 signature variants (RM #250)
r4495 by cxl on Jan 30, 2012   Diff
Core: new Value reintegrated for
conditional compilation (SVO_VALUE
flag)
All revisions of this file

File info

Size: 27869 bytes, 827 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting