1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
#include "QtCore/qglobal.h"
33
#include "QtCore/qatomic.h"
38
// POSIX defines truncate to truncate64
40
#error qbytearray.h must be included before any header file that defines truncate
43
/*****************************************************************************
44
Safe and portable C string functions; extensions to standard string.h
45
*****************************************************************************/
47
Q_CORE_EXPORT char *qstrdup(const char *);
49
inline uint qstrlen(const char *str)
50
{ return str ? uint(strlen(str)) : 0; }
52
Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
53
Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, uint len);
55
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
57
inline int qstrncmp(const char *str1, const char *str2, uint len)
59
return (str1 && str2) ? strncmp(str1, str2, len)
60
: (str1 ? 1 : (str2 ? -1 : 0));
62
Q_CORE_EXPORT int qstricmp(const char *, const char *);
63
Q_CORE_EXPORT int qstrnicmp(const char *, const char *, uint len);
65
// implemented in qvsnprintf.cpp
66
Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
67
Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
70
inline QT3_SUPPORT void *qmemmove(void *dst, const void *src, uint len)
71
{ return memmove(dst, src, len); }
72
inline QT3_SUPPORT uint cstrlen(const char *str)
73
{ return uint(strlen(str)); }
74
inline QT3_SUPPORT char *cstrcpy(char *dst, const char *src)
75
{ return qstrcpy(dst,src); }
76
inline QT3_SUPPORT int cstrcmp(const char *str1, const char *str2)
77
{ return strcmp(str1,str2); }
78
inline QT3_SUPPORT int cstrncmp(const char *str1, const char *str2, uint len)
79
{ return strncmp(str1,str2,len); }
82
// qChecksum: Internet checksum
84
Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len);
89
template <typename T> class QList;
91
class Q_CORE_EXPORT QByteArray
95
QByteArray(const char *);
96
QByteArray(const char *, int size);
97
QByteArray(int size, char c);
98
inline QByteArray(const QByteArray &);
101
QByteArray &operator=(const QByteArray &);
102
QByteArray &operator=(const char *str);
104
inline int size() const;
105
bool isEmpty() const;
106
void resize(int size);
108
QByteArray &fill(char c, int size = -1);
110
int capacity() const;
111
void reserve(int size);
114
operator const char *() const;
115
operator const void *() const;
117
const char *data() const;
118
inline const char *constData() const;
119
inline void detach();
120
bool isDetached() const;
123
const char at(int i) const;
124
const char operator[](int i) const;
125
QByteRef operator[](int i);
126
const char operator[](uint i) const;
127
QByteRef operator[](uint i);
129
int indexOf(char c, int from = 0) const;
130
inline int indexOf(const char *c, int from = 0) const;
131
int indexOf(const QByteArray &a, int from = 0) const;
132
int lastIndexOf(char c, int from = -1) const;
133
inline int lastIndexOf(const char *c, int from = -1) const;
134
int lastIndexOf(const QByteArray &a, int from = -1) const;
136
QBool contains(char c) const;
137
QBool contains(const char *a) const;
138
QBool contains(const QByteArray &a) const;
139
int count(char c) const;
140
int count(const char *a) const;
141
int count(const QByteArray &a) const;
143
QByteArray left(int len) const;
144
QByteArray right(int len) const;
145
QByteArray mid(int index, int len = -1) const;
147
bool startsWith(const QByteArray &a) const;
148
bool startsWith(char c) const;
149
bool startsWith(const char *c) const;
151
bool endsWith(const QByteArray &a) const;
152
bool endsWith(char c) const;
153
bool endsWith(const char *c) const;
155
void truncate(int pos);
158
QByteArray toLower() const;
159
QByteArray toUpper() const;
161
QByteArray trimmed() const;
162
QByteArray simplified() const;
163
QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
164
QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
167
inline QT3_SUPPORT QByteArray leftJustify(uint width, char fill = ' ', bool truncate = false) const
168
{ return leftJustified(int(width), fill, truncate); }
169
inline QT3_SUPPORT QByteArray rightJustify(uint width, char fill = ' ', bool truncate = false) const
170
{ return rightJustified(int(width), fill, truncate); }
173
QByteArray &prepend(char c);
174
QByteArray &prepend(const char *s);
175
QByteArray &prepend(const QByteArray &a);
176
QByteArray &append(char c);
177
QByteArray &append(const char *s);
178
QByteArray &append(const QByteArray &a);
179
QByteArray &insert(int i, char c);
180
QByteArray &insert(int i, const char *s);
181
QByteArray &insert(int i, const QByteArray &a);
182
QByteArray &remove(int index, int len);
183
QByteArray &replace(int index, int len, const char *s);
184
QByteArray &replace(int index, int len, const QByteArray &s);
185
QByteArray &replace(char before, const char *after);
186
QByteArray &replace(char before, const QByteArray &after);
187
QByteArray &replace(const char *before, const char *after);
188
QByteArray &replace(const QByteArray &before, const QByteArray &after);
189
QByteArray &replace(const QByteArray &before, const char *after);
190
QByteArray &replace(const char *before, const QByteArray &after);
191
QByteArray &replace(char before, char after);
192
QByteArray &operator+=(char c);
193
QByteArray &operator+=(const char *s);
194
QByteArray &operator+=(const QByteArray &a);
196
QList<QByteArray> split(char sep) const;
198
#ifndef QT_NO_CAST_TO_ASCII
199
QByteArray &append(const QString &s);
200
QByteArray &insert(int i, const QString &s);
201
QByteArray &replace(const QString &before, const char *after);
202
QByteArray &replace(char c, const QString &after);
203
QByteArray &replace(const QString &before, const QByteArray &after);
205
QByteArray &operator+=(const QString &s);
206
int indexOf(const QString &s, int from = 0) const;
207
int lastIndexOf(const QString &s, int from = -1) const;
209
#ifndef QT_NO_CAST_FROM_ASCII
210
inline bool operator==(const QString &s2) const;
211
inline bool operator!=(const QString &s2) const;
212
inline bool operator<(const QString &s2) const;
213
inline bool operator>(const QString &s2) const;
214
inline bool operator<=(const QString &s2) const;
215
inline bool operator>=(const QString &s2) const;
218
short toShort(bool *ok = 0, int base = 10) const;
219
ushort toUShort(bool *ok = 0, int base = 10) const;
220
int toInt(bool *ok = 0, int base = 10) const;
221
uint toUInt(bool *ok = 0, int base = 10) const;
222
qlonglong toLongLong(bool *ok = 0, int base = 10) const;
223
qulonglong toULongLong(bool *ok = 0, int base = 10) const;
224
float toFloat(bool *ok = 0) const;
225
double toDouble(bool *ok = 0) const;
226
QByteArray toBase64() const;
228
QByteArray &setNum(short, int base = 10);
229
QByteArray &setNum(ushort, int base = 10);
230
QByteArray &setNum(int, int base = 10);
231
QByteArray &setNum(uint, int base = 10);
232
QByteArray &setNum(qlonglong, int base = 10);
233
QByteArray &setNum(qulonglong, int base = 10);
234
QByteArray &setNum(float, char f = 'g', int prec = 6);
235
QByteArray &setNum(double, char f = 'g', int prec = 6);
237
static QByteArray number(int, int base = 10);
238
static QByteArray number(uint, int base = 10);
239
static QByteArray number(qlonglong, int base = 10);
240
static QByteArray number(qulonglong, int base = 10);
241
static QByteArray number(double, char f = 'g', int prec = 6);
242
static QByteArray fromRawData(const char *, int size);
243
static QByteArray fromBase64(const QByteArray &base64);
245
typedef char *iterator;
246
typedef const char *const_iterator;
247
typedef iterator Iterator;
248
typedef const_iterator ConstIterator;
250
const_iterator begin() const;
251
const_iterator constBegin() const;
253
const_iterator end() const;
254
const_iterator constEnd() const;
257
void push_back(char c);
258
void push_back(const char *c);
259
void push_back(const QByteArray &a);
260
void push_front(char c);
261
void push_front(const char *c);
262
void push_front(const QByteArray &a);
264
inline int count() const { return d->size; }
265
int length() const { return d->size; }
270
QT3_SUPPORT_CONSTRUCTOR QByteArray(int size);
271
inline QT3_SUPPORT QByteArray& duplicate(const QByteArray& a) { *this = a; return *this; }
272
inline QT3_SUPPORT QByteArray& duplicate(const char *a, uint n)
273
{ *this = QByteArray(a, n); return *this; }
274
inline QT3_SUPPORT QByteArray& setRawData(const char *a, uint n)
275
{ *this = fromRawData(a, n); return *this; }
276
inline QT3_SUPPORT void resetRawData(const char *, uint) { clear(); }
277
inline QT3_SUPPORT QByteArray lower() const { return toLower(); }
278
inline QT3_SUPPORT QByteArray upper() const { return toUpper(); }
279
inline QT3_SUPPORT QByteArray stripWhiteSpace() const { return trimmed(); }
280
inline QT3_SUPPORT QByteArray simplifyWhiteSpace() const { return simplified(); }
281
inline QT3_SUPPORT int find(char c, int from = 0) const { return indexOf(c, from); }
282
inline QT3_SUPPORT int find(const char *c, int from = 0) const { return indexOf(c, from); }
283
inline QT3_SUPPORT int find(const QByteArray &ba, int from = 0) const { return indexOf(ba, from); }
284
inline QT3_SUPPORT int findRev(char c, int from = -1) const { return lastIndexOf(c, from); }
285
inline QT3_SUPPORT int findRev(const char *c, int from = -1) const { return lastIndexOf(c, from); }
286
inline QT3_SUPPORT int findRev(const QByteArray &ba, int from = -1) const { return lastIndexOf(ba, from); }
287
#ifndef QT_NO_CAST_TO_ASCII
288
QT3_SUPPORT int find(const QString &s, int from = 0) const;
289
QT3_SUPPORT int findRev(const QString &s, int from = -1) const;
294
operator QNoImplicitBoolCast() const;
301
static Data shared_null;
302
static Data shared_empty;
304
QByteArray(Data *dd, int /*dummy*/, int /*dummy*/) : d(dd) {}
305
void realloc(int alloc);
308
friend class QByteRef;
309
friend class QString;
312
inline QByteArray::QByteArray(): d(&shared_null) { d->ref.ref(); }
313
inline QByteArray::~QByteArray() { if (!d->ref.deref()) qFree(d); }
314
inline int QByteArray::size() const
316
inline const char QByteArray::at(int i) const
317
{ Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
318
inline const char QByteArray::operator[](int i) const
319
{ Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
320
inline const char QByteArray::operator[](uint i) const
321
{ Q_ASSERT(i < uint(size())); return d->data[i]; }
322
inline bool QByteArray::isEmpty() const
323
{ return d->size == 0; }
324
inline QByteArray::operator const char *() const
326
inline QByteArray::operator const void *() const
328
inline char *QByteArray::data()
329
{ detach(); return d->data; }
330
inline const char *QByteArray::data() const
332
inline const char *QByteArray::constData() const
334
inline void QByteArray::detach()
335
{ if (d->ref != 1 || d->data != d->array) realloc(d->size); }
336
inline bool QByteArray::isDetached() const
337
{ return d->ref == 1; }
338
inline QByteArray::QByteArray(const QByteArray &a) : d(a.d)
341
inline QByteArray::QByteArray(int size) : d(&shared_null)
342
{ d->ref.ref(); if (size > 0) fill('\0', size); }
345
inline int QByteArray::capacity() const
348
inline void QByteArray::reserve(int asize)
349
{ if (d->ref != 1 || asize > d->alloc) realloc(asize); }
351
inline void QByteArray::squeeze()
352
{ if (d->size < d->alloc) realloc(d->size); }
354
class Q_CORE_EXPORT QByteRef {
357
inline QByteRef(QByteArray &array, int idx)
359
friend class QByteArray;
361
inline operator const char() const
362
{ return i < a.d->size ? a.d->data[i] : 0; }
363
inline QByteRef &operator=(char c)
364
{ if (a.d->ref != 1 || i >= a.d->size) a.expand(i);
365
a.d->data[i] = c; return *this; }
366
inline QByteRef &operator=(const QByteRef &c)
367
{ if (a.d->ref != 1 || i >= a.d->size) a.expand(i);
368
a.d->data[i] = c.a.d->data[c.i]; return *this; }
369
inline bool operator==(char c) const
370
{ return a.d->data[i] == c; }
371
inline bool operator!=(char c) const
372
{ return a.d->data[i] != c; }
373
inline bool operator>(char c) const
374
{ return a.d->data[i] > c; }
375
inline bool operator>=(char c) const
376
{ return a.d->data[i] >= c; }
377
inline bool operator<(char c) const
378
{ return a.d->data[i] < c; }
379
inline bool operator<=(char c) const
380
{ return a.d->data[i] <= c; }
383
inline QByteRef QByteArray::operator[](int i)
384
{ Q_ASSERT(i >= 0); return QByteRef(*this, i); }
385
inline QByteRef QByteArray::operator[](uint i)
386
{ return QByteRef(*this, i); }
387
inline QByteArray::iterator QByteArray::begin()
388
{ detach(); return d->data; }
389
inline QByteArray::const_iterator QByteArray::begin() const
391
inline QByteArray::const_iterator QByteArray::constBegin() const
393
inline QByteArray::iterator QByteArray::end()
394
{ detach(); return d->data + d->size; }
395
inline QByteArray::const_iterator QByteArray::end() const
396
{ return d->data + d->size; }
397
inline QByteArray::const_iterator QByteArray::constEnd() const
398
{ return d->data + d->size; }
399
inline QByteArray &QByteArray::operator+=(char c)
400
{ return append(c); }
401
inline QByteArray &QByteArray::operator+=(const char *s)
402
{ return append(s); }
403
inline QByteArray &QByteArray::operator+=(const QByteArray &a)
404
{ return append(a); }
405
inline void QByteArray::push_back(char c)
407
inline void QByteArray::push_back(const char *c)
409
inline void QByteArray::push_back(const QByteArray &a)
411
inline void QByteArray::push_front(char c)
413
inline void QByteArray::push_front(const char *c)
415
inline void QByteArray::push_front(const QByteArray &a)
417
inline QBool QByteArray::contains(const QByteArray &a) const
418
{ return QBool(indexOf(a) != -1); }
419
inline QBool QByteArray::contains(char c) const
420
{ return QBool(indexOf(c) != -1); }
421
inline bool operator==(const QByteArray &a1, const QByteArray &a2)
422
{ return (a1.size() == a2.size()) && (memcmp(a1, a2, a1.size())==0); }
423
inline bool operator==(const QByteArray &a1, const char *a2)
424
{ return a2 ? strcmp(a1,a2) == 0 : a1.isEmpty(); }
425
inline bool operator==(const char *a1, const QByteArray &a2)
426
{ return a1 ? strcmp(a1,a2) == 0 : a2.isEmpty(); }
427
inline bool operator!=(const QByteArray &a1, const QByteArray &a2)
428
{ return !(a1==a2); }
429
inline bool operator!=(const QByteArray &a1, const char *a2)
430
{ return a2 ? strcmp(a1,a2) != 0 : !a1.isEmpty(); }
431
inline bool operator!=(const char *a1, const QByteArray &a2)
432
{ return a1 ? strcmp(a1,a2) != 0 : !a2.isEmpty(); }
433
inline bool operator<(const QByteArray &a1, const QByteArray &a2)
434
{ return strcmp(a1, a2) < 0; }
435
inline bool operator<(const QByteArray &a1, const char *a2)
436
{ return qstrcmp(a1, a2) < 0; }
437
inline bool operator<(const char *a1, const QByteArray &a2)
438
{ return qstrcmp(a1, a2) < 0; }
439
inline bool operator<=(const QByteArray &a1, const QByteArray &a2)
440
{ return strcmp(a1, a2) <= 0; }
441
inline bool operator<=(const QByteArray &a1, const char *a2)
442
{ return qstrcmp(a1, a2) <= 0; }
443
inline bool operator<=(const char *a1, const QByteArray &a2)
444
{ return qstrcmp(a1, a2) <= 0; }
445
inline bool operator>(const QByteArray &a1, const QByteArray &a2)
446
{ return strcmp(a1, a2) > 0; }
447
inline bool operator>(const QByteArray &a1, const char *a2)
448
{ return qstrcmp(a1, a2) > 0; }
449
inline bool operator>(const char *a1, const QByteArray &a2)
450
{ return qstrcmp(a1, a2) > 0; }
451
inline bool operator>=(const QByteArray &a1, const QByteArray &a2)
452
{ return strcmp(a1, a2) >= 0; }
453
inline bool operator>=(const QByteArray &a1, const char *a2)
454
{ return qstrcmp(a1, a2) >= 0; }
455
inline bool operator>=(const char *a1, const QByteArray &a2)
456
{ return qstrcmp(a1, a2) >= 0; }
457
inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
458
{ return QByteArray(a1) += a2; }
459
inline const QByteArray operator+(const QByteArray &a1, const char *a2)
460
{ return QByteArray(a1) += a2; }
461
inline const QByteArray operator+(const QByteArray &a1, char a2)
462
{ return QByteArray(a1) += a2; }
463
inline const QByteArray operator+(const char *a1, const QByteArray &a2)
464
{ return QByteArray(a1) += a2; }
465
inline const QByteArray operator+(char a1, const QByteArray &a2)
466
{ return QByteArray(&a1, 1) += a2; }
467
inline int QByteArray::indexOf(const char *c, int i) const
468
{ return indexOf(fromRawData(c, qstrlen(c)), i); }
469
inline int QByteArray::lastIndexOf(const char *c, int i) const
470
{ return lastIndexOf(fromRawData(c, qstrlen(c)), i); }
471
inline QBool QByteArray::contains(const char *c) const
472
{ return contains(fromRawData(c, qstrlen(c))); }
473
inline QByteArray &QByteArray::replace(int index, int len, const char *c)
474
{ return replace(index, len, fromRawData(c, qstrlen(c))); }
475
inline QByteArray &QByteArray::replace(char before, const char *c)
476
{ return replace(before, fromRawData(c, qstrlen(c))); }
477
inline QByteArray &QByteArray::replace(const QByteArray &before, const char *c)
478
{ return replace(before, fromRawData(c, qstrlen(c))); }
479
inline QByteArray &QByteArray::replace(const char *c, const QByteArray &after)
480
{ return replace(fromRawData(c, qstrlen(c)), after); }
481
inline QByteArray &QByteArray::replace(const char *before, const char *after)
482
{ return replace(fromRawData(before, qstrlen(before)), fromRawData(after, qstrlen(after))); }
484
inline QByteArray &QByteArray::setNum(short n, int base)
485
{ return setNum(qlonglong(n), base); }
486
inline QByteArray &QByteArray::setNum(ushort n, int base)
487
{ return setNum(qulonglong(n), base); }
488
inline QByteArray &QByteArray::setNum(int n, int base)
489
{ return setNum(qlonglong(n), base); }
490
inline QByteArray &QByteArray::setNum(uint n, int base)
491
{ return setNum(qulonglong(n), base); }
492
inline QByteArray &QByteArray::setNum(float n, char f, int prec)
493
{ return setNum(double(n),f,prec); }
496
#ifndef QT_NO_DATASTREAM
497
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QByteArray &);
498
Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QByteArray &);
501
#ifndef QT_NO_COMPRESS
502
Q_CORE_EXPORT QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel = -1);
503
Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, int nbytes);
504
inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
505
{ return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
506
inline QByteArray qUncompress(const QByteArray& data)
507
{ return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
510
Q_DECLARE_TYPEINFO(QByteArray, Q_MOVABLE_TYPE);
511
Q_DECLARE_SHARED(QByteArray)
513
#endif // QBYTEARRAY_H