summaryrefslogtreecommitdiffstats
path: root/src/kvilib/core/kvi_qstring.cpp
blob: a477527b563f408adb558b387ab54d3e9303e472 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
//=============================================================================
//
//   File : kvi_qstring.cpp
//   Creation date : Mon Aug 04 2003 13:36:33 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2003-2006 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program 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 2
//   of the License, or (at your opinion) any later version.
//
//   This program 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 this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

//=============================================================================
//
// Helper functions for the TQString class
//
//=============================================================================

#define __KVILIB__


#include "kvi_qstring.h"
#include "kvi_string.h"
#include "kvi_malloc.h"
#include "kvi_locale.h"

#include <ctype.h> // for tolower()
#include <stdio.h> // for sprintf()
#include <tqregexp.h> 

// kvi_string.cpp
extern unsigned char iso88591_toLower_map[256];
extern unsigned char iso88591_toUpper_map[256];

#define MY_MIN(a,b) (((a) < (b)) ? (a) : (b))

namespace KviTQString
{
	// The global empty (and null) string
	const TQString empty;

	bool equalCSN(const TQString &sz1,const TQString &sz2,unsigned int len)
	{
		if(len == 0)return true; // assume equal
		const TQChar * c1 = sz1.unicode();
		const TQChar * c2 = sz2.unicode();
		unsigned int lmin = MY_MIN(sz1.length(),sz2.length());
		if(lmin < len)return false;
		const TQChar * c1e = c1 + len;
		
		if(!c1 || !c2)return (c1 == c2);
		
		while(c1 < c1e)
		{
			if(c1->unicode() != c2->unicode())return false;
			c1++;
			c2++;
		}
		return (c1 == c1e);
	}

	bool equalCIN(const TQString &sz1,const TQString &sz2,unsigned int len)
	{
		if(len == 0)return true; // assume equal
		const TQChar * c1 = sz1.unicode();
		const TQChar * c2 = sz2.unicode();
		unsigned int lmin = MY_MIN(sz1.length(),sz2.length());
		if(lmin < len)return false;
		const TQChar * c1e = c1 + len;

		if(!c1 || !c2)return (c1 == c2);

		while(c1 < c1e)
		{
			if(c1->lower().unicode() != c2->lower().unicode())return false;
			c1++;
			c2++;
		}
		return (c1 == c1e);
	}

	bool equalCSN(const TQString &sz1,const char * sz2,unsigned int len)
	{
		if(len == 0)return true; // assume equal
		const TQChar * c1 = sz1.unicode();
		if(sz1.length() < len)return false;
		const TQChar * c1e = c1 + len;

		if(!sz2)return !c1;
		if(!c1)return !sz2;

		while((c1 < c1e) && (*sz2))
		{
			if(c1->unicode() != *sz2)return false;
			c1++;
			sz2++;
		}
		return (c1 == c1e);
	}

	bool equalCIN(const TQString &sz1,const char * sz2,unsigned int len)
	{
		if(len == 0)return true; // assume equal
		const TQChar * c1 = sz1.unicode();
		if(sz1.length() < len)return false;
		const TQChar * c1e = c1 + len;

		if(!sz2)return !c1;
		if(!c1)return !(*sz2);

		while((c1 < c1e) && (*sz2))
		{
			if(c1->lower().unicode() != tolower(*sz2))return false;
			c1++;
			sz2++;
		}
		return (c1 == c1e);
	}

	// sz2 is assumed to be null terminated, sz1 is not!
	bool equalCIN(const TQString &sz1,const TQChar *sz2,unsigned int len)
	{
		if(len == 0)return true; // assume equal
		const TQChar * c1 = sz1.unicode();
		if(sz1.length() < len)return false;
		const TQChar * c1e = c1 + len;

		if(!sz2)return !c1;
		if(!c1)return !(sz2->unicode());

		while((c1 < c1e) && (sz2->unicode()))
		{
			if(c1->lower().unicode() != sz2->lower().unicode())return false;
			c1++;
			sz2++;
		}
		return (c1 == c1e);
	}

	TQString makeSizeReadable(size_t bytes)
	{
		double size = bytes;
		if(size<900)
			return TQString(__tr2qs("%1 bytes")).arg(size,0,'f',3);

		size/=1024;
		if(size<900)
			return TQString(__tr2qs("%1 KB")).arg(size,0,'f',3);

		size/=1024;
		if(size<900)
			return TQString(__tr2qs("%1 MB")).arg(size,0,'f',3);

		//Pirated DVD?;)
		size/=1024;
		if(size<900)
			return TQString(__tr2qs("%1 GB")).arg(size,0,'f',3);

		//Uhm.. We are downloading a whole internet:)))
		size/=1024;
		return TQString(__tr2qs("%1 TB")).arg(size,0,'f',3);
	}

	bool equalCS(const TQString &sz1,const TQString &sz2)
	{
		if(sz1.length() != sz2.length())return false;

		const TQChar * c1 = sz1.unicode();
		const TQChar * c2 = sz2.unicode();
		const TQChar * c1e = c1 + sz1.length();
		
		if(!c1 || !c2)return (c1 == c2);
		
		while(c1 < c1e)
		{
			if(c1->unicode() != c2->unicode())return false;
			c1++;
			c2++;
		}
		return (c1 == c1e);
	}

	bool equalCI(const TQString &sz1,const TQString &sz2)
	{
		if(sz1.length() != sz2.length())return false;

		const TQChar * c1 = sz1.unicode();
		const TQChar * c2 = sz2.unicode();
		const TQChar * c1e = c1 + sz1.length();

		if(!c1 || !c2)return (c1 == c2);

		while(c1 < c1e)
		{
			if(c1->lower().unicode() != c2->lower().unicode())return false;
			c1++;
			c2++;
		}
		return (c1 == c1e);
	}

	// sz2 is assumed to be null terminated, sz1 is not!
	bool equalCI(const TQString &sz1,const TQChar *sz2)
	{
		const TQChar * c1 = sz1.unicode();
		const TQChar * c1e = c1 + sz1.length();

		if(!c1 || !sz2)return (c1 == sz2);

		while(c1 < c1e)
		{
			if(!sz2->unicode())return false; // sz1 has at least another character
			if(c1->lower().unicode() != sz2->lower().unicode())return false;
			c1++;
			sz2++;
		}
		return (c1 == c1e) && (!sz2->unicode());
	}

	bool equalCS(const TQString &sz1,const char * sz2)
	{
		const TQChar * c1 = sz1.unicode();
		const TQChar * c1e = c1 + sz1.length();

		if(!c1)return !sz2;

		while((c1 < c1e) && (*sz2))
		{
			if(c1->unicode() != *sz2)return false;
			c1++;
			sz2++;
		}
		return ((c1 == c1e) && (*sz2 == '\0'));
	}

	bool equalCI(const TQString &sz1,const char * sz2)
	{
		const TQChar * c1 = sz1.unicode();
		const TQChar * c1e = c1 + sz1.length();

		if(!c1)return !sz2;

		while((c1 < c1e) && (*sz2))
		{
			if(c1->lower().unicode() != tolower(*sz2))return false;
			c1++;
			sz2++;
		}
		return ((c1 == c1e) && (*sz2 == '\0'));
	}

	int cmpCS(const TQString &sz1,const TQString &sz2)
	{
		const TQChar * c1 = sz1.unicode();
		const TQChar * c2 = sz2.unicode();
		const TQChar * c1e = c1 + sz1.length();
		const TQChar * c2e = c2 + sz2.length();

		if(!c1)
		{
			if(!c2)return 0;
			return -1;
		}
		if(!c2)return 1;


		for(;;)
		{
			if(c1 >= c1e)
			{
				if(c2 < c2e)return /* 0 */ - (c2->unicode());
				return 0;
			}
			if(c2 >= c2e)return c1->unicode() /* - 0 */;

			int diff = c1->unicode() - c2->unicode();
			if(diff)return diff;

			c1++;
			c2++;
		}

		return 0; // never here
	}

	int cmpCI(const TQString &sz1,const TQString &sz2)
	{
		const TQChar * c1 = sz1.unicode();
		const TQChar * c2 = sz2.unicode();
		const TQChar * c1e = c1 + sz1.length();
		const TQChar * c2e = c2 + sz2.length();

		if(!c1)
		{
			if(!c2)return 0;
			return -1;
		}
		if(!c2)return 1;

		for(;;)
		{
			if(c1 >= c1e)
			{
				if(c2 < c2e)return /* 0 */ - (c2->lower().unicode());
				return 0;
			}
			if(c2 >= c2e)return c1->lower().unicode() /* - 0 */;

			int diff = c1->lower().unicode() - c2->lower().unicode();
			if(diff)return diff;

			c1++;
			c2++;
		}

		return 0; // never here
	}

	int cmpCIN(const TQString &sz1,const TQString &sz2,unsigned int len)
	{
		if(len == 0)return 0; // assume equal
		unsigned int l1 = MY_MIN(len,sz1.length());
		unsigned int l = MY_MIN(l1,sz2.length()); // FIXME: THIS IS NOT OK

		const TQChar * c1 = sz1.unicode();
		const TQChar * c2 = sz2.unicode();
		const TQChar * c1e = c1 + l;

		if(!c1)
		{
			if(!c2)return 0;
			return -1;
		}
		if(!c2)return 1;

		int diff = 0;

		while((c1 < c1e) && !(diff = (c1->lower().unicode() - c2->lower().unicode())))
		{
			c1++;
			c2++;
		}

		return diff;
	}

	void ensureLastCharIs(TQString &szString,const TQChar &c)
	{
		if(!lastCharIs(szString,c))szString.append(c);
	}

	TQString getToken(TQString &szString,const TQChar &sep)
	{
		int i=0;
		while(i < szString.length())
		{
			if(szString[i] == sep)break;
			i++;
		}
		TQString ret;
		if(i == szString.length())
		{
			ret = szString;
			szString = "";
		} else {
			ret = szString.left(i);
			while(i < szString.length())
			{
				if(szString[i] != sep)break;
				i++;
			}
			if(i == szString.length())szString = "";
			else szString.remove(0,i);
		}
		return ret;
	}

	void stripRightWhiteSpace(TQString &s)
	{
		int iRemove = 0;
		while(iRemove < s.length())
		{
			if(s.at(s.length() - (iRemove + 1)).isSpace())iRemove++;
			else break;
		}
		if(iRemove > 0)s.remove(s.length() - iRemove,iRemove);
	}

	void stripRight(TQString &s,const TQChar &c)
	{
		int iRemove = 0;
		while(iRemove < s.length())
		{
			if(s.at(s.length() - (iRemove + 1)) == c)iRemove++;
			else break;
		}
		if(iRemove > 0)s.remove(s.length() - iRemove,iRemove);
	}

	void stripLeft(TQString &s,const TQChar &c)
	{
		int iRemove = 0;
		while(iRemove < s.length())
		{
			if(s[iRemove] == c)
				iRemove++;
			else
				break;
		}
		if(iRemove > 0)s.remove(0,iRemove);
	}

	void detach(TQString &sz)
	{
		sz.setLength(sz.length());
	}

	const TQChar * nullTerminatedArray(const TQString &sz)
	{
		//sz.setLength(sz.length()); // detach!
		return (const TQChar *)sz.ucs2(); // MAY BE NULL!
	}
	
	void appendNumber(TQString &s,double dReal)
	{
		char buffer[512];
		::sprintf(buffer,"%f",dReal);
		s.append(buffer);
	}
	
	void appendNumber(TQString &s,int iInteger)
	{
		char buffer[64];
		::sprintf(buffer,"%d",iInteger);
		s.append(buffer);
	}

	void appendNumber(TQString &s,kvi_i64_t iInteger)
	{
		char buffer[64];
		::sprintf(buffer,"%ld",iInteger);
		s.append(buffer);
	}
	
	void appendNumber(TQString &s,kvi_u64_t uInteger)
	{
		char buffer[64];
		::sprintf(buffer,"%lu",uInteger);
		s.append(buffer);
	}
	
	void appendNumber(TQString &s,unsigned int uInteger)
	{
		char buffer[64];
		::sprintf(buffer,"%u",uInteger);
		s.append(buffer);
	}

	void vsprintf(TQString &s,const TQString &szFmt,kvi_va_list list)
	{
#define MEMINCREMENT 32

		int reallen = 0;
		int allocsize = MEMINCREMENT;

		//s.setLength(allocsize);

		const TQChar * fmt = nullTerminatedArray(szFmt);
		if(!fmt)
		{
			s = TQString();
			return;
		}

		TQChar * buffer = (TQChar *)kvi_malloc(sizeof(TQChar) * allocsize);
		//TQChar * p = (TQChar *)s.unicode();

		char *argString;
		long argValue;
		unsigned long argUValue;
	
		//9999999999999999999999999999999\0
		char numberBuffer[32]; //enough ? 10 is enough for 32bit unsigned int...
		char *pNumBuf;
		unsigned int tmp;

		TQChar * p = buffer;

#define INCREMENT_MEM \
		{ \
			allocsize += MEMINCREMENT; \
			buffer = (TQChar *)kvi_realloc(buffer,sizeof(TQChar) * allocsize); \
			p = buffer + reallen; \
		}

#define INCREMENT_MEM_BY(numchars) \
		{ \
			allocsize += numchars + MEMINCREMENT; \
			buffer = (TQChar *)kvi_realloc(buffer,sizeof(TQChar) * allocsize); \
			p = buffer + reallen; \
		}


		for(; fmt->unicode() ; ++fmt)
		{
			if(reallen == allocsize)INCREMENT_MEM

			//copy up to a '%'
			if(fmt->unicode() != '%')
			{
				*p++ = *fmt;
				reallen++;
				continue;
			}

			++fmt; //skip this '%'
			switch(fmt->unicode())
			{
				case 's': // char * string
				{
					argString = kvi_va_arg(list,char *);
					if(!argString)argString = "[!NULL!]";
					TQString str(argString);
					if(str.isEmpty())continue;
					int len = str.length();
					const TQChar * ch = str.unicode();
					if(!ch)continue;
					if((allocsize - reallen) < len)INCREMENT_MEM_BY(len)
					while(len--)*p++ = *ch++;
					reallen += str.length();
					continue;
				}
				case 'S': // KviStr * string
				{
					KviStr * str = kvi_va_arg(list,KviStr *);
					if(!str)continue;
					if((allocsize - reallen) < str->len())INCREMENT_MEM_BY(str->len())
					argString = str->ptr();
					while(*argString)*p++ = TQChar(*argString++);
					reallen += str->len();
					continue;
				}
				case 'Q': // TQString * string
				{
					TQString * str = kvi_va_arg(list,TQString *);
					if(!str)continue;
					if(str->isEmpty())continue;
					int len = str->length();
					const TQChar * ch = str->unicode();
					if(!ch)continue;
					if((allocsize - reallen) < len)INCREMENT_MEM_BY(len)
					while(len--)*p++ = *ch++;
					reallen += str->length();
					continue;
				}
				case 'c': //char
				{
					//
					// I'm not sure about this...
					// In the linux kernel source the
					// unsigned char is extracted from an integer type.
					// We assume that gcc stacks a char argument
					// as sizeof(int) bytes value.
					// Is this always true ?
					//
					*p++ = (char)kvi_va_arg(list,int);
					reallen++;
					continue;
				}
				case 'q': // TQChar *
				{
					//
					// I'm not sure about this...
					// In the linux kernel source the
					// unsigned char is extracted from an integer type.
					// We assume that gcc stacks a char argument
					// as sizeof(int) bytes value.
					// Is this always true ?
					//
					*p++ = *((TQChar *)kvi_va_arg(list,TQChar *));
					reallen++;
					continue;
				}
				case 'd': //signed integer
				{
					argValue = kvi_va_arg(list,int);
					if(argValue < 0)
					{ //negative integer
						*p++ = '-';
						reallen++;
						argValue = -argValue; //need to have it positive
						// most negative integer exception (avoid completely senseless (non digit) responses)
						if(argValue < 0)argValue = 0;  //we get -0 here
					}
					//write the number in a temporary buffer
					pNumBuf = numberBuffer;
					do {
						tmp = argValue / 10;
						*pNumBuf++ = argValue - (tmp * 10) + '0';
					} while((argValue = tmp));
					//copy now....
					argUValue = pNumBuf - numberBuffer; //length of the number string
					if((allocsize - reallen) < (int)argUValue)INCREMENT_MEM_BY(argUValue)
					do { *p++ = TQChar(*--pNumBuf); } while(pNumBuf != numberBuffer);
					reallen += argUValue;
					continue;
				}
				case 'u': //unsigned integer
				{
					argUValue = kvi_va_arg(list,unsigned int); //many implementations place int here
					//write the number in a temporary buffer
					pNumBuf = numberBuffer;
					do {
						tmp = argUValue / 10;
						*pNumBuf++ = argUValue - (tmp * 10) + '0';
					} while((argUValue = tmp));
					//copy now....
					argValue = pNumBuf - numberBuffer; //length of the number string
					if((allocsize - reallen) < argValue)INCREMENT_MEM_BY(argValue)
					do { *p++ = *--pNumBuf; } while(pNumBuf != numberBuffer);
					reallen += argValue;
					continue;
				}
				case 'h':
				case 'x': // hexadecimal unsigned integer
				{
					static char hexsmalldigits[]="0123456789abcdef";
					argUValue = kvi_va_arg(list,unsigned int); //many implementations place int here
					//write the number in a temporary buffer
					pNumBuf = numberBuffer;
					do {
						tmp = argUValue / 16;
						*pNumBuf++ = hexsmalldigits[argUValue - (tmp * 16)];
					} while((argUValue = tmp));
					//copy now....
					argValue = pNumBuf - numberBuffer; //length of the number string
					if((allocsize - reallen) < argValue)INCREMENT_MEM_BY(argValue)
					do { *p++ = *--pNumBuf; } while(pNumBuf != numberBuffer);
					reallen += argValue;
					continue;
				}
				case 'H':
				case 'X': // hexadecimal unsigned integer
				{
					static char hexbigdigits[]="0123456789ABCDEF";
					argUValue = kvi_va_arg(list,unsigned int); //many implementations place int here
					//write the number in a temporary buffer
					pNumBuf = numberBuffer;
					do {
						tmp = argUValue / 16;
						*pNumBuf++ = hexbigdigits[argUValue - (tmp * 16)];
					} while((argUValue = tmp));
					//copy now....
					argValue = pNumBuf - numberBuffer; //length of the number string
					if((allocsize - reallen) < argValue)INCREMENT_MEM_BY(argValue)
					do { *p++ = *--pNumBuf; } while(pNumBuf != numberBuffer);
					reallen += argValue;
					continue;
				}
				default: //a normal percent followed by some char
				{
					*p++ = '%';  //write it
					reallen++;
					if(fmt->unicode())
					{
						if(reallen == allocsize)INCREMENT_MEM
						*p++ = *fmt;
						reallen++;
					}
					continue;
				}
			}
		}

		s.setUnicode(buffer,reallen);
		kvi_free(buffer);
		//s.squeeze();
	}


	TQString & sprintf(TQString &s,const TQString &szFmt,...)
	{
		kvi_va_list list;
		kvi_va_start_by_reference(list,szFmt);
		//print...with max 256 chars
		KviTQString::vsprintf(s,szFmt,list);
		kvi_va_end(list);
		return s;
	}

	void appendFormatted(TQString &s,const TQString &szFmt,...)
	{
		TQString tmp;
		kvi_va_list list;
		kvi_va_start_by_reference(list,szFmt);
		//print...with max 256 chars
		KviTQString::vsprintf(tmp,szFmt,list);
		kvi_va_end(list);
		s.append(tmp);
	}

	bool matchWildExpressionsCI(const TQString &szM1,const TQString &szM2)
	{
		//Matches two regular expressions containging wildcards (* and ?)
	
		//          s1
		//          m1
		// mask1 : *xor
		// mask2 : xorand*xor
		//         m2
		//          s2
	
		//                        s2
		//                       m2
		//                       | 
		// XorT!xor@111.111.111.11
		//
		// *!*@*.net
		//      |
		//      m1
		//      s1
		//

		const TQChar * m1 = (const TQChar *)szM1.ucs2();
		const TQChar * m2 = (const TQChar *)szM2.ucs2();

		if(!(m1 && m2 && (m1->unicode())))return false;
		const TQChar * savePos1 = 0;
		const TQChar * savePos2 = m2;
		while(m1->unicode())
		{
			//loop managed by m1 (initially first mask)
			if(m1->unicode()=='*')
			{
				//Found a wildcard in m1
				savePos1 = ++m1;            //move to the next char and save the position...this is our jolly
				if(!savePos1->unicode())return true;  //last was a wildcard , matches everything ahead...
				savePos2 = m2+1;            //next return state for the second string
				continue;                   //and return
			}
			if(!m2->unicode())return false;         //m2 finished and we had something to match here!
			if(m1->lower()==m2->lower())
			{
				//chars matched
				m1++;                       //Go ahead in the two strings
				m2++;                       //
				if((!(m1->unicode())) && m2->unicode() && savePos1)
				{
					//m1 finished , but m2 not yet and we have a savePosition for m1 (there was a wildcard)...
					//retry matching the string following the * from the savePos2 (one char ahead last time)
					m1 = savePos1;          //back to char after wildcard
					m2 = savePos2;          //back to last savePos2
					savePos2++;             //next savePos2 will be next char
				}
			} else {
				if(m2->unicode() == '*')
				{
					//A wlidcard in the second string
					//Invert the game : mask1 <-> mask2
					//mask2 now leads the game...
					savePos1 = m1;          //aux
					m1 = m2;                //...swap
					m2 = savePos1;          //...swap
					savePos1 = m1;          //sync save pos1
					savePos2 = m2 + 1;      //sync save pos2
					continue;               //...and again
				}
				// m1 != m2 , m1 != * , m2 != *
				if((m1->unicode() == '?') || (m2->unicode() == '?'))
				{
					m1++;
					m2++;
					if((!(m1->unicode())) && m2->unicode() && savePos1)
					{
						//m1 finished , but m2 not yet and we have a savePosition for m1 (there was a wildcard)...
						//retry matching the string following the * from the savePos2 (one char ahead last time)
						m1 = savePos1;          //back to char after wildcard
						m2 = savePos2;          //back to last savePos2
						savePos2++;             //next savePos2 will be next char
					}
				} else {
					if(savePos1)
					{
						//Have a jolly man...allow not matching...
						m1 = savePos1;          //go back to char after wildcard...need to rematch...
						m2 = savePos2;          //back to last savePos2
						savePos2++;             //and set next savePos2
					} else return false;        //No previous wildcards...not matched!
				}
			}
		}
		return (!(m2->unicode()));           //m1 surely finished , so for the match , m2 must be finished too
	}

	bool matchStringCI(const TQString &szExp,const TQString &szStr,bool bIsRegExp,bool bExact)
	{
		TQString szWildcard;
		TQChar* ptr=(TQChar*)szExp.ucs2();
		if(!ptr) return 0;
		while(ptr->unicode())
		{
			if((ptr->unicode()=='[') || (ptr->unicode()==']'))
			{
				szWildcard.append("[");
				szWildcard.append(*ptr);
				szWildcard.append("]");
			} else {
				szWildcard.append(*ptr);
			}
			ptr++;
		}
		TQRegExp re(szWildcard,false,!bIsRegExp);
		if(bExact) return re.exactMatch(szStr);
		return re.search(szStr) != -1;
	}

	bool matchStringCS(const TQString &szExp,const TQString &szStr,bool bIsRegExp,bool bExact)
	{
		TQString szWildcard;
		TQChar* ptr=(TQChar*)szExp.ucs2();
		if(!ptr) return 0;
		while(ptr->unicode())
		{
			if((ptr->unicode()=='[')) // <-- hum ?
			{
				szWildcard.append("[");
				szWildcard.append(*ptr);
				szWildcard.append("]");
			} else {
				szWildcard.append(*ptr);
			}
			ptr++;
		}
		TQRegExp re(szWildcard,true,!bIsRegExp);
		if(bExact) return re.exactMatch(szStr);
		return re.search(szStr) != -1;
	}

	void cutFromFirst(TQString &s,const TQChar &c,bool bIncluded)
	{
		int idx = s.find(c);
		if(idx == -1)return;
		s.truncate(bIncluded ? idx : idx + 1);
	}

	void cutFromLast(TQString &s,const TQChar &c,bool bIncluded)
	{
		int idx = s.findRev(c);
		if(idx == -1)return;
		s.truncate(bIncluded ? idx : idx + 1);
	}
	
	void cutToFirst(TQString &s,const TQChar &c,bool bIncluded,bool bClearIfNotFound)
	{
		int idx = s.find(c);
		if(idx == -1)
		{
			if(bClearIfNotFound)s = "";
			return;
		}
		s.remove(0,bIncluded ? idx + 1 : idx);
	}
	
	void cutToLast(TQString &s,const TQChar &c,bool bIncluded,bool bClearIfNotFound)
	{
		int idx = s.findRev(c);
		if(idx == -1)
		{
			if(bClearIfNotFound)s = "";
			return;
		}
		s.remove(0,bIncluded ? idx + 1 : idx);
	}

	void cutFromFirst(TQString &s,const TQString &c,bool bIncluded)
	{
		int idx = s.find(c);
		if(idx == -1)return;
		s.truncate(bIncluded ? idx : idx + c.length());
	}

	void cutFromLast(TQString &s,const TQString &c,bool bIncluded)
	{
		int idx = s.findRev(c);
		if(idx == -1)return;
		s.truncate(bIncluded ? idx : idx + c.length());
	}
	
	void cutToFirst(TQString &s,const TQString &c,bool bIncluded,bool bClearIfNotFound)
	{
		int idx = s.find(c);
		if(idx == -1)
		{
			if(bClearIfNotFound)s = "";
			return;
		}
		s.remove(0,bIncluded ? idx + c.length() : idx);
	}
	
	void cutToLast(TQString &s,const TQString &c,bool bIncluded,bool bClearIfNotFound)
	{
		int idx = s.findRev(c);
		if(idx == -1)
		{
			if(bClearIfNotFound)s = "";
			return;
		}
		s.remove(0,bIncluded ? idx + c.length() : idx);
	}

	TQString upperISO88591(const TQString &szSrc)
	{
		const TQChar * c = nullTerminatedArray(szSrc);
		if(!c)
		{
			TQString ret;
			return ret;
		}
		TQChar * buffer = (TQChar *)kvi_malloc(sizeof(TQChar) * szSrc.length());
		TQChar * b = buffer;
		unsigned short us = c->unicode();
		while(us)
		{
			if(us < 256)
				*b=TQChar((unsigned short)iso88591_toUpper_map[us]);
			else
				*b = *c;
			c++;
			b++;
			us = c->unicode();
		}
		TQString ret(buffer,szSrc.length());
		kvi_free(buffer);
		return ret;
	}
	
	TQString lowerISO88591(const TQString &szSrc)
	{
		const TQChar * c = nullTerminatedArray(szSrc);
		if(!c)
		{
			TQString ret;
			return ret;
		}
		TQChar * buffer = (TQChar *)kvi_malloc(sizeof(TQChar) * szSrc.length());
		TQChar * b = buffer;
		unsigned short us = c->unicode();
		while(us)
		{
			if(us < 256)
			{
				*b=TQChar((unsigned short)iso88591_toLower_map[us]);
			} else
				*b = *c;
			c++;
			b++;
			us = c->unicode();
		}
		TQString ret(buffer,szSrc.length());
		kvi_free(buffer);
		return ret;
	}

	void transliterate(TQString &s,const TQString &szToFind,const TQString &szReplacement)
	{
		int i=0;
		int il = MY_MIN(szToFind.length(),szReplacement.length());
		while(i < il)
		{
			int k=0;
			int kl = s.length();
			while(k < kl)
			{
				if(s[k] == szToFind[i])s[k] = szReplacement[i];
				k++;
			}
			i++;
		}
	}
	
	static char hexdigits[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
	
	void bufferToHex(TQString &szRetBuffer,const unsigned char * buffer,unsigned int len)
	{
		szRetBuffer.setLength(len * 2);
		unsigned int i=0;
		while(i < (len*2))
		{
			szRetBuffer[int(i)] = TQChar( (unsigned int) hexdigits[(*buffer) / 16] );
			i++;
			szRetBuffer[int(i)] = TQChar( (unsigned int)hexdigits[(*buffer) % 16] );
			i++;
			buffer++;
		}
	}
};