Qt 4.8
qbytearray.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QBYTEARRAY_H
43 #define QBYTEARRAY_H
44 
45 #include <QtCore/qatomic.h>
46 #include <QtCore/qnamespace.h>
47 
48 #include <string.h>
49 #include <stdarg.h>
50 
51 #ifdef truncate
52 #error qbytearray.h must be included before any header file that defines truncate
53 #endif
54 
55 #if defined(Q_CC_GNU) && (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
56 //There is a bug in GCC 4.0 that tries to instantiate template of annonymous enum
57 # ifdef QT_USE_FAST_OPERATOR_PLUS
58 # undef QT_USE_FAST_OPERATOR_PLUS
59 # endif
60 # ifdef QT_USE_QSTRINGBUILDER
61 # undef QT_USE_QSTRINGBUILDER
62 # endif
63 
64 #endif
65 
66 
68 
70 
71 QT_MODULE(Core)
72 
73 /*****************************************************************************
74  Safe and portable C string functions; extensions to standard string.h
75  *****************************************************************************/
76 
77 Q_CORE_EXPORT char *qstrdup(const char *);
78 
79 inline uint qstrlen(const char *str)
80 { return str ? uint(strlen(str)) : 0; }
81 
82 inline uint qstrnlen(const char *str, uint maxlen)
83 {
84  uint length = 0;
85  if (str) {
86  while (length < maxlen && *str++)
87  length++;
88  }
89  return length;
90 }
91 
92 Q_CORE_EXPORT char *qstrcpy(char *dst, const char *src);
93 Q_CORE_EXPORT char *qstrncpy(char *dst, const char *src, uint len);
94 
95 Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2);
96 Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const QByteArray &str2);
97 Q_CORE_EXPORT int qstrcmp(const QByteArray &str1, const char *str2);
98 static inline int qstrcmp(const char *str1, const QByteArray &str2)
99 { return -qstrcmp(str2, str1); }
100 
101 inline int qstrncmp(const char *str1, const char *str2, uint len)
102 {
103  return (str1 && str2) ? strncmp(str1, str2, len)
104  : (str1 ? 1 : (str2 ? -1 : 0));
105 }
106 Q_CORE_EXPORT int qstricmp(const char *, const char *);
107 Q_CORE_EXPORT int qstrnicmp(const char *, const char *, uint len);
108 
109 // implemented in qvsnprintf.cpp
110 Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap);
111 Q_CORE_EXPORT int qsnprintf(char *str, size_t n, const char *fmt, ...);
112 
113 #ifdef QT3_SUPPORT
114 inline QT3_SUPPORT void *qmemmove(void *dst, const void *src, uint len)
115 { return memmove(dst, src, len); }
116 inline QT3_SUPPORT uint cstrlen(const char *str)
117 { return uint(strlen(str)); }
118 inline QT3_SUPPORT char *cstrcpy(char *dst, const char *src)
119 { return qstrcpy(dst,src); }
120 inline QT3_SUPPORT int cstrcmp(const char *str1, const char *str2)
121 { return strcmp(str1,str2); }
122 inline QT3_SUPPORT int cstrncmp(const char *str1, const char *str2, uint len)
123 { return strncmp(str1,str2,len); }
124 #endif
125 
126 // qChecksum: Internet checksum
127 
128 Q_CORE_EXPORT quint16 qChecksum(const char *s, uint len);
129 
130 class QByteRef;
131 class QString;
132 class QDataStream;
133 template <typename T> class QList;
134 
136 {
137 private:
138  struct Data {
140  int alloc, size;
141  // ### Qt 5.0: We need to add the missing capacity bit
142  // (like other tool classes have), to maintain the
143  // reserved memory on resize.
144  char *data;
145  char array[1];
146  };
147 
148 public:
149  inline QByteArray();
150  QByteArray(const char *);
151  QByteArray(const char *, int size);
152  QByteArray(int size, char c);
153  QByteArray(int size, Qt::Initialization);
154  inline QByteArray(const QByteArray &);
155  inline ~QByteArray();
156 
157  QByteArray &operator=(const QByteArray &);
158  QByteArray &operator=(const char *str);
159 #ifdef Q_COMPILER_RVALUE_REFS
160  inline QByteArray &operator=(QByteArray &&other)
161  { qSwap(d, other.d); return *this; }
162 #endif
163 
164  inline void swap(QByteArray &other) { qSwap(d, other.d); }
165 
166  inline int size() const;
167  bool isEmpty() const;
168  void resize(int size);
169 
170  QByteArray &fill(char c, int size = -1);
171 
172  int capacity() const;
173  void reserve(int size);
174  void squeeze();
175 
176 #ifndef QT_NO_CAST_FROM_BYTEARRAY
177  operator const char *() const;
178  operator const void *() const;
179 #endif
180  char *data();
181  const char *data() const;
182  inline const char *constData() const;
183  inline void detach();
184  bool isDetached() const;
185  inline bool isSharedWith(const QByteArray &other) const { return d == other.d; }
186  void clear();
187 
188 #ifdef Q_COMPILER_MANGLES_RETURN_TYPE
189  const char at(int i) const;
190  const char operator[](int i) const;
191  const char operator[](uint i) const;
192 #else
193  char at(int i) const;
194  char operator[](int i) const;
195  char operator[](uint i) const;
196 #endif
197  QByteRef operator[](int i);
198  QByteRef operator[](uint i);
199 
200  int indexOf(char c, int from = 0) const;
201  int indexOf(const char *c, int from = 0) const;
202  int indexOf(const QByteArray &a, int from = 0) const;
203  int lastIndexOf(char c, int from = -1) const;
204  int lastIndexOf(const char *c, int from = -1) const;
205  int lastIndexOf(const QByteArray &a, int from = -1) const;
206 
207  QBool contains(char c) const;
208  QBool contains(const char *a) const;
209  QBool contains(const QByteArray &a) const;
210  int count(char c) const;
211  int count(const char *a) const;
212  int count(const QByteArray &a) const;
213 
214  QByteArray left(int len) const;
215  QByteArray right(int len) const;
216  QByteArray mid(int index, int len = -1) const;
217 
218  bool startsWith(const QByteArray &a) const;
219  bool startsWith(char c) const;
220  bool startsWith(const char *c) const;
221 
222  bool endsWith(const QByteArray &a) const;
223  bool endsWith(char c) const;
224  bool endsWith(const char *c) const;
225 
226  void truncate(int pos);
227  void chop(int n);
228 
229  QByteArray toLower() const;
230  QByteArray toUpper() const;
231 
232  QByteArray trimmed() const;
233  QByteArray simplified() const;
234  QByteArray leftJustified(int width, char fill = ' ', bool truncate = false) const;
235  QByteArray rightJustified(int width, char fill = ' ', bool truncate = false) const;
236 
237 #ifdef QT3_SUPPORT
238  inline QT3_SUPPORT QByteArray leftJustify(uint width, char aFill = ' ', bool aTruncate = false) const
239  { return leftJustified(int(width), aFill, aTruncate); }
240  inline QT3_SUPPORT QByteArray rightJustify(uint width, char aFill = ' ', bool aTruncate = false) const
241  { return rightJustified(int(width), aFill, aTruncate); }
242 #endif
243 
244  QByteArray &prepend(char c);
245  QByteArray &prepend(const char *s);
246  QByteArray &prepend(const char *s, int len);
247  QByteArray &prepend(const QByteArray &a);
248  QByteArray &append(char c);
249  QByteArray &append(const char *s);
250  QByteArray &append(const char *s, int len);
251  QByteArray &append(const QByteArray &a);
252  QByteArray &insert(int i, char c);
253  QByteArray &insert(int i, const char *s);
254  QByteArray &insert(int i, const char *s, int len);
255  QByteArray &insert(int i, const QByteArray &a);
256  QByteArray &remove(int index, int len);
257  QByteArray &replace(int index, int len, const char *s);
258  QByteArray &replace(int index, int len, const char *s, int alen);
259  QByteArray &replace(int index, int len, const QByteArray &s);
260  QByteArray &replace(char before, const char *after);
261  QByteArray &replace(char before, const QByteArray &after);
262  QByteArray &replace(const char *before, const char *after);
263  QByteArray &replace(const char *before, int bsize, const char *after, int asize);
264  QByteArray &replace(const QByteArray &before, const QByteArray &after);
265  QByteArray &replace(const QByteArray &before, const char *after);
266  QByteArray &replace(const char *before, const QByteArray &after);
267  QByteArray &replace(char before, char after);
268  QByteArray &operator+=(char c);
269  QByteArray &operator+=(const char *s);
270  QByteArray &operator+=(const QByteArray &a);
271 
272  QList<QByteArray> split(char sep) const;
273 
274  QByteArray repeated(int times) const;
275 
276 #ifndef QT_NO_CAST_TO_ASCII
277  QT_ASCII_CAST_WARN QByteArray &append(const QString &s);
278  QT_ASCII_CAST_WARN QByteArray &insert(int i, const QString &s);
279  QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const char *after);
280  QT_ASCII_CAST_WARN QByteArray &replace(char c, const QString &after);
281  QT_ASCII_CAST_WARN QByteArray &replace(const QString &before, const QByteArray &after);
282 
284  QT_ASCII_CAST_WARN int indexOf(const QString &s, int from = 0) const;
285  QT_ASCII_CAST_WARN int lastIndexOf(const QString &s, int from = -1) const;
286 #endif
287 #ifndef QT_NO_CAST_FROM_ASCII
288  inline QT_ASCII_CAST_WARN bool operator==(const QString &s2) const;
289  inline QT_ASCII_CAST_WARN bool operator!=(const QString &s2) const;
290  inline QT_ASCII_CAST_WARN bool operator<(const QString &s2) const;
291  inline QT_ASCII_CAST_WARN bool operator>(const QString &s2) const;
292  inline QT_ASCII_CAST_WARN bool operator<=(const QString &s2) const;
293  inline QT_ASCII_CAST_WARN bool operator>=(const QString &s2) const;
294 #endif
295 
296  short toShort(bool *ok = 0, int base = 10) const;
297  ushort toUShort(bool *ok = 0, int base = 10) const;
298  int toInt(bool *ok = 0, int base = 10) const;
299  uint toUInt(bool *ok = 0, int base = 10) const;
300  long toLong(bool *ok = 0, int base = 10) const;
301  ulong toULong(bool *ok = 0, int base = 10) const;
302  qlonglong toLongLong(bool *ok = 0, int base = 10) const;
303  qulonglong toULongLong(bool *ok = 0, int base = 10) const;
304  float toFloat(bool *ok = 0) const;
305  double toDouble(bool *ok = 0) const;
306  QByteArray toBase64() const;
307  QByteArray toHex() const;
308  QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
309  const QByteArray &include = QByteArray(),
310  char percent = '%') const;
311 
312  QByteArray &setNum(short, int base = 10);
313  QByteArray &setNum(ushort, int base = 10);
314  QByteArray &setNum(int, int base = 10);
315  QByteArray &setNum(uint, int base = 10);
316  QByteArray &setNum(qlonglong, int base = 10);
317  QByteArray &setNum(qulonglong, int base = 10);
318  QByteArray &setNum(float, char f = 'g', int prec = 6);
319  QByteArray &setNum(double, char f = 'g', int prec = 6);
320  QByteArray &setRawData(const char *a, uint n); // ### Qt 5: use an int
321 
322  static QByteArray number(int, int base = 10);
323  static QByteArray number(uint, int base = 10);
324  static QByteArray number(qlonglong, int base = 10);
325  static QByteArray number(qulonglong, int base = 10);
326  static QByteArray number(double, char f = 'g', int prec = 6);
327  static QByteArray fromRawData(const char *, int size);
328  static QByteArray fromBase64(const QByteArray &base64);
329  static QByteArray fromHex(const QByteArray &hexEncoded);
330  static QByteArray fromPercentEncoding(const QByteArray &pctEncoded, char percent = '%');
331 
332 
333  typedef char *iterator;
334  typedef const char *const_iterator;
335  typedef iterator Iterator;
336  typedef const_iterator ConstIterator;
337  iterator begin();
338  const_iterator begin() const;
339  const_iterator constBegin() const;
340  iterator end();
341  const_iterator end() const;
342  const_iterator constEnd() const;
343 
344  // stl compatibility
345  typedef const char & const_reference;
346  typedef char & reference;
347  typedef char value_type;
348  void push_back(char c);
349  void push_back(const char *c);
350  void push_back(const QByteArray &a);
351  void push_front(char c);
352  void push_front(const char *c);
353  void push_front(const QByteArray &a);
354 
355  inline int count() const { return d->size; }
356  int length() const { return d->size; }
357  bool isNull() const;
358 
359  // compatibility
360 #ifdef QT3_SUPPORT
361  QT3_SUPPORT_CONSTRUCTOR QByteArray(int size);
362  inline QT3_SUPPORT QByteArray& duplicate(const QByteArray& a) { *this = a; return *this; }
363  inline QT3_SUPPORT QByteArray& duplicate(const char *a, uint n)
364  { *this = QByteArray(a, n); return *this; }
365  inline QT3_SUPPORT void resetRawData(const char *, uint) { clear(); }
366  inline QT3_SUPPORT QByteArray lower() const { return toLower(); }
367  inline QT3_SUPPORT QByteArray upper() const { return toUpper(); }
368  inline QT3_SUPPORT QByteArray stripWhiteSpace() const { return trimmed(); }
369  inline QT3_SUPPORT QByteArray simplifyWhiteSpace() const { return simplified(); }
370  inline QT3_SUPPORT int find(char c, int from = 0) const { return indexOf(c, from); }
371  inline QT3_SUPPORT int find(const char *c, int from = 0) const { return indexOf(c, from); }
372  inline QT3_SUPPORT int find(const QByteArray &ba, int from = 0) const { return indexOf(ba, from); }
373  inline QT3_SUPPORT int findRev(char c, int from = -1) const { return lastIndexOf(c, from); }
374  inline QT3_SUPPORT int findRev(const char *c, int from = -1) const { return lastIndexOf(c, from); }
375  inline QT3_SUPPORT int findRev(const QByteArray &ba, int from = -1) const { return lastIndexOf(ba, from); }
376 #ifndef QT_NO_CAST_TO_ASCII
377  QT3_SUPPORT int find(const QString &s, int from = 0) const;
378  QT3_SUPPORT int findRev(const QString &s, int from = -1) const;
379 #endif
380 #endif
381 
382 private:
383  operator QNoImplicitBoolCast() const;
386  Data *d;
387  QByteArray(Data *dd, int /*dummy*/, int /*dummy*/) : d(dd) {}
388  void realloc(int alloc);
389  void expand(int i);
390  QByteArray nulTerminated() const;
391 
392  friend class QByteRef;
393  friend class QString;
394  friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);
395 public:
396  typedef Data * DataPtr;
397  inline DataPtr &data_ptr() { return d; }
398 };
399 
401 inline QByteArray::~QByteArray() { if (!d->ref.deref()) qFree(d); }
402 inline int QByteArray::size() const
403 { return d->size; }
404 
405 #ifdef Q_COMPILER_MANGLES_RETURN_TYPE
406 inline const char QByteArray::at(int i) const
407 { Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
408 inline const char QByteArray::operator[](int i) const
409 { Q_ASSERT(i >= 0 && i < size()); return d->data[i]; }
410 inline const char QByteArray::operator[](uint i) const
411 { Q_ASSERT(i < uint(size())); return d->data[i]; }
412 #else
413 inline char QByteArray::at(int i) const
414 { Q_ASSERT(uint(i) < uint(size())); return d->data[i]; }
415 inline char QByteArray::operator[](int i) const
416 { Q_ASSERT(uint(i) < uint(size())); return d->data[i]; }
417 inline char QByteArray::operator[](uint i) const
418 { Q_ASSERT(i < uint(size())); return d->data[i]; }
419 #endif
420 
421 inline bool QByteArray::isEmpty() const
422 { return d->size == 0; }
423 #ifndef QT_NO_CAST_FROM_BYTEARRAY
424 inline QByteArray::operator const char *() const
425 { return d->data; }
426 inline QByteArray::operator const void *() const
427 { return d->data; }
428 #endif
429 inline char *QByteArray::data()
430 { detach(); return d->data; }
431 inline const char *QByteArray::data() const
432 { return d->data; }
433 inline const char *QByteArray::constData() const
434 { return d->data; }
435 inline void QByteArray::detach()
436 { if (d->ref != 1 || d->data != d->array) realloc(d->size); }
437 inline bool QByteArray::isDetached() const
438 { return d->ref == 1; }
439 inline QByteArray::QByteArray(const QByteArray &a) : d(a.d)
440 { d->ref.ref(); }
441 #ifdef QT3_SUPPORT
442 inline QByteArray::QByteArray(int aSize) : d(&shared_null)
443 { d->ref.ref(); if (aSize > 0) fill('\0', aSize); }
444 #endif
445 
446 inline int QByteArray::capacity() const
447 { return d->alloc; }
448 
449 inline void QByteArray::reserve(int asize)
450 { if (d->ref != 1 || asize > d->alloc) realloc(asize); }
451 
452 inline void QByteArray::squeeze()
453 { if (d->size < d->alloc) realloc(d->size); }
454 
457  int i;
458  inline QByteRef(QByteArray &array, int idx)
459  : a(array),i(idx) {}
460  friend class QByteArray;
461 public:
462 #ifdef Q_COMPILER_MANGLES_RETURN_TYPE
463  inline operator const char() const
464  { return i < a.d->size ? a.d->data[i] : char(0); }
465 #else
466  inline operator char() const
467  { return i < a.d->size ? a.d->data[i] : char(0); }
468 #endif
469  inline QByteRef &operator=(char c)
470  { if (i >= a.d->size) a.expand(i); else a.detach();
471  a.d->data[i] = c; return *this; }
472  inline QByteRef &operator=(const QByteRef &c)
473  { if (i >= a.d->size) a.expand(i); else a.detach();
474  a.d->data[i] = c.a.d->data[c.i]; return *this; }
475  inline bool operator==(char c) const
476  { return a.d->data[i] == c; }
477  inline bool operator!=(char c) const
478  { return a.d->data[i] != c; }
479  inline bool operator>(char c) const
480  { return a.d->data[i] > c; }
481  inline bool operator>=(char c) const
482  { return a.d->data[i] >= c; }
483  inline bool operator<(char c) const
484  { return a.d->data[i] < c; }
485  inline bool operator<=(char c) const
486  { return a.d->data[i] <= c; }
487 };
488 
490 { Q_ASSERT(i >= 0); return QByteRef(*this, i); }
492 { return QByteRef(*this, i); }
494 { detach(); return d->data; }
496 { return d->data; }
498 { return d->data; }
500 { detach(); return d->data + d->size; }
502 { return d->data + d->size; }
504 { return d->data + d->size; }
506 { return append(c); }
507 inline QByteArray &QByteArray::operator+=(const char *s)
508 { return append(s); }
510 { return append(a); }
511 inline void QByteArray::push_back(char c)
512 { append(c); }
513 inline void QByteArray::push_back(const char *c)
514 { append(c); }
515 inline void QByteArray::push_back(const QByteArray &a)
516 { append(a); }
517 inline void QByteArray::push_front(char c)
518 { prepend(c); }
519 inline void QByteArray::push_front(const char *c)
520 { prepend(c); }
522 { prepend(a); }
524 { return QBool(indexOf(a) != -1); }
525 inline QBool QByteArray::contains(char c) const
526 { return QBool(indexOf(c) != -1); }
527 inline bool operator==(const QByteArray &a1, const QByteArray &a2)
528 { return (a1.size() == a2.size()) && (memcmp(a1.constData(), a2.constData(), a1.size())==0); }
529 inline bool operator==(const QByteArray &a1, const char *a2)
530 { return a2 ? qstrcmp(a1,a2) == 0 : a1.isEmpty(); }
531 inline bool operator==(const char *a1, const QByteArray &a2)
532 { return a1 ? qstrcmp(a1,a2) == 0 : a2.isEmpty(); }
533 inline bool operator!=(const QByteArray &a1, const QByteArray &a2)
534 { return !(a1==a2); }
535 inline bool operator!=(const QByteArray &a1, const char *a2)
536 { return a2 ? qstrcmp(a1,a2) != 0 : !a1.isEmpty(); }
537 inline bool operator!=(const char *a1, const QByteArray &a2)
538 { return a1 ? qstrcmp(a1,a2) != 0 : !a2.isEmpty(); }
539 inline bool operator<(const QByteArray &a1, const QByteArray &a2)
540 { return qstrcmp(a1, a2) < 0; }
541  inline bool operator<(const QByteArray &a1, const char *a2)
542 { return qstrcmp(a1, a2) < 0; }
543 inline bool operator<(const char *a1, const QByteArray &a2)
544 { return qstrcmp(a1, a2) < 0; }
545 inline bool operator<=(const QByteArray &a1, const QByteArray &a2)
546 { return qstrcmp(a1, a2) <= 0; }
547 inline bool operator<=(const QByteArray &a1, const char *a2)
548 { return qstrcmp(a1, a2) <= 0; }
549 inline bool operator<=(const char *a1, const QByteArray &a2)
550 { return qstrcmp(a1, a2) <= 0; }
551 inline bool operator>(const QByteArray &a1, const QByteArray &a2)
552 { return qstrcmp(a1, a2) > 0; }
553 inline bool operator>(const QByteArray &a1, const char *a2)
554 { return qstrcmp(a1, a2) > 0; }
555 inline bool operator>(const char *a1, const QByteArray &a2)
556 { return qstrcmp(a1, a2) > 0; }
557 inline bool operator>=(const QByteArray &a1, const QByteArray &a2)
558 { return qstrcmp(a1, a2) >= 0; }
559 inline bool operator>=(const QByteArray &a1, const char *a2)
560 { return qstrcmp(a1, a2) >= 0; }
561 inline bool operator>=(const char *a1, const QByteArray &a2)
562 { return qstrcmp(a1, a2) >= 0; }
563 #if !defined(QT_USE_QSTRINGBUILDER)
564 inline const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
565 { return QByteArray(a1) += a2; }
566 inline const QByteArray operator+(const QByteArray &a1, const char *a2)
567 { return QByteArray(a1) += a2; }
568 inline const QByteArray operator+(const QByteArray &a1, char a2)
569 { return QByteArray(a1) += a2; }
570 inline const QByteArray operator+(const char *a1, const QByteArray &a2)
571 { return QByteArray(a1) += a2; }
572 inline const QByteArray operator+(char a1, const QByteArray &a2)
573 { return QByteArray(&a1, 1) += a2; }
574 #endif // QT_USE_QSTRINGBUILDER
575 inline QBool QByteArray::contains(const char *c) const
576 { return QBool(indexOf(c) != -1); }
577 inline QByteArray &QByteArray::replace(char before, const char *c)
578 { return replace(&before, 1, c, qstrlen(c)); }
579 inline QByteArray &QByteArray::replace(const QByteArray &before, const char *c)
580 { return replace(before.constData(), before.size(), c, qstrlen(c)); }
581 inline QByteArray &QByteArray::replace(const char *before, const char *after)
582 { return replace(before, qstrlen(before), after, qstrlen(after)); }
583 
584 inline QByteArray &QByteArray::setNum(short n, int base)
585 { return setNum(qlonglong(n), base); }
587 { return setNum(qulonglong(n), base); }
588 inline QByteArray &QByteArray::setNum(int n, int base)
589 { return setNum(qlonglong(n), base); }
591 { return setNum(qulonglong(n), base); }
592 inline QByteArray &QByteArray::setNum(float n, char f, int prec)
593 { return setNum(double(n),f,prec); }
594 
595 
596 #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
599 #endif
600 
601 #ifndef QT_NO_COMPRESS
602 Q_CORE_EXPORT QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel = -1);
603 Q_CORE_EXPORT QByteArray qUncompress(const uchar* data, int nbytes);
604 inline QByteArray qCompress(const QByteArray& data, int compressionLevel = -1)
605 { return qCompress(reinterpret_cast<const uchar *>(data.constData()), data.size(), compressionLevel); }
607 { return qUncompress(reinterpret_cast<const uchar*>(data.constData()), data.size()); }
608 #endif
609 
612 
614 
616 
617 #ifdef QT_USE_QSTRINGBUILDER
618 #include <QtCore/qstring.h>
619 #endif
620 
621 #endif // QBYTEARRAY_H
#define QT_ASCII_CAST_WARN
Definition: qglobal.h:1144
double d
Definition: qnumeric_p.h:62
bool operator>=(char c) const
Definition: qbytearray.h:481
Data * DataPtr
Definition: qbytearray.h:396
bool operator<(char c) const
Definition: qbytearray.h:483
int qstrncmp(const char *str1, const char *str2, uint len)
Definition: qbytearray.h:101
QByteArray & fill(char c, int size=-1)
Sets every byte in the byte array to character ch.
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
friend class QByteRef
Definition: qbytearray.h:392
QByteRef & operator=(char c)
Definition: qbytearray.h:469
#define QT_MODULE(x)
Definition: qglobal.h:2783
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
void swap(QByteArray &other)
Swaps byte array other with this byte array.
Definition: qbytearray.h:164
int QNoImplicitBoolCast
Definition: qglobal.h:1181
QT_ASCII_CAST_WARN bool operator==(const QString &s2) const
Returns true if this byte array is equal to string str; otherwise returns false.
Definition: qstring.h:981
Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes)
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
bool operator>(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:551
const char * const_iterator
Definition: qbytearray.h:334
QByteArray & append(char c)
Appends the character ch to this byte array.
Data * d
Definition: qbytearray.h:386
bool operator!=(char c) const
Definition: qbytearray.h:477
const_iterator constBegin() const
Definition: qbytearray.h:497
char operator[](int i) const
Same as at(i).
Definition: qbytearray.h:415
Q_CORE_EXPORT void qFree(void *ptr)
Definition: qmalloc.cpp:58
#define at(className, varName)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
QBasicAtomicInt ref
Definition: qbytearray.h:139
QByteArray()
Constructs an empty byte array.
Definition: qbytearray.h:400
bool operator<=(char c) const
Definition: qbytearray.h:485
void squeeze()
Releases any memory not required to store the array&#39;s data.
Definition: qbytearray.h:452
long ASN1_INTEGER_get ASN1_INTEGER * a
int count() const
Same as size().
Definition: qbytearray.h:355
QByteArray & prepend(char c)
Prepends the character ch to this byte array.
The QString class provides a Unicode character string.
Definition: qstring.h:83
int qstrcmp(const char *str1, const char *str2)
A safe strcmp() function.
Definition: qbytearray.cpp:231
uint qstrnlen(const char *str, uint maxlen)
Definition: qbytearray.h:82
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const uint base
Definition: qurl.cpp:268
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Returns a byte array that is the result of concatenating byte array a1 and byte array a2...
Definition: qbytearray.h:564
bool operator<=(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:545
#define Q_DECLARE_SHARED(TYPE)
Definition: qglobal.h:2214
char * qstrcpy(char *dst, const char *src)
Copies all the characters up to and including the &#39;\0&#39; from src into dst and returns a pointer to dst...
Definition: qbytearray.cpp:137
void realloc(int alloc)
static Data shared_null
Definition: qstring.h:616
static char toHex(quint8 c)
Definition: qurl.cpp:4440
unsigned char uchar
Definition: qglobal.h:994
bool operator==(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:527
QT_ASCII_CAST_WARN bool operator>=(const QString &s2) const
Returns true if this byte array is greater than or equal to string str; otherwise returns false...
Definition: qstring.h:991
quint16 qChecksum(const char *data, uint len)
Returns the CRC-16 checksum of the first len bytes of data.
Definition: qbytearray.cpp:434
QByteArray(Data *dd, int, int)
Definition: qbytearray.h:387
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
~QByteArray()
Destroys the byte array.
Definition: qbytearray.h:401
static bool isEmpty(const char *str)
char value_type
Definition: qbytearray.h:347
QByteRef(QByteArray &array, int idx)
Definition: qbytearray.h:458
QT_ASCII_CAST_WARN bool operator!=(const QString &s2) const
Returns true if this byte array is not equal to string str; otherwise returns false.
Definition: qstring.h:983
bool operator>(char c) const
Definition: qbytearray.h:479
static int toInt(const QByteArray &str)
Definition: generator.cpp:167
unsigned short quint16
Definition: qglobal.h:936
void push_back(char c)
Same as append(ch).
Definition: qbytearray.h:511
QByteArray & a
Definition: qbytearray.h:456
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
void expand(int i)
QByteArray qCompress(const QByteArray &data, int compressionLevel)
Compresses the data byte array and returns the compressed data in a new byte array.
Definition: qbytearray.h:604
static void split(QT_FT_Vector *b)
int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
A portable vsnprintf() function.
Definition: qvsnprintf.cpp:78
int qstrnicmp(const char *str1, const char *str2, uint len)
A safe strnicmp() function.
Definition: qbytearray.cpp:316
unsigned long ulong
Definition: qglobal.h:997
int indexOf(char c, int from=0) const
Returns the index position of the first occurrence of the character ch in the byte array...
int qstricmp(const char *str1, const char *str2)
A safe stricmp() function.
Definition: qbytearray.cpp:279
const char & const_reference
Definition: qbytearray.h:345
QDataStream & operator<<(QDataStream &out, const QByteArray &ba)
Writes byte array ba to the stream out and returns a reference to the stream.
QT_ASCII_CAST_WARN bool operator>(const QString &s2) const
Returns true if this byte array is lexically greater than string str; otherwise returns false...
Definition: qstring.h:987
bool isSharedWith(const QByteArray &other) const
Definition: qbytearray.h:185
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
QByteRef & operator=(const QByteRef &c)
Definition: qbytearray.h:472
bool isDetached() const
Definition: qbytearray.h:437
bool operator!=(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:533
char * iterator
Definition: qbytearray.h:333
int length() const
Same as size().
Definition: qbytearray.h:356
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
const_iterator ConstIterator
Definition: qbytearray.h:336
const_iterator constEnd() const
Definition: qbytearray.h:503
DataPtr & data_ptr()
Definition: qbytearray.h:397
QString trimmed(QString source)
Definition: generator.cpp:233
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
iterator Iterator
Definition: qbytearray.h:335
iterator begin()
Definition: qbytearray.h:493
unsigned short ushort
Definition: qglobal.h:995
uint qstrlen(const char *str)
Definition: qbytearray.h:79
bool operator>=(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:557
static uchar fromHex(const uchar c, const uchar c2)
QByteArray & setNum(short, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qbytearray.h:584
QT_ASCII_CAST_WARN bool operator<=(const QString &s2) const
Returns true if this byte array is lexically less than or equal to string str; otherwise returns fals...
Definition: qstring.h:989
int qsnprintf(char *str, size_t n, const char *fmt,...)
A portable snprintf() function, calls qvsnprintf.
Definition: qvsnprintf.cpp:128
Q_DECLARE_TYPEINFO(QByteArray, Q_MOVABLE_TYPE)
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
static Data shared_null
Definition: qbytearray.h:384
QByteArray & operator+=(char c)
Appends the character ch onto the end of this byte array and returns a reference to this byte array...
Definition: qbytearray.h:505
quint16 index
bool operator==(char c) const
Definition: qbytearray.h:475
bool operator<(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:539
int capacity() const
Returns the maximum number of bytes that can be stored in the byte array without forcing a reallocati...
Definition: qbytearray.h:446
char * qstrdup(const char *src)
Returns a duplicate string.
Definition: qbytearray.cpp:114
static qreal toDouble(const QChar *&str)
QDataStream & operator>>(QDataStream &in, QByteArray &ba)
Reads a byte array into ba from the stream in and returns a reference to the stream.
QByteArray & replace(int index, int len, const char *s)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Initialization
Definition: qnamespace.h:1729
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
quint64 qulonglong
Definition: qglobal.h:952
timeval & operator+=(timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:120
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
void reserve(int size)
Attempts to allocate memory for at least size bytes.
Definition: qbytearray.h:449
static const KeyPair *const end
char * qstrncpy(char *dst, const char *src, uint len)
A safe strncpy() function.
Definition: qbytearray.cpp:175
QT_ASCII_CAST_WARN bool operator<(const QString &s2) const
Returns true if this byte array is lexically less than string str; otherwise returns false...
Definition: qstring.h:985
qint64 qlonglong
Definition: qglobal.h:951
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
#define QT_END_HEADER
Definition: qglobal.h:137
void detach()
Definition: qbytearray.h:435
iterator end()
Definition: qbytearray.h:499
static Data shared_empty
Definition: qbytearray.h:385
void push_front(char c)
Same as prepend(ch).
Definition: qbytearray.h:517
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
char & reference
Definition: qbytearray.h:346
friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes)
Uncompresses the first nbytes of data and returns a new byte array with the uncompressed data...
Definition: qbytearray.cpp:562