Fix for UBSan build
[platform/upstream/doxygen.git] / qtools / qcstring.h
1 /****************************************************************************
2 ** 
3 **
4 ** Definition of the extended char array operations,
5 ** and QByteArray and QCString classes
6 **
7 ** Created : 920609
8 **
9 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
10 **
11 ** This file is part of the tools module of the Qt GUI Toolkit.
12 **
13 ** This file may be distributed under the terms of the Q Public License
14 ** as defined by Trolltech AS of Norway and appearing in the file
15 ** LICENSE.QPL included in the packaging of this file.
16 **
17 ** This file may be distributed and/or modified under the terms of the
18 ** GNU General Public License version 2 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.GPL included in the
20 ** packaging of this file.
21 **
22 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
23 ** licenses may use this file in accordance with the Qt Commercial License
24 ** Agreement provided with the Software.
25 **
26 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
27 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 **
29 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
30 **   information about Qt Commercial License Agreements.
31 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
32 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
33 **
34 ** Contact info@trolltech.com if any conditions of this licensing are
35 ** not clear to you.
36 **
37 **********************************************************************/
38
39 #ifndef QCSTRING_H
40 #define QCSTRING_H
41
42 #ifndef QT_H
43 #include "qarray.h"
44 #endif // QT_H
45
46 #include <stdlib.h>
47 #include <string.h>
48
49 #if defined(_OS_SUN_) && defined(_CC_GNU_)
50 #include <strings.h>
51 #endif
52
53
54 class QGString;
55
56 /*****************************************************************************
57   Fixes and workarounds for some platforms
58  *****************************************************************************/
59
60 #if defined(_OS_HPUX_)
61 // HP-UX has badly defined strstr() etc.
62 // ### fix in 3.0: change hack_* to qt_hack_*
63 // by the way HP-UX is probably right, the standard has evolved and
64 // we'll have to adapt to it
65 inline char *hack_strstr( const char *s1, const char *s2 )
66 { return (char *)strstr(s1, s2); }
67 inline char *hack_strchr( const char *s, int c )
68 { return (char *)strchr(s, c); }
69 inline char *hack_strrchr( const char *s, int c )
70 { return (char *)strrchr(s, c); }
71 #define strstr(s1,s2)   hack_strstr((s1),(s2))
72 #define strchr(s,c)     hack_strchr((s),(c))
73 #define strrchr(s,c)    hack_strrchr((s),(c))
74 #endif
75
76
77 /*****************************************************************************
78   Safe and portable C string functions; extensions to standard string.h
79  *****************************************************************************/
80
81 Q_EXPORT void *qmemmove( void *dst, const void *src, uint len );
82
83 #if defined(_OS_SUN_) || defined(_CC_OC_)
84 #define memmove(s1,s2,n) qmemmove((s1),(s2),(n))
85 #endif
86
87 Q_EXPORT char *qstrdup( const char * );
88
89 Q_EXPORT inline uint cstrlen( const char *str )
90 { return (uint)strlen(str); }
91
92 Q_EXPORT inline uint qstrlen( const char *str )
93 { return str ? (uint)strlen(str) : 0; }
94
95 Q_EXPORT inline char *cstrcpy( char *dst, const char *src )
96 { return strcpy(dst,src); }
97
98 Q_EXPORT inline char *qstrcpy( char *dst, const char *src )
99 { return src ? strcpy(dst, src) : 0; }
100
101 Q_EXPORT char *qstrncpy( char *dst, const char *src, uint len );
102
103 Q_EXPORT inline int cstrcmp( const char *str1, const char *str2 )
104 { return strcmp(str1,str2); }
105
106 Q_EXPORT inline int qstrcmp( const char *str1, const char *str2 )
107 { return (str1 && str2) ? strcmp(str1,str2) : (int)((long)str2 - (long)str1); }
108
109 Q_EXPORT inline int cstrncmp( const char *str1, const char *str2, uint len )
110 { return strncmp(str1,str2,len); }
111
112 Q_EXPORT inline int qstrncmp( const char *str1, const char *str2, uint len )
113 { return (str1 && str2) ? strncmp(str1,str2,len) :
114                           (int)((long)str2 - (long)str1); }
115
116 Q_EXPORT int qstricmp( const char *, const char * );
117
118 Q_EXPORT int qstrnicmp( const char *, const char *, uint len );
119
120 // ### TODO for 3.0: these and the cstr* functions should be used if
121 //                   !defined(QT_CLEAN_NAMESPACE)
122 //                   We want to keep source compatibility for 2.x
123 // ### TODO for 4.0: completely remove these and the cstr* functions
124
125 #if !defined(QT_GENUINE_STR)
126
127 #undef  strlen
128 #define strlen qstrlen
129
130 #undef  strcpy
131 #define strcpy qstrcpy
132
133 #undef  strcmp
134 #define strcmp qstrcmp
135
136 #undef  strncmp
137 #define strncmp qstrncmp
138
139 #undef  stricmp
140 #define stricmp  qstricmp
141
142 #undef  strnicmp
143 #define strnicmp qstrnicmp
144
145 #endif
146
147 // qChecksum: Internet checksum
148
149 Q_EXPORT Q_UINT16 qChecksum( const char *s, uint len );
150
151 /*****************************************************************************
152   QByteArray class
153  *****************************************************************************/
154
155 #if defined(Q_TEMPLATEDLL)
156 template class Q_EXPORT QArray<char>;
157 #endif
158 typedef QArray<char> QByteArray;
159
160
161 /*****************************************************************************
162   QByteArray stream functions
163  *****************************************************************************/
164 #ifndef QT_NO_DATASTREAM
165 Q_EXPORT QDataStream &operator<<( QDataStream &, const QByteArray & );
166 Q_EXPORT QDataStream &operator>>( QDataStream &, QByteArray & );
167 #endif
168
169 class QRegExp;
170
171 /** This is an alternative implementation of QCString. It provides basically
172  *  the same functions but uses less memory for administration. This class
173  *  is just a wrapper around a plain C string requiring only 4 bytes "overhead".
174  *  QCString features sharing of data and stores the string length, but 
175  *  requires 4 + 12 bytes for this (even for the empty string). As doxygen 
176  *  uses a LOT of string during a run it saves a lot of memory to use a 
177  *  more memory efficient implementation at the cost of relatively low
178  *  runtime overhead.
179  */
180 class QCString 
181 {
182 public:
183     QCString() : m_data(0) {}           // make null string
184     QCString( const QCString &s );
185     QCString( int size );
186     QCString( const char *str );
187     QCString( const char *str, uint maxlen );
188     ~QCString();
189
190     QCString    &operator=( const QCString &s );// deep copy
191     QCString    &operator=( const char *str );  // deep copy
192
193     bool        isNull()        const;
194     bool        isEmpty()       const;
195     uint        length()        const;
196     uint        size()          const { return m_data ? length()+1 : 0; }
197     char *      data()          const { return m_data; }
198     bool        resize( uint newlen );
199     bool        truncate( uint pos );
200     bool        fill( char c, int len = -1 );
201
202     QCString    copy()  const;
203
204     QCString    &sprintf( const char *format, ... );
205
206     int         find( char c, int index=0, bool cs=TRUE ) const;
207     int         find( const char *str, int index=0, bool cs=TRUE ) const;
208     int         find( const QCString &str, int index=0, bool cs=TRUE ) const;
209     int         find( const QRegExp &, int index=0 ) const;
210     int         findRev( char c, int index=-1, bool cs=TRUE) const;
211     int         findRev( const char *str, int index=-1, bool cs=TRUE) const;
212     int         findRev( const QRegExp &, int index=-1 ) const;
213     int         contains( char c, bool cs=TRUE ) const;
214     int         contains( const char *str, bool cs=TRUE ) const;
215     int         contains( const QRegExp & ) const;
216     bool        stripPrefix(const char *prefix);
217
218     QCString    left( uint len )  const;
219     QCString    right( uint len ) const;
220     QCString    mid( uint index, uint len=0xffffffff) const;
221
222     QCString    lower() const;
223     QCString    upper() const;
224
225     QCString    stripWhiteSpace()       const;
226     QCString    simplifyWhiteSpace()    const;
227
228     QCString    &assign( const char *str );
229     QCString    &insert( uint index, const char * );
230     QCString    &insert( uint index, char );
231     QCString    &append( const char *s );
232     QCString    &prepend( const char *s );
233     QCString    &remove( uint index, uint len );
234     QCString    &replace( uint index, uint len, const char * );
235     QCString    &replace( const QRegExp &, const char * );
236
237     short       toShort( bool *ok=0 )   const;
238     ushort      toUShort( bool *ok=0 )  const;
239     int         toInt( bool *ok=0 )     const;
240     uint        toUInt( bool *ok=0 )    const;
241     long        toLong( bool *ok=0 )    const;
242     ulong       toULong( bool *ok=0 )   const;
243
244     QCString    &setNum( short );
245     QCString    &setNum( ushort );
246     QCString    &setNum( int );
247     QCString    &setNum( uint );
248     QCString    &setNum( long );
249     QCString    &setNum( ulong );
250     QCString    &setNum( float, char f='g', int prec=6 );
251     QCString    &setNum( double, char f='g', int prec=6 );
252
253                 operator const char *() const;
254     QCString    &operator+=( const char *str );
255     QCString    &operator+=( char c );
256     char &at( uint index ) const;
257     char &operator[]( int i ) const { return at(i); }
258     
259   private:
260     static void msg_index( uint );
261     void duplicate( const QCString &s );
262     void duplicate( const char *str);
263     QCString &duplicate( const char *str, int);
264
265     char *      m_data;
266 };
267
268 inline char &QCString::at( uint index ) const
269 {
270   return m_data[index];
271 }
272
273 inline void QCString::duplicate( const QCString &s )
274 {
275   if (!s.isEmpty()) 
276   {
277     uint l = strlen(s.data());
278     m_data = (char *)malloc(l+1);
279     if (m_data) memcpy(m_data,s.data(),l+1);
280   }
281   else 
282     m_data=0; 
283 }
284
285 inline void QCString::duplicate( const char *str)
286 {
287   if (str && str[0]!='\0') 
288   {
289     uint l = strlen(str);
290     m_data = (char *)malloc(l+1);
291     if (m_data) memcpy(m_data,str,l+1);
292   }
293   else 
294     m_data=0;
295 }
296
297 inline QCString &QCString::duplicate( const char *str, int)
298 {
299   if (m_data==str) return *this;
300   if (m_data) free(m_data);
301   duplicate(str);
302   return *this;
303 }
304
305 /*****************************************************************************
306   QCString stream functions
307  *****************************************************************************/
308 #ifndef QT_NO_DATASTREAM
309 Q_EXPORT QDataStream &operator<<( QDataStream &, const QCString & );
310 Q_EXPORT QDataStream &operator>>( QDataStream &, QCString & );
311 #endif
312
313 /*****************************************************************************
314   QCString inline functions
315  *****************************************************************************/
316
317 inline QCString &QCString::operator=( const QCString &s )
318 { return (QCString&)assign( s ); }
319
320 inline QCString &QCString::operator=( const char *str )
321 { return (QCString&)duplicate( str, qstrlen(str)+1 ); }
322
323 inline bool QCString::isNull() const
324 { return data() == 0; }
325
326 inline bool QCString::isEmpty() const
327 { return data() == 0 || *data() == '\0'; }
328
329 inline uint QCString::length() const
330 { return qstrlen( data() ); }
331
332 inline bool QCString::truncate( uint pos )
333 { return resize(pos+1); }
334
335 inline QCString QCString::copy() const
336 { return QCString( data() ); }
337
338 inline QCString &QCString::prepend( const char *s )
339 { return insert(0,s); }
340
341 inline QCString &QCString::append( const char *s )
342 { return operator+=(s); }
343
344 inline QCString &QCString::setNum( short n )
345 { return setNum((long)n); }
346
347 inline QCString &QCString::setNum( ushort n )
348 { return setNum((ulong)n); }
349
350 inline QCString &QCString::setNum( int n )
351 { return setNum((long)n); }
352
353 inline QCString &QCString::setNum( uint n )
354 { return setNum((ulong)n); }
355
356 inline QCString &QCString::setNum( float n, char f, int prec )
357 { return setNum((double)n,f,prec); }
358
359 inline QCString::operator const char *() const
360 { return (const char *)data(); }
361
362
363 /*****************************************************************************
364   QCString non-member operators
365  *****************************************************************************/
366
367 Q_EXPORT inline bool operator==( const QCString &s1, const QCString &s2 )
368 { return qstrcmp(s1.data(),s2.data()) == 0; }
369
370 Q_EXPORT inline bool operator==( const QCString &s1, const char *s2 )
371 { return qstrcmp(s1.data(),s2) == 0; }
372
373 Q_EXPORT inline bool operator==( const char *s1, const QCString &s2 )
374 { return qstrcmp(s1,s2.data()) == 0; }
375
376 Q_EXPORT inline bool operator!=( const QCString &s1, const QCString &s2 )
377 { return qstrcmp(s1.data(),s2.data()) != 0; }
378
379 Q_EXPORT inline bool operator!=( const QCString &s1, const char *s2 )
380 { return qstrcmp(s1.data(),s2) != 0; }
381
382 Q_EXPORT inline bool operator!=( const char *s1, const QCString &s2 )
383 { return qstrcmp(s1,s2.data()) != 0; }
384
385 Q_EXPORT inline bool operator<( const QCString &s1, const QCString& s2 )
386 { return qstrcmp(s1.data(),s2.data()) < 0; }
387
388 Q_EXPORT inline bool operator<( const QCString &s1, const char *s2 )
389 { return qstrcmp(s1.data(),s2) < 0; }
390
391 Q_EXPORT inline bool operator<( const char *s1, const QCString &s2 )
392 { return qstrcmp(s1,s2.data()) < 0; }
393
394 Q_EXPORT inline bool operator<=( const QCString &s1, const char *s2 )
395 { return qstrcmp(s1.data(),s2) <= 0; }
396
397 Q_EXPORT inline bool operator<=( const char *s1, const QCString &s2 )
398 { return qstrcmp(s1,s2.data()) <= 0; }
399
400 Q_EXPORT inline bool operator>( const QCString &s1, const char *s2 )
401 { return qstrcmp(s1.data(),s2) > 0; }
402
403 Q_EXPORT inline bool operator>( const char *s1, const QCString &s2 )
404 { return qstrcmp(s1,s2.data()) > 0; }
405
406 Q_EXPORT inline bool operator>=( const QCString &s1, const char *s2 )
407 { return qstrcmp(s1.data(),s2) >= 0; }
408
409 Q_EXPORT inline bool operator>=( const char *s1, const QCString &s2 )
410 { return qstrcmp(s1,s2.data()) >= 0; }
411
412 Q_EXPORT inline QCString operator+( const QCString &s1, const QCString &s2 )
413 {
414     QCString tmp(s1);
415     tmp += s2;
416     return tmp;
417 }
418
419
420 inline QCString operator+( const QCString &s1, const QGString &s2 );
421 inline QCString operator+( const QGString &s1, const QCString &s2 );
422
423
424 Q_EXPORT inline QCString operator+( const QCString &s1, const char *s2 )
425 {
426     QCString tmp(s1);
427     tmp += s2;
428     return tmp;
429 }
430
431 Q_EXPORT inline QCString operator+( const char *s1, const QCString &s2 )
432 {
433     QCString tmp(s1);
434     tmp += s2;
435     return tmp;
436 }
437
438 Q_EXPORT inline QCString operator+( const QCString &s1, char c2 )
439 {
440     QCString tmp( s1.data() );
441     tmp += c2;
442     return tmp;
443 }
444
445 Q_EXPORT inline QCString operator+( char c1, const QCString &s2 )
446 {
447     QCString tmp;
448     tmp += c1;
449     tmp += s2;
450     return tmp;
451 }
452
453 inline const char *qPrint(const char *s)
454 {
455   if (s) return s; else return "";
456 }
457
458 inline const char *qPrint(const QCString &s)
459 {
460   if (!s.isEmpty()) return s.data(); else return "";
461 }
462
463
464 #endif // QCSTRING_H