-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnstr.h
1041 lines (902 loc) · 25.3 KB
/
nstr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Pixcen - A windows platform low level pixel editor for C64
Copyright (C) 2013 John Hammarberg ([email protected])
This file is part of Pixcen.
Pixcen is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pixcen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Pixcen. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef _NEWSTR_CLASS
#define _NEWSTR_CLASS
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#if defined(_MSC_VER)
#define NSTRWIN
#include <windows.h>
#elif defined(__GNUC__)
#define NSTROSX
#include <ctype.h>
#include <xlocale.h>
#include <libkern/OSTypes.h>
#include <libkern/OSAtomic.h>
#endif
#define NSTRCLASS template<class NCHAR>
NSTRCLASS class NSTR;
#define NSTRTYPE NSTR<NCHAR>
typedef NSTR<char> nstrc;
typedef NSTR<wchar_t> nstrw;
#ifdef _UNICODE
typedef nstrw nstr;
#define _N(x) L ## x
#else
typedef nstrc nstr;
#define _N(x) x
#endif
template<class NCHAR>
class nstrptr
{
public:
size_t l; //String len
size_t a; //Allocated
volatile long r;//Reference count
NCHAR b[1];
static int refsize(void)
{
return sizeof(size_t)+sizeof(size_t)+sizeof(long);
}
};
#define NSTRPTR nstrptr<NCHAR>
NSTRCLASS
class NSTR
{
public:
NSTR(){p=&nullstr;}
NSTR(const char *);
NSTR(const NSTR<char> &o);
NSTR(const wchar_t *);
NSTR(const NSTR<wchar_t> &o);
#ifdef _WINDOWS_
NSTR(UINT nID, int size=256);
#endif
~NSTR(){deref(p);}
NSTR &operator=(const NSTR<char> &o);
NSTR &operator=(const char *s);
NSTR &operator=(int s);
NSTR &operator=(const NSTR<wchar_t> &o);
NSTR &operator=(const wchar_t *s);
void copy(const NSTR &o){*this=o;}
void cpy(const NSTR &o){*this=o;}
void cpyn(const NCHAR *s, size_t len);
NSTR operator+(const NSTR &o)const;
NSTR operator+(const NCHAR *s)const;
NSTR &operator+=(const NSTR &o);
NSTR &operator+=(const NCHAR *s);
NSTR &operator+=(NCHAR c);
inline size_t len(void)const{return p->l;}
inline size_t count(void)const{return p->l;}
inline size_t size(void)const{return p->l;}
size_t alloc(size_t l){unsharealloc(l);return p->a;}
size_t alloc(void)const{return p->a;}
size_t optimize(void);
long referencecount(void)const{return p->r;}
NCHAR *lockbuf(size_t len=-1);
void releasebuf(size_t len=-1);
void upper(void);
void lower(void);
//Reverse for Motorola Little Endian <-> Big Endian
#if defined(NSTRWIN)
void wordswap(void){nwordswap(p);}
#endif
void limit(size_t n);
int cmp(const NSTR &o)const{return nstrcmp(p->b,o.p->b);}
int rcmp(const NSTR &o)const{return nstrcmp(o.p->b,p->b);}
int rcmpi(const NSTR &o)const{return nstrcmpi(o.p->b,p->b);}
int cmpi(const NSTR &o)const{return nstrcmpi(p->b,o.p->b);}
int cmpn(const NSTR &o, size_t l)const{return nstrcmpn(p->b,o.p->b,l);}
int cmpni(const NSTR &o, size_t l)const{return nstrcmpni(p->b,o.p->b,l);}
int cmp(const NCHAR *s)const{return nstrcmp(p->b,s);}
int rcmp(const NCHAR *s)const{return nstrcmp(s,p->b);}
int cmpi(const NCHAR *s)const{return nstrcmpi(p->b,s);}
int cmpn(const NCHAR *s, size_t l)const{return nstrcmpn(p->b,s,l);}
int cmpni(const NCHAR *s, size_t l)const{return nstrcmpni(p->b,s,l);}
//Case insensitive wildcard compare ? and *, not good for all DOS style file names without preparation
//For instance "thefile" would not match "*.*", you have to add a '.' for extension like "thefile."
bool cmpcard(const NCHAR *car, bool caseinsensitive=true)const{return ncmpcard(p->b,car,caseinsensitive);}
NSTR mid(size_t first, size_t l=-1)const;
NSTR left(size_t l)const{return mid(0,l);}
NSTR right(size_t l)const{return mid(l>p->l?0:p->l-l);};
void insert(size_t index, NCHAR c);
void insert(size_t index, const NCHAR *s);
void cut(size_t first, size_t l);
inline NCHAR lastchar(void)const{return p->l?p->b[p->l-1]:0;}
inline void ensurelast(NCHAR c){if(lastchar()!=c)(*this)+=c;}
#ifdef NSTRWIN
void reverse(void){unshare();nstrrev(p->b);}
#endif
void empty(void){(*this)=NULL;}
bool isnotempty(void)const{return p->l!=0;}
bool isempty(void)const{return p->l==0;}
bool isnull(void)const{return (p==&nullstr);}
void trimleft(const NCHAR *s=NULL);
void trimright(const NCHAR *s=NULL);
void trim(const NCHAR *s=NULL){trimright(s);trimleft(s);}
size_t find(NCHAR c, size_t start=0)const;
size_t rfind(NCHAR c, size_t start=-1)const;
size_t find(const NCHAR *s, size_t start=0)const;
size_t findi(const NCHAR *s, size_t start=0)const;
size_t findany(const NCHAR *s, size_t start=0)const;
size_t rfindany(const NCHAR *s, size_t start=-1)const;
NCHAR getat(size_t n)const{return p->b[n];}
NCHAR operator[](size_t n)const{return p->b[n];}
NCHAR operator[](int n)const{return p->b[n];}
void setat(size_t n, NCHAR c){unshare();p->b[n]=c;}
operator const NCHAR *()const{return (const NCHAR *)p->b;}
inline const NCHAR *cstr(void)const{return (const NCHAR *)p->b;}
NSTR &operator<<(NSTR &o){(*this)+=o;return *this;}
NSTR &operator<<(const NCHAR *s){(*this)+=s;return *this;}
#ifdef NSTRWIN
NSTR &operator<<(long n){NCHAR b[16];nitoa(n,b,10);(*this)+=(const NCHAR *)b;return *this;}
NSTR &operator<<(unsigned long n){NCHAR b[16];nutoa(n,b,10);(*this)+=(const NCHAR *)b;return *this;}
NSTR &operator<<(int n){NCHAR b[16];nitoa(n,b,10);(*this)+=(const NCHAR *)b;return *this;}
NSTR &operator<<(NCHAR n){(*this)+=n;return *this;}
NSTR &operator<<(unsigned int n){NCHAR b[16];nutoa(n,b,10);(*this)+=(const NCHAR *)b;return *this;}
NSTR &operator<<(__int64 n){NCHAR b[24];ni64toa(n,b,10);(*this)+=(const NCHAR *)b;return *this;}
NSTR &operator<<(unsigned __int64 n){NCHAR b[24];nui64toa(n,b,10);(*this)+=(const NCHAR *)b;return *this;}
#endif
size_t format(const NCHAR *format, ...);
bool load(FILE *fp);
#ifdef _WINDOWS_
bool load(UINT uID, int size=256)
{
int n=load(lockbuf(size),size,uID);
releasebuf();
return n!=NULL;
}
#endif
//-------------------------------------------------------
// Private section
//-------------------------------------------------------
private:
friend class NSTR<char>;
friend class NSTR<wchar_t>;
NSTRPTR *p;
NSTR(NSTRPTR *t){p=t;}
static inline size_t ninterlockeddecrement(volatile long *p)
{
#if defined(NSTRWIN)
return InterlockedDecrement(p);
#elif defined(NSTROSX)
return ::OSAtomicDecrement32((volatile int32_t *)p);
#else
(*p)--;
return *p;
#endif
}
static inline size_t ninterlockedincrement(volatile long *p)
{
#if defined(NSTRWIN)
return InterlockedIncrement(p);
#elif defined(NSTROSX)
return ::OSAtomicIncrement32((volatile int32_t *)p);
#else
(*p)++;
return *p;
#endif
}
static inline void deref(NSTRPTR *p)
{
if(p!=&nullstr)
{
if(!ninterlockeddecrement(&p->r))
{
free(p);
}
}
}
static inline void ref(NSTRPTR *p)
{
if(p!=&nullstr)
{
ninterlockedincrement(&p->r);
}
}
static NSTRPTR *newblock(size_t l)
{
size_t al=l+32;
NSTRPTR *n=(NSTRPTR *)malloc(NSTRPTR::refsize()+(al)*sizeof(NCHAR));
n->r=1;
n->a=al;
return n;
}
inline void renewblock(size_t l)
{
size_t al=l+32;
p=(NSTRPTR *)realloc(p,NSTRPTR::refsize()+(p->a=al)*sizeof(NCHAR));
}
inline static size_t nstrlen(const char *a){return strlen(a);}
inline static size_t nstrlen(const wchar_t *a){return wcslen(a);}
inline static int nstrcmp(const char *a, const char *b){return strcmp(a,b);}
inline static int nstrcmp(const wchar_t *a, const wchar_t *b){return wcscmp(a,b);}
inline static int nstrcmpn(const char *a, const char *b, size_t l){return strncmp(a,b,l);}
inline static int nstrcmpn(const wchar_t *a, const wchar_t *b, size_t l){return wcsncmp(a,b,l);}
#if defined(NSTRWIN)
inline static int nstrcmpi(const char *a, const char *b){return _stricmp(a,b);}
inline static int nstrcmpi(const wchar_t *a, const wchar_t *b){return _wcsicmp(a,b);}
inline static int nstrcmpni(const char *a, const char *b, size_t l){return _strnicmp(a,b,l);}
inline static int nstrcmpni(const wchar_t *a, const wchar_t *b, size_t l){return _wcsnicmp(a,b,l);}
#elif defined(NSTROSX)
inline static int nstrcmpi(const char *a, const char *b){return strcasecmp(a,b);}
inline static int nstrcmpi(const wchar_t *a, const wchar_t *b)
{
int n;
for(;*a||*b;a++,b++)
{
n=ntolower(*a)-ntolower(*b);
if(n)return n;
}
return 0;
}
inline static int nstrcmpni(const char *a, const char *b, size_t l){return strncasecmp(a,b,l);}
inline static int nstrcmpni(const wchar_t *a, const wchar_t *b, size_t l)
{
int n;
for(;l!=0&&(*a||*b);a++,b++,l--)
{
n=ntolower(*a)-ntolower(*b);
if(n)return n;
}
return 0;
}
#endif
inline static char ntolower(char a){return tolower(a);}
inline static wchar_t ntolower(wchar_t a){return towlower(a);}
inline static char ntoupper(char a){return toupper(a);}
inline static wchar_t ntoupper(wchar_t a){return towupper(a);}
#if defined(NSTRWIN)
inline static void nstrupr(char *a){_strupr(a);}
inline static void nstrupr(wchar_t *a){_wcsupr(a);}
inline static void nstrlwr(char *a){_strlwr(a);}
inline static void nstrlwr(wchar_t *a){_wcslwr(a);}
#else
inline static void nstrupr(NCHAR *a){while(*a){*a=ntoupper(*a);a++;}}
inline static void nstrlwr(NCHAR *a){while(*a){*a=ntolower(*a);a++;}}
#endif
#if defined(NSTRWIN)
inline static char *nstrrev(char *a){return _strrev(a);}
inline static wchar_t *nstrrev(wchar_t *a){return _wcsrev(a);}
#endif
static int nisspace(char a){return isspace(a);}
static int nisspace(wchar_t a){return iswspace(a);}
inline static char *nstrchr(const char *a, char c){return strchr((char *)a,c);}
inline static wchar_t *nstrchr(const wchar_t *a, wchar_t c){return wcschr((wchar_t *)a,c);}
inline static char *nstrstr(char *a, const char *c){return strstr(a,c);}
inline static wchar_t *nstrstr(wchar_t *a, const wchar_t *c){return wcsstr(a,c);}
inline static char *nstrpbrk(char *a, const char *c){return strpbrk(a,c);}
inline static wchar_t *nstrpbrk(wchar_t *a, const wchar_t *c){return wcspbrk(a,c);}
#if defined(NSTRWIN)
inline static char *nitoa(long n, char *s, int r){return _ltoa(n,s,r);}
inline static wchar_t *nitoa(long n, wchar_t *s, int r){return _ltow(n,s,r);}
inline static char *nutoa(long n, char *s, int r){return _ultoa(n,s,r);}
inline static wchar_t *nutoa(long n, wchar_t *s, int r){return _ultow(n,s,r);}
inline static char *ni64toa(__int64 n, char *s, int r){return _i64toa(n,s,r);}
inline static wchar_t *ni64toa(__int64 n, wchar_t *s, int r){return _i64tow(n,s,r);}
inline static char *nui64toa(unsigned __int64 n, char *s, int r){return _ui64toa(n,s,r);}
inline static wchar_t *nui64toa(unsigned __int64 n, wchar_t *s, int r){return _ui64tow(n,s,r);}
#endif
size_t _nstrprintf(const char *format, va_list argptr);
size_t _nstrprintf(const wchar_t *format, va_list argptr);
static bool ncmpcard(const NCHAR *str, const NCHAR *car, bool caseinsensitive);
static void copyconvert( nstrptr<char> *p, const char *s){memcpy((void *)p->b,(const void *)s,(p->l+1)*sizeof(NCHAR));}
static void copyconvert( nstrptr<wchar_t> *p, const wchar_t *s){memcpy((void *)p->b,(const void *)s,(p->l+1)*sizeof(NCHAR));}
static void copyconvert( nstrptr<char> *p, const wchar_t *s){if(wcstombs(p->b,s,p->l+1)==-1)*p->b=0,p->l=0;}
static void copyconvert( nstrptr<wchar_t> *p, const char *s){if(mbstowcs(p->b,s,p->l+1)==-1)*p->b=0,p->l=0;}
void assigncopyconvert( nstrptr<char> *p2, nstrptr<char> *op){ref(p=op);}
void assigncopyconvert( nstrptr<wchar_t> *p2, nstrptr<wchar_t> *op){ref(p=op);}
void assigncopyconvert( nstrptr<char> *p2, nstrptr<wchar_t> *op)
{
size_t l=nstrlen(op->b);
p=newblock(l);
p->l=l;
copyconvert(p,op->b);
}
void assigncopyconvert( nstrptr<wchar_t> *p2, nstrptr<char> *op)
{
size_t l=nstrlen(op->b);
p=newblock(l);
p->l=l;
copyconvert(p,op->b);
}
inline static void nwordswap( nstrptr<char> *p)
{
//No effect on ascii atrings
}
inline static void nwordswap( nstrptr<wchar_t> *p)
{
size_t r;
char *l,t;
for(r=0;r<p->l;r++)
{
l=(char *)&p->b[r];
t=l[0];
l[0]=l[1];
l[1]=t;
}
}
void unshare(void)
{
if(p->r>1)
{
NSTRPTR *n=newblock(p->l);
n->l=p->l;
memcpy((void *)n->b,(const void *)p->b,(p->l+1)*sizeof(NCHAR));
deref(p);
p=n;
}
}
void unsharealloc(size_t newsize)
{
if(p->r>1)
{
NSTRPTR *n=newblock(newsize);
n->l=newsize<p->l?newsize:p->l;
memcpy((void *)n->b,(const void *)p->b,(n->l+1)*sizeof(NCHAR));
deref(p);
p=n;
}
else if(newsize+1>p->a)
{
renewblock(newsize);
}
}
#ifdef _WINDOWS_
static int load(char *buf, int max, UINT uID)
{
return ::LoadStringA(GetModuleHandle(NULL),uID,buf,max);
}
static int load(wchar_t *buf, int max, UINT uID)
{
return ::LoadStringW(GetModuleHandle(NULL),uID,buf,max);
}
#endif
static nstrptr<NCHAR> nullstr;
};
NSTRCLASS static inline NSTRTYPE operator+(const NCHAR * s, const NSTRTYPE &a){return NSTRTYPE(s)+a;}
#ifdef NSTRICOMPARE
//Case insensitive operators
NSTRCLASS static inline bool operator==(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmpi(b)==false);}
NSTRCLASS static inline bool operator==(const NSTRTYPE &a,const NCHAR * s){return (a.cmpi(s)==false);}
NSTRCLASS static inline bool operator==(const NCHAR * s,const NSTRTYPE &b){return (b.rcmpi(s)==false);}
NSTRCLASS static inline bool operator!=(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmpi(b)!=false);}
NSTRCLASS static inline bool operator!=(const NSTRTYPE &a,const NCHAR * s){return (a.cmpi(s)!=false);}
NSTRCLASS static inline bool operator!=(const NCHAR * s,const NSTRTYPE &b){return (b.rcmpi(s)!=false);}
NSTRCLASS static inline bool operator<(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmpi(b)<0);}
NSTRCLASS static inline bool operator<(const NSTRTYPE &a,const NCHAR * s){return (a.cmpi(s)<0);}
NSTRCLASS static inline bool operator<(const NCHAR * s,const NSTRTYPE &b){return (b.rcmpi(s)<0);}
NSTRCLASS static inline bool operator>(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmpi(b)>0);}
NSTRCLASS static inline bool operator>(const NSTRTYPE &a,const NCHAR * s){return (a.cmpi(s)>0);}
NSTRCLASS static inline bool operator>(const NCHAR * s,const NSTRTYPE &b){return (b.rcmpi(s)>0);}
NSTRCLASS static inline bool operator<=(const NSTRTYPE &a,const NSTRTYPE &b){return !(a>b);}
NSTRCLASS static inline bool operator<=(const NSTRTYPE &a,const NCHAR * s){return !(a>s);}
NSTRCLASS static inline bool operator<=(const NCHAR * s,const NSTRTYPE &b){return !(s>b);}
NSTRCLASS static inline bool operator>=(const NSTRTYPE &a,const NSTRTYPE &b){return !(a<b);}
NSTRCLASS static inline bool operator>=(const NSTRTYPE &a,const NCHAR * s){return !(a<s);}
NSTRCLASS static inline bool operator>=(const NCHAR * s,const NSTRTYPE &b){return !(s<b);}
#else
//Case sensitive operators
NSTRCLASS static inline bool operator==(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmp(b)==false);}
NSTRCLASS static inline bool operator==(const NSTRTYPE &a,const NCHAR * s){return (a.cmp(s)==false);}
NSTRCLASS static inline bool operator==(const NCHAR * s,const NSTRTYPE &b){return (b.rcmp(s)==false);}
NSTRCLASS static inline bool operator!=(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmp(b)!=false);}
NSTRCLASS static inline bool operator!=(const NSTRTYPE &a,const NCHAR * s){return (a.cmp(s)!=false);}
NSTRCLASS static inline bool operator!=(const NCHAR * s,const NSTRTYPE &b){return (b.rcmp(s)!=false);}
NSTRCLASS static inline bool operator<(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmp(b)<0);}
NSTRCLASS static inline bool operator<(const NSTRTYPE &a,const NCHAR * s){return (a.cmp(s)<0);}
NSTRCLASS static inline bool operator<(const NCHAR * s,const NSTRTYPE &b){return (b.rcmp(s)<0);}
NSTRCLASS static inline bool operator>(const NSTRTYPE &a,const NSTRTYPE &b){return (a.cmp(b)>0);}
NSTRCLASS static inline bool operator>(const NSTRTYPE &a,const NCHAR * s){return (a.cmp(s)>0);}
NSTRCLASS static inline bool operator>(const NCHAR * s,const NSTRTYPE &b){return (b.rcmp(s)>0);}
NSTRCLASS static inline bool operator<=(const NSTRTYPE &a,const NSTRTYPE &b){return !(a>b);}
NSTRCLASS static inline bool operator<=(const NSTRTYPE &a,const NCHAR * s){return !(a>s);}
NSTRCLASS static inline bool operator<=(const NCHAR * s,const NSTRTYPE &b){return !(s>b);}
NSTRCLASS static inline bool operator>=(const NSTRTYPE &a,const NSTRTYPE &b){return !(a<b);}
NSTRCLASS static inline bool operator>=(const NSTRTYPE &a,const NCHAR * s){return !(a<s);}
NSTRCLASS static inline bool operator>=(const NCHAR * s,const NSTRTYPE &b){return !(s<b);}
#endif //NSTRICOMPARE
//------------------------------------------------------------------------------------
#ifdef _WINDOWS_
NSTRCLASS NSTRTYPE::NSTR(UINT nID, int size)
{
p=&nullstr;
load(nID,size);
}
#endif
NSTRCLASS NSTRTYPE::NSTR(const NSTR<char> &o)
{
if(o.p!=&o.nullstr)
assigncopyconvert(p,o.p);
else
p=&nullstr;
}
NSTRCLASS NSTRTYPE::NSTR(const NSTR<wchar_t> &o)
{
if(o.p!=&o.nullstr)
assigncopyconvert(p,o.p);
else
p=&nullstr;
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator=(const NSTR<char> &o)
{
deref(p);
if(o.p!=&o.nullstr)
assigncopyconvert(p,o.p);
else
p=&nullstr;
return *this;
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator=(const NSTR<wchar_t> &o)
{
deref(p);
if(o.p!=&o.nullstr)
assigncopyconvert(p,o.p);
else
p=&nullstr;
return *this;
}
NSTRCLASS NSTRTYPE::NSTR(const char *s)
{
if(!s)
p=&nullstr;
else
{
size_t l=nstrlen(s);
p=newblock(l);
p->l=l;
copyconvert(p,s);
}
}
NSTRCLASS NSTRTYPE::NSTR(const wchar_t *s)
{
if(!s)
p=&nullstr;
else
{
size_t l=nstrlen(s);
p=newblock(l);
p->l=l;
copyconvert(p,s);
}
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator=(const char *s)
{
if(!s)
{
deref(p);
p=&nullstr;
}
else
{
size_t l=nstrlen(s);
unsharealloc(l);
p->l=l;
copyconvert(p,s);
}
return *this;
}
NSTRCLASS void NSTRTYPE::cpyn(const NCHAR *s, size_t len)
{
if(!s || !len)
{
deref(p);
p=&nullstr;
}
else
{
size_t l=0;
unsharealloc(len);
while(l<len && (*s))
{
p->b[l]=*s;
l++;
s++;
}
p->l=l;
p->b[l]=0;
}
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator=(const wchar_t *s)
{
deref(p);
if(!s)
p=&nullstr;
else
{
size_t l=nstrlen(s);
p=newblock(l);
p->l=l;
copyconvert(p,s);
}
return *this;
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator=(int c)
{
if(c)
{
NCHAR b[2]={NCHAR(c),0};
(*this)=(const NCHAR *)b;
}
else
{
deref(p);
p=&nullstr;
}
return *this;
}
NSTRCLASS NSTRTYPE NSTRTYPE::operator+(const NSTR &o)const
{
size_t l=p->l+o.p->l;
NSTRPTR *n=newblock(l);
n->l=l;
memcpy((void *)n->b,(void *)p->b,p->l*sizeof(NCHAR));
memcpy((void *)(n->b+p->l),(void *)o.p->b,(o.p->l+1)*sizeof(NCHAR));
return NSTR(n);
}
NSTRCLASS NSTRTYPE NSTRTYPE::operator+(const NCHAR *s)const
{
if(!s)return *this;
size_t sl=nstrlen(s);
size_t l=p->l+sl;
NSTRPTR *n=newblock(l);
n->l=l;
memcpy(n->b,p->b,p->l*sizeof(NCHAR));
memcpy(n->b+p->l,s,(sl+1)*sizeof(NCHAR));
return NSTR(n);
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator+=(const NSTR &o)
{
size_t sl=o.p->l;
size_t l=p->l+sl;
unsharealloc(l);
memcpy(p->b+p->l,o.p->b,(sl+1)*sizeof(NCHAR));
p->l=l;
return *this;
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator+=(const NCHAR *s)
{
size_t sl=nstrlen(s);
size_t l=p->l+sl;
unsharealloc(l);
memcpy(p->b+p->l,s,(sl+1)*sizeof(NCHAR));
p->l=l;
return *this;
}
NSTRCLASS NSTRTYPE &NSTRTYPE::operator+=(NCHAR c)
{
size_t l=p->l+1;
unsharealloc(l);
p->b[p->l]=c;
p->b[l]=0;
p->l=l;
return *this;
}
NSTRCLASS NCHAR *NSTRTYPE::lockbuf(size_t len)
{
if(len==-1)len=p->l;
unsharealloc(len);
p->l=len;
return p->b;
}
NSTRCLASS void NSTRTYPE::releasebuf(size_t end)
{
if(end==-1)
{
p->b[p->a-1]=0;
p->l=nstrlen(p->b);
}
else
{
p->b[end]=0;
p->l=end;
}
}
NSTRCLASS void NSTRTYPE::upper(void)
{
unshare();
nstrupr(p->b);
}
NSTRCLASS void NSTRTYPE::lower(void)
{
unshare();
nstrlwr(p->b);
}
NSTRCLASS void NSTRTYPE::limit(size_t nl)
{
if(nl<p->l)
{
unshare();
p->b[p->l=nl]=0;
}
}
NSTRCLASS bool NSTRTYPE::ncmpcard(const NCHAR *str, const NCHAR *car, bool caseinsensitive)
{
int r,l;
const NCHAR *lop;
for(;;str++,car++)
{
switch(*car)
{
case '\0':
if(!*str)return true; //Both card and string ended, all matched.
return false; //The string didn't end, no match
case '*':
if(!car[1])return true; //The wildcard ends with a *, all match and the rest is match
for(lop=str;;lop++)
{
if(*lop)
{
if(ncmpcard(lop,car+1,caseinsensitive))return true;
}
else break; //Else skip the *
}
break;
case '?':
if(!*str)return false; //Any letter is a match but end of string
break;
default:
r=*car,l=*str;
if(caseinsensitive)
{
if(ntolower((NCHAR)r)!=ntolower((NCHAR)l))return false; //char by char comparison failed
}
else
{
if(r!=l)return false; //char by char comparison failed
}
break;
}
}
}
NSTRCLASS NSTRTYPE NSTRTYPE::mid(size_t first, size_t l)const
{
if(first>=p->l)return NSTR(&nullstr);
size_t al=p->l-first;
if(l>al)l=al;
NSTRPTR *n=newblock(l);
n->l=l;
memcpy(n->b,p->b+first,l*sizeof(NCHAR));
n->b[l]=0;
return NSTR(n);
}
NSTRCLASS void NSTRTYPE::insert(size_t index, NCHAR c)
{
size_t l=1,nl=p->l+1;
unsharealloc(nl);
memmove(p->b+index+l,p->b+index,((p->l-index)+1)*sizeof(NCHAR));
p->b[index]=c;
p->l=nl;
}
NSTRCLASS void NSTRTYPE::insert(size_t index, const NCHAR *s)
{
size_t l=nstrlen(s),nl=p->l+l;
unsharealloc(nl);
memmove(p->b+index+l,p->b+index,((p->l-index)+1)*sizeof(NCHAR));
memcpy(p->b+index,s,l*sizeof(NCHAR));
p->l=nl;
}
NSTRCLASS void NSTRTYPE::cut(size_t first, size_t l)
{
unshare();
if(first+l>p->l)l=p->l-first;
memmove(p->b+first,p->b+first+l,(p->l-(first+l)+1)*sizeof(NCHAR));
p->l-=l;
}
NSTRCLASS void NSTRTYPE::trimleft(const NCHAR *s)
{
unshare();
NCHAR *r=p->b;
if(!s)
{
while(nisspace(*r))r++;
}
else
{
while(*r)
{
if(!nstrchr(s,*r))break;
r++;
}
}
memmove(p->b,r,((p->l=(p->l-(r-p->b)))+1)*sizeof(NCHAR));
}
NSTRCLASS void NSTRTYPE::trimright(const NCHAR *s)
{
if(!p->l)return;
unshare();
NCHAR *r=p->b+p->l-1;
if(!s)
{
do
{
if(!nisspace(*r))break;
r--;
}
while(r>=p->b);
}
else
{
do
{
if(!nstrchr(s,*r))break;
r--;
}
while(r>=p->b);
}
p->l=(r+1)-p->b;
p->b[p->l]=0;
}
NSTRCLASS size_t NSTRTYPE::find(NCHAR c, size_t start)const
{
if(start<p->l)
{
NCHAR *t=nstrchr(p->b+start,c);
if(t)return (size_t)(t-p->b);
}
return -1;
}
NSTRCLASS size_t NSTRTYPE::rfind(NCHAR c, size_t start)const
{
if(start==-1)start=len()-1;
while(start!=-1)
{
if(p->b[start]==c)break;
start--;
}
return start;
}
NSTRCLASS size_t NSTRTYPE::find(const NCHAR *c, size_t start)const
{
if((int)start<p->l)
{
NCHAR *t=nstrstr(p->b+start,c);
if(t)return (int)(t-p->b);
}
return -1;
}
NSTRCLASS size_t NSTRTYPE::findi(const NCHAR *c, size_t start)const
{
size_t l=nstrlen(c);
NCHAR sstr[3]={0,0,0};
sstr[0]=tolower(c[0]);
sstr[1]=toupper(c[0]);
NCHAR *t=p->b+start;
for(;;)
{
t=nstrpbrk(t,sstr);
if(!t)break;
if(!nstrcmpni(t,c,l))return (size_t)(t-p->b);
t++;
}
return -1;
}
NSTRCLASS size_t NSTRTYPE::findany(const NCHAR *c, size_t start)const
{
if((int)start<p->l)
{
NCHAR *t=nstrpbrk(p->b+start,c);
if(t)return (size_t)(t-p->b);
}
return -1;
}
NSTRCLASS size_t NSTRTYPE::rfindany(const NCHAR *c, size_t start)const
{
if(start==-1)start=len()-1;
const NCHAR *i;
while(start!=-1)
{
i=c;
for(;*i;i++)
{
if(p->b[start]==*i)return start;
}
start--;
}
return start;
}
NSTRCLASS bool NSTRTYPE::load(FILE *fp)
{
long cur=ftell(fp),end;
if(fseek(fp,0,SEEK_END ))return false;
end=ftell(fp);
if(fseek(fp,cur,SEEK_SET ))return false;
limit(0); //Empty the string
alloc((end-cur)+8);
NCHAR *w=p->b;
int c;
if(sizeof(NCHAR)==sizeof(wchar_t))
{
for(;;w++)
{
if((c=fgetwc(fp))==WEOF)break;
*w=(NCHAR)c;
}
}
else
{
for(;;w++)
{
if((c=fgetc(fp))==EOF)break;
*w=(NCHAR)c;
}
}
*w=0;
p->l=(int)(w-p->b);
return true;
}
NSTRCLASS size_t NSTRTYPE::format(const NCHAR *format, ...)
{
va_list arglist;
va_start(arglist, format);
return _nstrprintf(format,arglist);
}
NSTRCLASS size_t NSTRTYPE::_nstrprintf(const char *format, va_list argptr)
{
#if defined(NSTRWIN)
int size=_vscprintf(format, argptr);
if(size<0)throw size;
unsharealloc(size+16);
return p->l=vsprintf_s(p->b,p->a,format,argptr);
#elif defined(NSTROSX)
int size=vsnprintf(p->b,0,format, argptr);
if(size<0)throw size;
unsharealloc(size+16);
return p->l=vsnprintf(p->b,p->a,format,argptr);
#endif
}
NSTRCLASS size_t NSTRTYPE::_nstrprintf(const wchar_t *format, va_list argptr)
{
#if defined(NSTRWIN)