summaryrefslogtreecommitdiffstats
path: root/src/kvilib/core/kvi_qstring.h
blob: c82063e93c45f5390d4e9fb8b5e6693c0b10a8f0 (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
#ifndef _KVI_QSTRING_H_
#define _KVI_QSTRING_H_

//=============================================================================
//
//   File : kvi_qstring.h
//   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 QString class
//
//=============================================================================

#include "kvi_settings.h"
#include "kvi_inttypes.h"
#include "kvi_stdarg.h"
#include "kvi_qcstring.h"

#include <qstring.h>

///
/// \namespace KviQString
///
/// \brief A namespace for QString helper functions
///
/// This namespace contains several helper functions
/// that are used when dealing with QString.
///
namespace KviQString
{
	extern KVILIB_API QString makeSizeReadable(size_t size);
	extern KVILIB_API bool equalCS(const QString &sz1,const QString &sz2);
	extern KVILIB_API bool equalCI(const QString &sz1,const QString &sz2);
	extern KVILIB_API bool equalCS(const QString &sz1,const char * sz2);
	extern KVILIB_API bool equalCI(const QString &sz1,const char * sz2);
	// sz2 is assumed to be null terminated here!
	extern KVILIB_API bool equalCI(const QString &sz1,const QChar * sz2);
	inline bool equalCS(const char * sz1,const QString &sz2)
		{ return equalCS(sz2,sz1); };
	inline bool equalCI(const char * sz1,const QString &sz2)
		{ return equalCI(sz2,sz1); };
	// sz1 is assumed to be null terminated here!
	inline bool equalCI(const QChar * sz1,const QString &sz2)
		{ return equalCI(sz2,sz1); };

	extern KVILIB_API bool equalCSN(const QString &sz1,const QString &sz2,unsigned int len);
	extern KVILIB_API bool equalCIN(const QString &sz1,const QString &sz2,unsigned int len);
	extern KVILIB_API bool equalCSN(const QString &sz1,const char * sz2,unsigned int len);
	extern KVILIB_API bool equalCIN(const QString &sz1,const char * sz2,unsigned int len);
	// sz2 is assumed to be null terminated here!
	extern KVILIB_API bool equalCIN(const QString &sz1,const QChar * sz2,unsigned int len);
	inline bool equalCSN(const char * sz1,const QString &sz2,unsigned int len)
		{ return equalCSN(sz2,sz1,len); };
	inline bool equalCIN(const char * sz1,const QString &sz2,unsigned int len)
		{ return equalCIN(sz2,sz1,len); };
	// sz1 is assumed to be null terminated here!
	inline bool equalCIN(const QChar * sz1,const QString &sz2,unsigned int len)
		{ return equalCIN(sz2,sz1,len); };

	//note that greater here means that come AFTER in the alphabetic order
	// return < 0 ---> str1 < str2
	// return = 0 ---> str1 = str2
	// return > 0 ---> str1 > str2
	extern KVILIB_API int cmpCI(const QString &sz1,const QString &sz2);
	extern KVILIB_API int cmpCIN(const QString &sz1,const QString &sz2,unsigned int len);
	extern KVILIB_API int cmpCS(const QString &sz1,const QString &sz2);

	extern KVILIB_API void detach(QString &sz);

	// this makes the QString sz appear as a null terminated array
	// it MAY RETURN 0 when the QString is null!
	extern KVILIB_API const QChar * nullTerminatedArray(const QString &sz);

	inline bool lastCharIs(QString &szString,const QChar &c)
					{ return szString.endsWith(c); };

	extern KVILIB_API void ensureLastCharIs(QString &szString,const QChar &c);

	// wild expression matching
	extern KVILIB_API bool matchWildExpressionsCI(const QString &szM1,const QString &szM2);
	// wild or regexp matching
	extern KVILIB_API bool matchStringCI(const QString &szExp,const QString &szStr,bool bIsRegExp = false,bool bExact = false);
	extern KVILIB_API bool matchStringCS(const QString &szExp,const QString &szStr,bool bIsRegExp = false,bool bExact = false);

	extern KVILIB_API void vsprintf(QString &s,const QString &szFmt,kvi_va_list list);
	extern KVILIB_API QString & sprintf(QString &s,const QString &szFmt,...);
	extern KVILIB_API void stripRightWhiteSpace(QString &s);
	extern KVILIB_API void stripLeft(QString &s,const QChar &c);
	extern KVILIB_API void stripRight(QString &s,const QChar &c);
	extern KVILIB_API void appendFormatted(QString &s,const QString &szFmt,...);
	extern KVILIB_API void appendNumber(QString &s,double dReal);
	extern KVILIB_API void appendNumber(QString &s,kvi_i64_t iInteger);
	extern KVILIB_API void appendNumber(QString &s,int iInteger);
	extern KVILIB_API void appendNumber(QString &s,unsigned int uInteger);
	extern KVILIB_API void appendNumber(QString &s,kvi_u64_t uInteger);
	
	extern KVILIB_API void cutFromFirst(QString &s,const QChar &c,bool bIncluded = true);
	extern KVILIB_API void cutFromLast(QString &s,const QChar &c,bool bIncluded = true);
	extern KVILIB_API void cutToFirst(QString &s,const QChar &c,bool bIncluded = true,bool bClearIfNotFound = false);
	extern KVILIB_API void cutToLast(QString &s,const QChar &c,bool bIncluded = true,bool bClearIfNotFound = false);
	extern KVILIB_API void cutFromFirst(QString &s,const QString &c,bool bIncluded = true);
	extern KVILIB_API void cutFromLast(QString &s,const QString &c,bool bIncluded = true);
	extern KVILIB_API void cutToFirst(QString &s,const QString &c,bool bIncluded = true,bool bClearIfNotFound = false);
	extern KVILIB_API void cutToLast(QString &s,const QString &c,bool bIncluded = true,bool bClearIfNotFound = false);
	
	extern KVILIB_API QString upperISO88591(const QString &szSrc);
	extern KVILIB_API QString lowerISO88591(const QString &szSrc);
	extern KVILIB_API QString getToken(QString &szString,const QChar &sep);
	
	extern KVILIB_API void transliterate(QString &s,const QString &szToFind,const QString &szReplacement);

	extern KVILIB_API void bufferToHex(QString &szRetBuffer,const unsigned char * buffer,unsigned int len);

	// a global empty string (note that this is ALSO NULL under Qt 3.x)
	extern KVILIB_API const QString empty;

	///
	/// A portability wrapper which with Qt3 and Qt4.
	/// Returns a lowcase version of the parameter string.
	///
	inline QString toLower(const QString &s)
	{
#ifdef COMPILE_USE_QT4
		return s.toLower();
#else
		return s.lower();
#endif
	}
	
	inline int find(const QString &s,QChar c,int index = 0,bool cs = true)
	{
#ifdef COMPILE_USE_QT4
		return s.indexOf(c,index,cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
#else
		return s.find(c,index,cs);
#endif
	}

	inline int find(const QString &s,char c,int index = 0,bool cs = true)
	{
#ifdef COMPILE_USE_QT4
		return s.indexOf(c,index,cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
#else
		return s.find(c,index,cs);
#endif
	}

	inline int find(const QString &s,const QString & str,int index = 0,bool cs = true)
	{
#ifdef COMPILE_USE_QT4
		return s.indexOf(str,index,cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
#else
		return s.find(str,index,cs);
#endif
	}

	inline int find(const QString &s,const QRegExp & rx,int index = 0)
	{
#ifdef COMPILE_USE_QT4
		return s.indexOf(rx,index);
#else
		return s.find(rx,index);
#endif
	}

	inline int find(const QString &s,const char * str,int index = 0)
	{
#ifdef COMPILE_USE_QT4
		return s.indexOf(QString(str),index);
#else
		return s.find(str,index);
#endif
	}

	inline int findRev(const QString &s,QChar c,int index = -1,bool cs = true)
	{
#ifdef COMPILE_USE_QT4
		return s.lastIndexOf(c,index,cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
#else
		return s.findRev(c,index,cs);
#endif
	}

	inline int findRev(const QString &s,char c,int index = -1,bool cs = true)
	{
#ifdef COMPILE_USE_QT4
		return s.lastIndexOf(c,index,cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
#else
		return s.findRev(c,index,cs);
#endif
	}

	inline int findRev(const QString &s,const QString & str,int index = -1,bool cs = true)
	{
#ifdef COMPILE_USE_QT4
		return s.lastIndexOf(str,index,cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
#else
		return s.findRev(str,index,cs);
#endif
	}

	inline int findRev(const QString &s,const QRegExp & rx,int index = -1)
	{
#ifdef COMPILE_USE_QT4
		return s.lastIndexOf(rx,index);
#else
		return s.findRev(rx,index);
#endif
	}

	inline int findRev(const QString &s,const char * str,int index = -1)
	{
#ifdef COMPILE_USE_QT4
		return s.lastIndexOf(QString(str),index);
#else
		return s.findRev(str,index);
#endif
	}

	inline QString trimmed(const QString &s)
	{
#ifdef COMPILE_USE_QT4
		return s.trimmed();
#else
		return s.stripWhiteSpace();
#endif
	}

	// WARNING: DO NOT USE CONSTRUCTS LIKE char * c = KviQString::toUtf8(something).data();
	//          They are dangerous since with many compilers the returned string gets destroyed
	//          at the end of the instruction and the c pointer gets thus invalidated.
	//          Use
	//           KviQCString tmp = KviQString::toUtf8(something);
	//           char * c = tmp.data(); 
	//          instead.
	//          Yes, I know that it sucks, but it's the only way to
	//          transit to qt 4.x more or less cleanly...
	inline KviQCString toUtf8(const QString &s)
	{
#ifdef COMPILE_USE_QT4
		return s.toUtf8();
#else
		return s.utf8();
#endif
	}

	inline KviQCString toLocal8Bit(const QString &s)
	{
		return s.local8Bit();
	}
	
	inline kvi_i64_t toI64(QString &szNumber,bool * bOk)
	{
#if SIZEOF_LONG_INT == 8
		return szNumber.toLong(bOk);
#else
		return szNumber.toLongLong(bOk);
#endif
	}
	
	inline kvi_u64_t toU64(QString &szNumber,bool * bOk)
	{
#if SIZEOF_LONG_INT == 8
		return szNumber.toULong(bOk);
#else
		return szNumber.toULongLong(bOk);
#endif
	}
};

// QT4SUX: Because QString::null is gone. QString() is SLOWER than QString::null since it invokes a constructor and destructor.

#endif //!_KVI_QSTRING_H_