~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/tools/qbytearray.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qbytearray.h"
 
30
#include "qbytearraymatcher.h"
 
31
#include "qtools_p.h"
 
32
#include "qstring.h"
 
33
#include "qlist.h"
 
34
#include "qlocale.h"
 
35
#include "qlocale_p.h"
 
36
#include "qunicodetables_p.h"
 
37
#ifndef QT_NO_DATASTREAM
 
38
#include <qdatastream.h>
 
39
#endif
 
40
 
 
41
#ifndef QT_NO_COMPRESS
 
42
#include <zlib.h>
 
43
#endif
 
44
#include <ctype.h>
 
45
#include <limits.h>
 
46
#include <string.h>
 
47
 
 
48
int qAllocMore(int alloc, int extra)
 
49
{
 
50
    const int page = 1<<12;
 
51
    int nalloc;
 
52
    alloc += extra;
 
53
    if (alloc < 1<<6) {
 
54
        nalloc = (1<<3) + ((alloc >>3) << 3);
 
55
    } else if (alloc < page) {
 
56
        nalloc = 1<<3;
 
57
        while (nalloc < alloc)
 
58
            nalloc *= 2;
 
59
    } else {
 
60
        nalloc = ((alloc + page) / page) * page;
 
61
    }
 
62
    return nalloc - extra;
 
63
}
 
64
 
 
65
/*****************************************************************************
 
66
  Safe and portable C string functions; extensions to standard string.h
 
67
 *****************************************************************************/
 
68
 
 
69
/*! \relates QByteArray
 
70
 
 
71
    Returns a duplicate string.
 
72
 
 
73
    Allocates space for a copy of \a src, copies it, and returns a
 
74
    pointer to the copy. If \a src is 0, it immediately returns 0.
 
75
 
 
76
    Ownership is passed to the caller, so the returned string must be
 
77
    deleted using \c delete[].
 
78
*/
 
79
 
 
80
char *qstrdup(const char *src)
 
81
{
 
82
    if (!src)
 
83
        return 0;
 
84
    char *dst = new char[strlen(src) + 1];
 
85
    return qstrcpy(dst, src);
 
86
}
 
87
 
 
88
/*! \relates QByteArray
 
89
 
 
90
    Copies all the characters up to and including the '\\0' from \a
 
91
    src into \a dst and returns a pointer to \a dst. If \a src is 0,
 
92
    it immediately returns 0.
 
93
 
 
94
    This function assumes that \a dst is large enough to hold the
 
95
    contents of \a src.
 
96
 
 
97
    \sa qstrncpy()
 
98
*/
 
99
 
 
100
char *qstrcpy(char *dst, const char *src)
 
101
{
 
102
    if (!src)
 
103
        return 0;
 
104
#if defined(_MSC_VER) && _MSC_VER >= 1400
 
105
    int len = qstrlen(src);
 
106
        // This is actually not secure!!! It will be fixed
 
107
        // properly in a later release!
 
108
    if (len >= 0 && strcpy_s(dst, len+1, src) == 0)
 
109
            return dst;
 
110
    return 0;
 
111
#else
 
112
    return strcpy(dst, src);
 
113
#endif
 
114
}
 
115
 
 
116
/*! \relates QByteArray
 
117
 
 
118
    A safe strncpy() function.
 
119
 
 
120
    Copies at most \a len bytes from \a src (stopping at \a len or the
 
121
    terminating '\\0' whichever comes first) into \a dst and returns a
 
122
    pointer to \a dst. Guarantees that \a dst is '\\0'-terminated. If
 
123
    \a src or \a dst is 0, returns 0 immediately.
 
124
 
 
125
    This function assumes that \a dst is at least \a len characters
 
126
    long.
 
127
 
 
128
    \sa qstrcpy()
 
129
*/
 
130
 
 
131
char *qstrncpy(char *dst, const char *src, uint len)
 
132
{
 
133
    if (!src || !dst)
 
134
        return 0;
 
135
#if defined(_MSC_VER) && _MSC_VER >= 1400
 
136
        strncpy_s(dst, len, src, len-1);
 
137
#else
 
138
    strncpy(dst, src, len);
 
139
#endif
 
140
    if (len > 0)
 
141
        dst[len-1] = '\0';
 
142
    return dst;
 
143
}
 
144
 
 
145
/*! \fn uint qstrlen(const char *str);
 
146
 
 
147
    \relates QByteArray
 
148
 
 
149
    A safe strlen() function.
 
150
 
 
151
    Returns the number of characters that precede the terminating '\\0',
 
152
    or 0 if \a str is 0.
 
153
*/
 
154
 
 
155
/*! \relates QByteArray
 
156
 
 
157
    A safe strcmp() function.
 
158
 
 
159
    Compares \a str1 and \a str2. Returns a negative value if \a str1
 
160
    is less than \a str2, 0 if \a str1 is equal to \a str2 or a
 
161
    positive value if \a str1 is greater than \a str2.
 
162
 
 
163
    Special case 1: Returns 0 if \a str1 and \a str2 are both 0.
 
164
 
 
165
    Special case 2: Returns a random non-zero value if \a str1 is 0
 
166
    or \a str2 is 0 (but not both).
 
167
 
 
168
    \sa qstrncmp(), qstricmp(), qstrnicmp(),
 
169
        {Note on 8-bit character comparisons}
 
170
*/
 
171
int qstrcmp(const char *str1, const char *str2)
 
172
{
 
173
    return (str1 && str2) ? strcmp(str1, str2)
 
174
        : (str1 ? 1 : (str2 ? -1 : 0));
 
175
}
 
176
 
 
177
/*! \fn int qstrncmp(const char *str1, const char *str2, uint len);
 
178
 
 
179
    \relates QByteArray
 
180
 
 
181
    A safe strncmp() function.
 
182
 
 
183
    Compares at most \a len bytes of \a str1 and \a str2.
 
184
 
 
185
    Returns a negative value if \a str1 is less than \a str2, 0 if \a
 
186
    str1 is equal to \a str2 or a positive value if \a str1 is greater
 
187
    than \a str2.
 
188
 
 
189
    Special case 1: Returns 0 if \a str1 and \a str2 are both 0.
 
190
 
 
191
    Special case 2: Returns a random non-zero value if \a str1 is 0
 
192
    or \a str2 is 0 (but not both).
 
193
 
 
194
    \sa qstrcmp(), qstricmp(), qstrnicmp(),
 
195
        {Note on 8-bit character comparisons}
 
196
*/
 
197
 
 
198
/*! \relates QByteArray
 
199
 
 
200
    A safe stricmp() function.
 
201
 
 
202
    Compares \a str1 and \a str2 ignoring the case of the
 
203
    characters. The encoding of the strings is assumed to be Latin-1.
 
204
 
 
205
    Returns a negative value if \a str1 is less than \a str2, 0 if \a
 
206
    str1 is equal to \a str2 or a positive value if \a str1 is greater
 
207
    than \a str2.
 
208
 
 
209
    Special case 1: Returns 0 if \a str1 and \a str2 are both 0.
 
210
 
 
211
    Special case 2: Returns a random non-zero value if \a str1 is 0
 
212
    or \a str2 is 0 (but not both).
 
213
 
 
214
    \sa qstrcmp(), qstrncmp(), qstrnicmp(),
 
215
        {Note on 8-bit character comparisons}
 
216
*/
 
217
 
 
218
int qstricmp(const char *str1, const char *str2)
 
219
{
 
220
    register const uchar *s1 = reinterpret_cast<const uchar *>(str1);
 
221
    register const uchar *s2 = reinterpret_cast<const uchar *>(str2);
 
222
    int res;
 
223
    uchar c;
 
224
    if (!s1 || !s2)
 
225
        return s1 ? 1 : (s2 ? -1 : 0);
 
226
    for (; !(res = (c = QUnicodeTables::lower(*s1)) - QUnicodeTables::lower(*s2)); s1++, s2++)
 
227
        if (!c)                                // strings are equal
 
228
            break;
 
229
    return res;
 
230
}
 
231
 
 
232
/*! \relates QByteArray
 
233
 
 
234
    A safe strnicmp() function.
 
235
 
 
236
    Compares at most \a len bytes of \a str1 and \a str2 ignoring the
 
237
    case of the characters. The encoding of the strings is assumed to
 
238
    be Latin-1.
 
239
 
 
240
    Returns a negative value if \a str1 is less than \a str2, 0 if \a str1
 
241
    is equal to \a str2 or a positive value if \a str1 is greater than \a
 
242
    str2.
 
243
 
 
244
    Special case 1: Returns 0 if \a str1 and \a str2 are both 0.
 
245
 
 
246
    Special case 2: Returns a random non-zero value if \a str1 is 0
 
247
    or \a str2 is 0 (but not both).
 
248
 
 
249
    \sa qstrcmp(), qstrncmp(), qstricmp(),
 
250
        {Note on 8-bit character comparisons}
 
251
*/
 
252
 
 
253
int qstrnicmp(const char *str1, const char *str2, uint len)
 
254
{
 
255
    register const uchar *s1 = reinterpret_cast<const uchar *>(str1);
 
256
    register const uchar *s2 = reinterpret_cast<const uchar *>(str2);
 
257
    int res;
 
258
    uchar c;
 
259
    if (!s1 || !s2)
 
260
        return s1 ? 1 : (s2 ? -1 : 0);
 
261
    for (; len--; s1++, s2++) {
 
262
        if ((res = (c = QUnicodeTables::lower(*s1)) - QUnicodeTables::lower(*s2)))
 
263
            return res;
 
264
        if (!c)                                // strings are equal
 
265
            break;
 
266
    }
 
267
    return 0;
 
268
}
 
269
 
 
270
// the CRC table below is created by the following piece of code
 
271
#if 0
 
272
static void createCRC16Table()                        // build CRC16 lookup table
 
273
{
 
274
    register unsigned int i;
 
275
    register unsigned int j;
 
276
    unsigned short crc_tbl[16];
 
277
    unsigned int v0, v1, v2, v3;
 
278
    for (i = 0; i < 16; i++) {
 
279
        v0 = i & 1;
 
280
        v1 = (i >> 1) & 1;
 
281
        v2 = (i >> 2) & 1;
 
282
        v3 = (i >> 3) & 1;
 
283
        j = 0;
 
284
#undef SET_BIT
 
285
#define SET_BIT(x, b, v) (x) |= (v) << (b)
 
286
        SET_BIT(j,  0, v0);
 
287
        SET_BIT(j,  7, v0);
 
288
        SET_BIT(j, 12, v0);
 
289
        SET_BIT(j,  1, v1);
 
290
        SET_BIT(j,  8, v1);
 
291
        SET_BIT(j, 13, v1);
 
292
        SET_BIT(j,  2, v2);
 
293
        SET_BIT(j,  9, v2);
 
294
        SET_BIT(j, 14, v2);
 
295
        SET_BIT(j,  3, v3);
 
296
        SET_BIT(j, 10, v3);
 
297
        SET_BIT(j, 15, v3);
 
298
        crc_tbl[i] = j;
 
299
    }
 
300
    printf("static const quint16 crc_tbl[16] = {\n");
 
301
    for (int i = 0; i < 16; i +=4)
 
302
        printf("    0x%04x, 0x%04x, 0x%04x, 0x%04x,\n", crc_tbl[i], crc_tbl[i+1], crc_tbl[i+2], crc_tbl[i+3]);
 
303
    printf("};\n");
 
304
}
 
305
#endif
 
306
 
 
307
static const quint16 crc_tbl[16] = {
 
308
    0x0000, 0x1081, 0x2102, 0x3183,
 
309
    0x4204, 0x5285, 0x6306, 0x7387,
 
310
    0x8408, 0x9489, 0xa50a, 0xb58b,
 
311
    0xc60c, 0xd68d, 0xe70e, 0xf78f
 
312
};
 
313
 
 
314
/*! \relates QByteArray
 
315
 
 
316
    Returns the CRC-16 checksum of the first \a len bytes of \a data.
 
317
 
 
318
    The checksum is independent of the byte order (endianness).
 
319
*/
 
320
 
 
321
quint16 qChecksum(const char *data, uint len)
 
322
{
 
323
    register quint16 crc = 0xffff;
 
324
    uchar c;
 
325
    const uchar *p = reinterpret_cast<const uchar *>(data);
 
326
    while (len--) {
 
327
        c = *p++;
 
328
        crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
 
329
        c >>= 4;
 
330
        crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
 
331
    }
 
332
    return ~crc & 0xffff;
 
333
}
 
334
 
 
335
/*! \fn QByteArray qCompress(const QByteArray& data, int compressionLevel)
 
336
 
 
337
    \relates QByteArray
 
338
 
 
339
    Compresses the \a data byte array and returns the compressed data
 
340
    in a new byte array.
 
341
 
 
342
    The \a compressionLevel parameter specifies how much compression
 
343
    should be used. Valid values are between 0 and 9, with 9
 
344
    corresponding to the greatest compression (i.e. smaller compressed
 
345
    data) at the cost of using a slower algorithm. Smaller values (8,
 
346
    7, ..., 1) provide successively less compression at slightly
 
347
    faster speeds. The value 0 corresponds to no compression at all.
 
348
    The default value is -1, which specifies zlib's default
 
349
    compression.
 
350
 
 
351
    \sa qUncompress()
 
352
*/
 
353
 
 
354
/*! \relates QByteArray
 
355
 
 
356
    \overload
 
357
 
 
358
    Compresses the first \a nbytes of \a data and returns the
 
359
    compressed data in a new byte array.
 
360
*/
 
361
 
 
362
#ifndef QT_NO_COMPRESS
 
363
QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel)
 
364
{
 
365
    if (nbytes == 0) {
 
366
        return QByteArray(4, '\0');
 
367
    }
 
368
    if (!data) {
 
369
        qWarning("qCompress: Data is null");
 
370
        return QByteArray();
 
371
    }
 
372
    if (compressionLevel < -1 || compressionLevel > 9)
 
373
        compressionLevel = -1;
 
374
 
 
375
    ulong len = nbytes + nbytes / 100 + 13;
 
376
    QByteArray bazip;
 
377
    int res;
 
378
    do {
 
379
        bazip.resize(len + 4);
 
380
        res = ::compress2((uchar*)bazip.data()+4, &len, (uchar*)data, nbytes, compressionLevel);
 
381
 
 
382
        switch (res) {
 
383
        case Z_OK:
 
384
            bazip.resize(len + 4);
 
385
            bazip[0] = (nbytes & 0xff000000) >> 24;
 
386
            bazip[1] = (nbytes & 0x00ff0000) >> 16;
 
387
            bazip[2] = (nbytes & 0x0000ff00) >> 8;
 
388
            bazip[3] = (nbytes & 0x000000ff);
 
389
            break;
 
390
        case Z_MEM_ERROR:
 
391
            qWarning("qCompress: Z_MEM_ERROR: Not enough memory");
 
392
            bazip.resize(0);
 
393
            break;
 
394
        case Z_BUF_ERROR:
 
395
            len *= 2;
 
396
            break;
 
397
        }
 
398
    } while (res == Z_BUF_ERROR);
 
399
 
 
400
    return bazip;
 
401
}
 
402
#endif
 
403
 
 
404
/*! \fn QByteArray qUncompress(const QByteArray& data)
 
405
 
 
406
    \relates QByteArray
 
407
 
 
408
    Uncompresses the \a data byte array and returns a new byte array
 
409
    with the uncompressed data.
 
410
 
 
411
    Returns an empty QByteArray if the input data was corrupt.
 
412
 
 
413
    This function will uncompress data compressed with qCompress()
 
414
    from this and any earlier Qt version, back to Qt 3.1 when this
 
415
    feature was added.
 
416
 
 
417
    \sa qCompress()
 
418
*/
 
419
 
 
420
/*! \relates QByteArray
 
421
 
 
422
    \overload
 
423
 
 
424
    Uncompresses the first \a nbytes of \a data and returns a new byte
 
425
    array with the uncompressed data.
 
426
*/
 
427
 
 
428
#ifndef QT_NO_COMPRESS
 
429
QByteArray qUncompress(const uchar* data, int nbytes)
 
430
{
 
431
    if (!data) {
 
432
        qWarning("qUncompress: Data is null");
 
433
        return QByteArray();
 
434
    }
 
435
    if (nbytes <= 4) {
 
436
        if (nbytes < 4 || (data[0]!=0 || data[1]!=0 || data[2]!=0 || data[3]!=0))
 
437
            qWarning("qUncompress: Input data is corrupted");
 
438
        return QByteArray();
 
439
    }
 
440
    ulong expectedSize = (data[0] << 24) | (data[1] << 16) |
 
441
                       (data[2] <<  8) | (data[3]      );
 
442
    ulong len = qMax(expectedSize, 1ul);
 
443
    QByteArray baunzip;
 
444
    int res;
 
445
    do {
 
446
        baunzip.resize(len);
 
447
        res = ::uncompress((uchar*)baunzip.data(), &len,
 
448
                            (uchar*)data+4, nbytes-4);
 
449
 
 
450
        switch (res) {
 
451
        case Z_OK:
 
452
            if ((int)len != baunzip.size())
 
453
                baunzip.resize(len);
 
454
            break;
 
455
        case Z_MEM_ERROR:
 
456
            qWarning("qUncompress: Z_MEM_ERROR: Not enough memory");
 
457
            break;
 
458
        case Z_BUF_ERROR:
 
459
            len *= 2;
 
460
            break;
 
461
        case Z_DATA_ERROR:
 
462
            qWarning("qUncompress: Z_DATA_ERROR: Input data is corrupted");
 
463
            break;
 
464
        }
 
465
    } while (res == Z_BUF_ERROR);
 
466
 
 
467
    if (res != Z_OK)
 
468
        baunzip = QByteArray();
 
469
 
 
470
    return baunzip;
 
471
}
 
472
#endif
 
473
 
 
474
static inline bool qIsUpper(char c)
 
475
{
 
476
    return c >= 'A' && c <= 'Z';
 
477
}
 
478
 
 
479
static inline char qToLower(char c)
 
480
{
 
481
    if (c >= 'A' && c <= 'Z')
 
482
        return c - 'A' + 'a';
 
483
    else
 
484
        return c;
 
485
}
 
486
 
 
487
Q_CORE_EXPORT QByteArray::Data QByteArray::shared_null = {Q_ATOMIC_INIT(1), 0, 0, shared_null.array, {0} };
 
488
QByteArray::Data QByteArray::shared_empty = { Q_ATOMIC_INIT(1), 0, 0, shared_empty.array, {0} };
 
489
 
 
490
/*!
 
491
    \class QByteArray
 
492
    \brief The QByteArray class provides an array of bytes.
 
493
 
 
494
    \ingroup tools
 
495
    \ingroup shared
 
496
    \ingroup text
 
497
    \mainclass
 
498
    \reentrant
 
499
 
 
500
    QByteArray can be used to store both raw bytes (including '\\0's)
 
501
    and traditional 8-bit '\\0'-terminated strings. Using QByteArray
 
502
    is much more convenient than using \c{const char *}. Behind the
 
503
    scenes, it always ensures that the data is followed by a '\\0'
 
504
    terminator, and uses \l{implicit sharing} (copy-on-write) to
 
505
    reduce memory usage and avoid needless copying of data.
 
506
 
 
507
    In addition to QByteArray, Qt also provides the QString class to
 
508
    store string data. For most purposes, QString is the class you
 
509
    want to use. It stores 16-bit Unicode characters, making it easy
 
510
    to store non-ASCII/non-Latin-1 characters in your application.
 
511
    Furthermore, QString is used throughout in the Qt API. The two
 
512
    main cases where QByteArray is appropriate are when you need to
 
513
    store raw binary data, and when memory conservation is critical
 
514
    (e.g. with Qt/Embedded).
 
515
 
 
516
    One way to initialize a QByteArray is simply to pass a \c{const
 
517
    char *} to its constructor. For example, the following code
 
518
    creates a byte array of size 5 containing the data "Hello":
 
519
 
 
520
    \code
 
521
        QByteArray ba("Hello");
 
522
    \endcode
 
523
 
 
524
    Although the size() is 5, the byte array also maintains an extra
 
525
    '\\0' character at the end so that if a function is used that
 
526
    asks for a pointer to the underlying data (e.g. a call to
 
527
    data()), the data pointed to is guaranteed to be
 
528
    '\\0'-terminated.
 
529
 
 
530
    QByteArray makes a deep copy of the \c{const char *} data, so you
 
531
    can modify it later without experiencing side effects. (If for
 
532
    performance reasons you don't want to take a deep copy of the
 
533
    character data, use QByteArray::fromRawData() instead.)
 
534
 
 
535
    Another approach is to set the size of the array using resize()
 
536
    and to initialize the data byte per byte. QByteArray uses 0-based
 
537
    indexes, just like C++ arrays. To access the byte at a particular
 
538
    index position, you can use operator[](). On non-const byte
 
539
    arrays, operator[]() returns a reference to a byte that can be
 
540
    used on the left side of an assignment. For example:
 
541
 
 
542
    \code
 
543
        QByteArray ba;
 
544
        ba.resize(5);
 
545
        ba[0] = 0x3c;
 
546
        ba[1] = 0xb8;
 
547
        ba[2] = 0x64;
 
548
        ba[3] = 0x18;
 
549
        ba[4] = 0xca;
 
550
    \endcode
 
551
 
 
552
    For read-only access, an alternative syntax is to use at():
 
553
 
 
554
    \code
 
555
        for (int i = 0; i < ba.size(); ++i) {
 
556
            if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
 
557
                cout << "Found character in range [a-f]" << endl;
 
558
        }
 
559
    \endcode
 
560
 
 
561
    at() can be faster than operator[](), because it never causes a
 
562
    \l{deep copy} to occur.
 
563
 
 
564
    To extract many bytes at a time, use left(), right(), or mid().
 
565
 
 
566
    A QByteArray can embed '\\0' bytes. The size() function always
 
567
    returns the size of the whole array, including embedded '\\0'
 
568
    bytes. If you want to obtain the length of the data up to and
 
569
    excluding the first '\\0' character, call qstrlen() on the byte
 
570
    array.
 
571
 
 
572
    After a call to resize(), newly allocated bytes have undefined
 
573
    values. To set all the bytes to a particular value, call fill().
 
574
 
 
575
    To obtain a pointer to the actual character data, call data() or
 
576
    constData(). These functions return a pointer to the beginning of
 
577
    the data. The pointer is guaranteed to remain valid until a
 
578
    non-const function is called on the QByteArray. It is also
 
579
    guaranteed that the data ends with a '\\0' byte. This '\\0' byte
 
580
    is automatically provided by QByteArray and is not counted in
 
581
    size().
 
582
 
 
583
    QByteArray provides the following basic functions for modifying
 
584
    the byte data: append(), prepend(), insert(), replace(), and
 
585
    remove(). For example:
 
586
 
 
587
    \code
 
588
        QByteArray x("and");
 
589
        x.prepend("rock ");         // x == "rock and"
 
590
        x.append(" roll");          // x == "rock and roll"
 
591
        x.replace(5, 3, "&");       // x == "rock & roll"
 
592
    \endcode
 
593
 
 
594
    The replace() and remove() functions' first two arguments are the
 
595
    position from which to start erasing and the number of bytes that
 
596
    should be erased.
 
597
 
 
598
    If you are building a QByteArray gradually and know in advance
 
599
    approximately how many bytes the QByteArray will contain,
 
600
    you can call reserve(), asking QByteArray to preallocate a
 
601
    certain amount of memory. You can also call capacity() to find
 
602
    out how much memory QByteArray actually allocated.
 
603
 
 
604
    A frequent requirement is to remove whitespace characters from a
 
605
    byte array ('\\n', '\\t', ' ', etc.). If you want to remove
 
606
    whitespace from both ends of a QByteArray, use trimmed(). If you
 
607
    want to remove whitespace from both ends and replace multiple
 
608
    consecutive whitespaces with a single space character within the
 
609
    byte array, use simplified().
 
610
 
 
611
    If you want to find all occurrences of a particular character or
 
612
    substring in a QByteArray, use indexOf() or lastIndexOf(). The
 
613
    former searches forward starting from a given index position, the
 
614
    latter searches backward. Both return the index position of the
 
615
    character or substring if they find it; otherwise, they return -1.
 
616
    For example, here's a typical loop that finds all occurrences of a
 
617
    particular substring:
 
618
 
 
619
    \code
 
620
        QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
 
621
        int j = 0;
 
622
        while ((j = ba.indexOf("<b>", j)) != -1) {
 
623
            cout << "Found <b> tag at index position " << j << endl;
 
624
            ++j;
 
625
        }
 
626
    \endcode
 
627
 
 
628
    If you simply want to check whether a QByteArray contains a
 
629
    particular character or substring, use contains(). If you want to
 
630
    find out how many times a particular character or substring
 
631
    occurs in the byte array, use count(). If you want to replace all
 
632
    occurrences of a particular value with another, use one of the
 
633
    two-parameter replace() overloads.
 
634
 
 
635
    QByteArrays can be compared using overloaded operators such as
 
636
    operator<(), operator<=(), operator==(), operator>=(), and so on.
 
637
    The comparison is based exclusively on the numeric values
 
638
    of the characters and is very fast, but is not what a human would
 
639
    expect. QString::localeAwareCompare() is a better choice for
 
640
    sorting user-interface strings.
 
641
 
 
642
    For historical reasons, QByteArray distinguishes between a null
 
643
    byte array and an empty byte array. A \e null byte array is a
 
644
    byte array that is initialized using QByteArray's default
 
645
    constructor or by passing (const char *)0 to the constructor. An
 
646
    \e empty byte array is any byte array with size 0. A null byte
 
647
    array is always empty, but an empty byte array isn't necessarily
 
648
    null:
 
649
 
 
650
    \code
 
651
        QByteArray().isNull();          // returns true
 
652
        QByteArray().isEmpty();         // returns true
 
653
 
 
654
        QByteArray("").isNull();        // returns false
 
655
        QByteArray("").isEmpty();       // returns true
 
656
 
 
657
        QByteArray("abc").isNull();     // returns false
 
658
        QByteArray("abc").isEmpty();    // returns false
 
659
    \endcode
 
660
 
 
661
    All functions except isNull() treat null byte arrays the same as
 
662
    empty byte arrays. For example, data() returns a pointer to a
 
663
    '\\0' character for a null byte array (\e not a null pointer),
 
664
    and QByteArray() compares equal to QByteArray(""). We recommend
 
665
    that you always use isEmpty() and avoid isNull().
 
666
 
 
667
    \section1 Note on 8-bit Character Comparisons
 
668
 
 
669
    In QByteArray, the notion of uppercase and lowercase and of which
 
670
    character is greater than or less than another character is
 
671
    locale dependent. This affects functions that support a case
 
672
    insensitive option or that compare or lowercase or uppercase
 
673
    their arguments. Case insensitive operations and comparisons will
 
674
    be accurate if both strings contain only ASCII characters. (If \c
 
675
    $LC_CTYPE is set, most Unix systems do "the right thing".)
 
676
    Functions that this affects include contains(), indexOf(),
 
677
    lastIndexOf(), operator<(), operator<=(), operator>(),
 
678
    operator>=(), toLower() and toUpper().
 
679
 
 
680
    This issue does not apply to QStrings since they represent
 
681
    characters using Unicode.
 
682
 
 
683
    \sa QString, QBitArray
 
684
*/
 
685
 
 
686
/*! \fn QByteArray::iterator QByteArray::begin()
 
687
 
 
688
    \internal
 
689
*/
 
690
 
 
691
/*! \fn QByteArray::const_iterator QByteArray::begin() const
 
692
 
 
693
    \internal
 
694
*/
 
695
 
 
696
/*! \fn QByteArray::const_iterator QByteArray::constBegin() const
 
697
 
 
698
    \internal
 
699
*/
 
700
 
 
701
/*! \fn QByteArray::iterator QByteArray::end()
 
702
 
 
703
    \internal
 
704
*/
 
705
 
 
706
/*! \fn QByteArray::const_iterator QByteArray::end() const
 
707
 
 
708
    \internal
 
709
*/
 
710
 
 
711
/*! \fn QByteArray::const_iterator QByteArray::constEnd() const
 
712
 
 
713
    \internal
 
714
*/
 
715
 
 
716
/*! \fn void QByteArray::push_back(const QByteArray &other)
 
717
 
 
718
    This function is provided for STL compatibility. It is equivalent
 
719
    to append(\a other).
 
720
*/
 
721
 
 
722
/*! \fn void QByteArray::push_back(const char *str)
 
723
 
 
724
    \overload
 
725
 
 
726
    Same as append(\a str).
 
727
*/
 
728
 
 
729
/*! \fn void QByteArray::push_back(char ch)
 
730
 
 
731
    \overload
 
732
 
 
733
    Same as append(\a ch).
 
734
*/
 
735
 
 
736
/*! \fn void QByteArray::push_front(const QByteArray &other)
 
737
 
 
738
    This function is provided for STL compatibility. It is equivalent
 
739
    to prepend(\a other).
 
740
*/
 
741
 
 
742
/*! \fn void QByteArray::push_front(const char *str)
 
743
 
 
744
    \overload
 
745
 
 
746
    Same as prepend(\a str).
 
747
*/
 
748
 
 
749
/*! \fn void QByteArray::push_front(char ch)
 
750
 
 
751
    \overload
 
752
 
 
753
    Same as prepend(\a ch).
 
754
*/
 
755
 
 
756
/*! \fn QByteArray::QByteArray(const QByteArray &other)
 
757
 
 
758
    Constructs a copy of \a other.
 
759
 
 
760
    This operation takes \l{constant time}, because QByteArray is
 
761
    \l{implicitly shared}. This makes returning a QByteArray from a
 
762
    function very fast. If a shared instance is modified, it will be
 
763
    copied (copy-on-write), and that takes \l{linear time}.
 
764
 
 
765
    \sa operator=()
 
766
*/
 
767
 
 
768
/*! \fn QByteArray::~QByteArray()
 
769
    Destroys the byte array.
 
770
*/
 
771
 
 
772
/*!
 
773
    Assigns \a other to this byte array and returns a reference to
 
774
    this byte array.
 
775
*/
 
776
QByteArray &QByteArray::operator=(const QByteArray & other)
 
777
{
 
778
    Data *x = other.d;
 
779
    x->ref.ref();
 
780
    x = qAtomicSetPtr(&d, x);
 
781
    if (!x->ref.deref())
 
782
        qFree(x);
 
783
    return *this;
 
784
}
 
785
 
 
786
 
 
787
/*!
 
788
    \overload
 
789
 
 
790
    Assigns \a str to this byte array.
 
791
*/
 
792
 
 
793
QByteArray &QByteArray::operator=(const char *str)
 
794
{
 
795
    Data *x;
 
796
    if (!str) {
 
797
        x = &shared_null;
 
798
    } else if (!*str) {
 
799
        x = &shared_empty;
 
800
    } else {
 
801
        int len = qstrlen(str);
 
802
        if (d->ref != 1 || len > d->alloc || (len < d->size && len < d->alloc >> 1))
 
803
            realloc(len);
 
804
        x = d;
 
805
        memcpy(x->data, str, len + 1); // include null terminator
 
806
        x->size = len;
 
807
    }
 
808
    x->ref.ref();
 
809
    x = qAtomicSetPtr(&d, x);
 
810
    if (!x->ref.deref())
 
811
         qFree(x);
 
812
    return *this;
 
813
}
 
814
 
 
815
/*! \fn int QByteArray::size() const
 
816
 
 
817
    Returns the number of bytes in this byte array.
 
818
 
 
819
    The last byte in the byte array is at position size() - 1. In
 
820
    addition, QByteArray ensures that the byte at position size() is
 
821
    always '\\0', so that you can use the return value of data() and
 
822
    constData() as arguments to functions that expect
 
823
    '\\0'-terminated strings.
 
824
 
 
825
    Example:
 
826
    \code
 
827
        QByteArray ba("Hello");
 
828
        int n = ba.size();          // n == 5
 
829
        ba.data()[0];               // returns 'H'
 
830
        ba.data()[4];               // returns 'o'
 
831
        ba.data()[5];               // returns '\0'
 
832
    \endcode
 
833
 
 
834
    \sa isEmpty(), resize()
 
835
*/
 
836
 
 
837
/*! \fn bool QByteArray::isEmpty() const
 
838
 
 
839
    Returns true if the byte array has size 0; otherwise returns false.
 
840
 
 
841
    Example:
 
842
    \code
 
843
        QByteArray().isEmpty();         // returns true
 
844
        QByteArray("").isEmpty();       // returns true
 
845
        QByteArray("abc").isEmpty();    // returns false
 
846
    \endcode
 
847
 
 
848
    \sa size()
 
849
*/
 
850
 
 
851
/*! \fn int QByteArray::capacity() const
 
852
 
 
853
    Returns the maximum number of bytes that can be stored in the
 
854
    byte array without forcing a reallocation.
 
855
 
 
856
    The sole purpose of this function is to provide a means of fine
 
857
    tuning QByteArray's memory usage. In general, you will rarely
 
858
    ever need to call this function. If you want to know how many
 
859
    bytes are in the byte array, call size().
 
860
 
 
861
    \sa reserve(), squeeze()
 
862
*/
 
863
 
 
864
/*! \fn void QByteArray::reserve(int size)
 
865
 
 
866
    Attempts to allocate memory for at least \a size bytes. If you
 
867
    know in advance how large the byte array will be, you can call
 
868
    this function, and if you call resize() often you are likely to
 
869
    get better performance. If \a size is an underestimate, the worst
 
870
    that will happen is that the QByteArray will be a bit slower.
 
871
 
 
872
    The sole purpose of this function is to provide a means of fine
 
873
    tuning QByteArray's memory usage. In general, you will rarely
 
874
    ever need to call this function. If you want to change the size
 
875
    of the byte array, call resize().
 
876
 
 
877
    \sa squeeze(), capacity()
 
878
*/
 
879
 
 
880
/*! \fn void QByteArray::squeeze()
 
881
 
 
882
    Releases any memory not required to store the array's data.
 
883
 
 
884
    The sole purpose of this function is to provide a means of fine
 
885
    tuning QByteArray's memory usage. In general, you will rarely
 
886
    ever need to call this function.
 
887
 
 
888
    \sa reserve(), capacity()
 
889
*/
 
890
 
 
891
/*! \fn QByteArray::operator const char *() const
 
892
 
 
893
    Returns a pointer to the data stored in the byte array. The
 
894
    pointer can be used to access the bytes that compose the array.
 
895
    The data is '\\0'-terminated. The pointer remains valid as long
 
896
    as the array isn't reallocated.
 
897
 
 
898
    This operator is mostly useful to pass a byte array to a function
 
899
    that accepts a \c{const char *}.
 
900
 
 
901
    Note: A QByteArray can store any byte values including '\\0's,
 
902
    but most functions that take \c{char *} arguments assume that the
 
903
    data ends at the first '\\0' they encounter.
 
904
 
 
905
    \sa constData()
 
906
*/
 
907
 
 
908
/*! \fn QByteArray::operator const void *() const
 
909
 
 
910
    Returns a void pointer to the data.
 
911
 
 
912
    This operator is mostly useful to pass a byte array to a function
 
913
    that accepts a void *.
 
914
 
 
915
    \sa constData()
 
916
*/
 
917
 
 
918
/*! \fn char *QByteArray::data()
 
919
 
 
920
    Returns a pointer to the data stored in the byte array. The
 
921
    pointer can be used to access and modify the bytes that compose
 
922
    the array. The data is '\\0'-terminated.
 
923
 
 
924
    Example:
 
925
    \code
 
926
        QByteArray ba("Hello world");
 
927
        char *data = ba.data();
 
928
        while (*data) {
 
929
            cout << "[" << *data << "]" << endl;
 
930
            ++data;
 
931
        }
 
932
    \endcode
 
933
 
 
934
    The pointer remains valid as long as the byte array isn't
 
935
    reallocated.
 
936
 
 
937
    This function is mostly useful to pass a byte array to a function
 
938
    that accepts a \c{const char *}.
 
939
 
 
940
    Note: A QByteArray can store any byte values including '\\0's,
 
941
    but most functions that take \c{char *} arguments assume that the
 
942
    data ends at the first '\\0' they encounter.
 
943
 
 
944
    \sa constData(), operator[]()
 
945
*/
 
946
 
 
947
/*! \fn const char *QByteArray::data() const
 
948
 
 
949
    \overload
 
950
*/
 
951
 
 
952
/*! \fn const char *QByteArray::constData() const
 
953
 
 
954
    Returns a pointer to the data stored in the byte array. The
 
955
    pointer can be used to access the bytes that compose the array.
 
956
    The data is '\\0'-terminated. The pointer remains valid as long
 
957
    as the byte array isn't reallocated.
 
958
 
 
959
    This function is mostly useful to pass a byte array to a function
 
960
    that accepts a \c{const char *}.
 
961
 
 
962
    Note: A QByteArray can store any byte values including '\\0's,
 
963
    but most functions that take \c{char *} arguments assume that the
 
964
    data ends at the first '\\0' they encounter.
 
965
 
 
966
    \sa data(), operator[]()
 
967
*/
 
968
 
 
969
/*! \fn void QByteArray::detach()
 
970
 
 
971
    \internal
 
972
*/
 
973
 
 
974
/*! \fn bool QByteArray::isDetached() const
 
975
 
 
976
    \internal
 
977
*/
 
978
 
 
979
/*! \fn char QByteArray::at(int i) const
 
980
 
 
981
    Returns the character at index position \a i in the byte array.
 
982
 
 
983
    \a i must be a valid index position in the byte array (i.e., 0 <=
 
984
    \a i < size()).
 
985
 
 
986
    \sa operator[]()
 
987
*/
 
988
 
 
989
/*! \fn QByteRef QByteArray::operator[](int i)
 
990
 
 
991
    Returns the byte at index position \a i as a modifiable reference.
 
992
 
 
993
    If an assignment is made beyond the end of the byte array, the
 
994
    array is extended with resize() before the assignment takes
 
995
    place.
 
996
 
 
997
    Example:
 
998
    \code
 
999
        QByteArray ba;
 
1000
        for (int i = 0; i < 10; ++i)
 
1001
            ba[i] = 'A' + i;
 
1002
        // ba == "ABCDEFGHIJ"
 
1003
    \endcode
 
1004
 
 
1005
    The return value is of type QByteRef, a helper class for
 
1006
    QByteArray. When you get an object of type QByteRef, you can use
 
1007
    it as if it were a char &. If you assign to it, the assignment
 
1008
    will apply to the character in the QByteArray from which you got
 
1009
    the reference.
 
1010
 
 
1011
    \sa at()
 
1012
*/
 
1013
 
 
1014
/*! \fn char QByteArray::operator[](int i) const
 
1015
 
 
1016
    \overload
 
1017
 
 
1018
    Same as at(\a i).
 
1019
*/
 
1020
 
 
1021
/*! \fn QByteRef QByteArray::operator[](uint i)
 
1022
 
 
1023
    \overload
 
1024
*/
 
1025
 
 
1026
/*! \fn char QByteArray::operator[](uint i) const
 
1027
 
 
1028
    \overload
 
1029
*/
 
1030
 
 
1031
/*! \fn QBool QByteArray::contains(const QByteArray &ba) const
 
1032
 
 
1033
    Returns true if the byte array contains an occurrence of the byte
 
1034
    array \a ba; otherwise returns false.
 
1035
 
 
1036
    \sa indexOf(), count()
 
1037
*/
 
1038
 
 
1039
/*! \fn QBool QByteArray::contains(const char *str) const
 
1040
 
 
1041
    \overload
 
1042
 
 
1043
    Returns true if the byte array contains the string \a str;
 
1044
    otherwise returns false.
 
1045
*/
 
1046
 
 
1047
/*! \fn QBool QByteArray::contains(char ch) const
 
1048
 
 
1049
    \overload
 
1050
 
 
1051
    Returns true if the byte array contains the character \a ch;
 
1052
    otherwise returns false.
 
1053
*/
 
1054
 
 
1055
/*!
 
1056
 
 
1057
    Truncates the byte array at index position \a pos.
 
1058
 
 
1059
    If \a pos is beyond the end of the array, nothing happens.
 
1060
 
 
1061
    Example:
 
1062
    \code
 
1063
        QByteArray ba("Stockholm");
 
1064
        ba.truncate(5);             // ba == "Stock"
 
1065
    \endcode
 
1066
 
 
1067
    \sa chop(), resize(), left()
 
1068
*/
 
1069
void QByteArray::truncate(int pos)
 
1070
{
 
1071
    if (pos < d->size)
 
1072
        resize(pos);
 
1073
}
 
1074
 
 
1075
/*!
 
1076
 
 
1077
    Removes \a n bytes from the end of the byte array.
 
1078
 
 
1079
    If \a n is greater than size(), the result is an empty byte
 
1080
    array.
 
1081
 
 
1082
    Example:
 
1083
    \code
 
1084
        QByteArray ba("STARTTLS\r\n");
 
1085
        ba.chop(2);                 // ba == "STARTTLS"
 
1086
    \endcode
 
1087
 
 
1088
    \sa truncate(), resize(), left()
 
1089
*/
 
1090
 
 
1091
void QByteArray::chop(int n)
 
1092
{
 
1093
    if (n > 0)
 
1094
        resize(d->size - n);
 
1095
}
 
1096
 
 
1097
 
 
1098
/*! \fn QByteArray &QByteArray::operator+=(const QByteArray &ba)
 
1099
 
 
1100
    Appends the byte array \a ba onto the end of this byte array and
 
1101
    returns a reference to this byte array.
 
1102
 
 
1103
    Example:
 
1104
    \code
 
1105
        QByteArray x("free");
 
1106
        QByteArray y("dom");
 
1107
        x += y;
 
1108
        // x == "freedom"
 
1109
    \endcode
 
1110
 
 
1111
    This operation is typically very fast (\l{constant time}),
 
1112
    because QByteArray preallocates extra space at the end of the
 
1113
    character data so it can grow without reallocating the entire
 
1114
    data each time.
 
1115
 
 
1116
    \sa append(), prepend()
 
1117
*/
 
1118
 
 
1119
/*! \fn QByteArray &QByteArray::operator+=(const QString &str)
 
1120
 
 
1121
    \overload
 
1122
 
 
1123
    Appends the string \a str onto the end of this byte array and
 
1124
    returns a reference to this byte array. The Unicode data is
 
1125
    converted into 8-bit characters using QString::toAscii().
 
1126
 
 
1127
    If the QString contains non-ASCII Unicode characters, using this
 
1128
    operator can lead to loss of information. You can disable this
 
1129
    operator by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
1130
    applications. You then need to call QString::toAscii() (or
 
1131
    QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
 
1132
    explicitly if you want to convert the data to \c{const char *}.
 
1133
*/
 
1134
 
 
1135
/*! \fn QByteArray &QByteArray::operator+=(const char *str)
 
1136
 
 
1137
    \overload
 
1138
 
 
1139
    Appends the string \a str onto the end of this byte array and
 
1140
    returns a reference to this byte array.
 
1141
*/
 
1142
 
 
1143
/*! \fn QByteArray &QByteArray::operator+=(char ch)
 
1144
 
 
1145
    \overload
 
1146
 
 
1147
    Appends the character \a ch onto the end of this byte array and
 
1148
    returns a reference to this byte array.
 
1149
*/
 
1150
 
 
1151
/*! \fn int QByteArray::length() const
 
1152
 
 
1153
    Same as size().
 
1154
*/
 
1155
 
 
1156
/*! \fn bool QByteArray::isNull() const
 
1157
 
 
1158
    Returns true if this byte array is null; otherwise returns false.
 
1159
 
 
1160
    Example:
 
1161
    \code
 
1162
        QByteArray().isNull();          // returns true
 
1163
        QByteArray("").isNull();        // returns false
 
1164
        QByteArray("abc").isNull();     // returns false
 
1165
    \endcode
 
1166
 
 
1167
    Qt makes a distinction between null byte arrays and empty byte
 
1168
    arrays for historical reasons. For most applications, what
 
1169
    matters is whether or not a byte array contains any data,
 
1170
    and this can be determined using isEmpty().
 
1171
 
 
1172
    \sa isEmpty()
 
1173
*/
 
1174
 
 
1175
/*! \fn QByteArray::QByteArray()
 
1176
 
 
1177
    Constructs an empty byte array.
 
1178
 
 
1179
    \sa isEmpty()
 
1180
*/
 
1181
 
 
1182
/*! \fn QByteArray::QByteArray(const char *str)
 
1183
 
 
1184
    Constructs a byte array initialized with the string \a str.
 
1185
 
 
1186
    QByteArray makes a deep copy of the string data.
 
1187
*/
 
1188
 
 
1189
QByteArray::QByteArray(const char *str)
 
1190
{
 
1191
    if (!str) {
 
1192
        d = &shared_null;
 
1193
    } else if (!*str) {
 
1194
        d = &shared_empty;
 
1195
    } else {
 
1196
        int len = qstrlen(str);
 
1197
        d = static_cast<Data *>(qMalloc(sizeof(Data)+len));
 
1198
        if (!d) {
 
1199
            d = &shared_null;
 
1200
        } else {
 
1201
            d->ref.init(0);
 
1202
            d->alloc = d->size = len;
 
1203
            d->data = d->array;
 
1204
            memcpy(d->array, str, len+1); // include null terminator
 
1205
        }
 
1206
    }
 
1207
    d->ref.ref();
 
1208
}
 
1209
 
 
1210
/*!
 
1211
    Constructs a byte array containing the first \a size bytes of
 
1212
    array \a data.
 
1213
 
 
1214
    If \a data is 0, a null byte array is constructed.
 
1215
 
 
1216
    QByteArray makes a deep copy of the string data.
 
1217
 
 
1218
    \sa fromRawData()
 
1219
*/
 
1220
 
 
1221
QByteArray::QByteArray(const char *data, int size)
 
1222
{
 
1223
    if (!data) {
 
1224
        d = &shared_null;
 
1225
    } else if (size <= 0) {
 
1226
        d = &shared_empty;
 
1227
    } else {
 
1228
        d = static_cast<Data *>(qMalloc(sizeof(Data) + size));
 
1229
        if (!d) {
 
1230
            d = &shared_null;
 
1231
        } else {
 
1232
            d->ref.init(0);
 
1233
            d->alloc = d->size = size;
 
1234
            d->data = d->array;
 
1235
            memcpy(d->array, data, size);
 
1236
            d->array[size] = '\0';
 
1237
        }
 
1238
    }
 
1239
    d->ref.ref();
 
1240
}
 
1241
 
 
1242
/*!
 
1243
    Constructs a byte array of size \a size with every byte set to
 
1244
    character \a ch.
 
1245
 
 
1246
    \sa fill()
 
1247
*/
 
1248
 
 
1249
QByteArray::QByteArray(int size, char ch)
 
1250
{
 
1251
    if (size <= 0) {
 
1252
        d = &shared_null;
 
1253
    } else {
 
1254
        d = static_cast<Data *>(qMalloc(sizeof(Data)+size));
 
1255
        if (!d) {
 
1256
            d = &shared_null;
 
1257
        } else {
 
1258
            d->ref.init(0);
 
1259
            d->alloc = d->size = size;
 
1260
            d->data = d->array;
 
1261
            d->array[size] = '\0';
 
1262
            memset(d->array, ch, size);
 
1263
        }
 
1264
    }
 
1265
    d->ref.ref();
 
1266
}
 
1267
 
 
1268
/*!
 
1269
    Sets the size of the byte array to \a size bytes.
 
1270
 
 
1271
    If \a size is greater than the current size, the byte array is
 
1272
    extended to make it \a size bytes with the extra bytes added to
 
1273
    the end. The new bytes are uninitialized.
 
1274
 
 
1275
    If \a size is less than the current size, bytes are removed from
 
1276
    the end.
 
1277
 
 
1278
    \sa size()
 
1279
*/
 
1280
 
 
1281
void QByteArray::resize(int size)
 
1282
{
 
1283
    if (size <= 0) {
 
1284
        Data *x = &shared_empty;
 
1285
        x->ref.ref();
 
1286
        x = qAtomicSetPtr(&d, x);
 
1287
        if (!x->ref.deref())
 
1288
            qFree(x);
 
1289
    } else {
 
1290
        if (d->ref != 1 || size > d->alloc || (size < d->size && size < d->alloc >> 1))
 
1291
            realloc(qAllocMore(size, sizeof(Data)));
 
1292
        if (d->alloc >= size) {
 
1293
            d->size = size;
 
1294
            d->data = d->array;
 
1295
            d->array[size] = '\0';
 
1296
        }
 
1297
    }
 
1298
}
 
1299
 
 
1300
/*!
 
1301
    Sets every byte in the byte array to character \a ch. If \a size
 
1302
    is different from -1 (the default), the byte array is resized to
 
1303
    size \a size beforehand.
 
1304
 
 
1305
    Example:
 
1306
    \code
 
1307
        QByteArray ba("Istambul");
 
1308
        ba.fill("o");
 
1309
        // ba == "oooooooo"
 
1310
 
 
1311
        ba.fill("X", 2);
 
1312
        // ba == "XX"
 
1313
    \endcode
 
1314
 
 
1315
    \sa resize()
 
1316
*/
 
1317
 
 
1318
QByteArray &QByteArray::fill(char ch, int size)
 
1319
{
 
1320
    resize(size < 0 ? d->size : size);
 
1321
    if (d->size)
 
1322
        memset(d->data, ch, d->size);
 
1323
    return *this;
 
1324
}
 
1325
 
 
1326
void QByteArray::realloc(int alloc)
 
1327
{
 
1328
    if (d->ref != 1 || d->data != d->array) {
 
1329
        Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc));
 
1330
        if (!x)
 
1331
            return;
 
1332
        x->size = qMin(alloc, d->size);
 
1333
        ::memcpy(x->array, d->data, x->size);
 
1334
        x->array[x->size] = '\0';
 
1335
        x->ref.init(1);
 
1336
        x->alloc = alloc;
 
1337
        x->data = x->array;
 
1338
        x = qAtomicSetPtr(&d, x);
 
1339
        if (!x->ref.deref())
 
1340
            qFree(x);
 
1341
    } else {
 
1342
        Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc));
 
1343
        if (!x)
 
1344
            return;
 
1345
        x->alloc = alloc;
 
1346
        x->data = x->array;
 
1347
        d = x;
 
1348
    }
 
1349
}
 
1350
 
 
1351
void QByteArray::expand(int i)
 
1352
{
 
1353
    resize(qMax(i + 1, d->size));
 
1354
}
 
1355
 
 
1356
/*!
 
1357
    Prepends the byte array \a ba to this byte array and returns a
 
1358
    reference to this byte array.
 
1359
 
 
1360
    Example:
 
1361
    \code
 
1362
        QByteArray x("ship");
 
1363
        QByteArray y("air");
 
1364
        x.prepend(y);
 
1365
        // x == "airship"
 
1366
    \endcode
 
1367
 
 
1368
    This is the same as insert(0, \a ba).
 
1369
 
 
1370
    \sa append(), insert()
 
1371
*/
 
1372
 
 
1373
QByteArray &QByteArray::prepend(const QByteArray &ba)
 
1374
{
 
1375
    if (d == &shared_null || d == &shared_empty) {
 
1376
        *this = ba;
 
1377
    } else if (ba.d != &shared_null) {
 
1378
        QByteArray tmp = *this;
 
1379
        *this = ba;
 
1380
        append(tmp);
 
1381
    }
 
1382
    return *this;
 
1383
}
 
1384
 
 
1385
/*!
 
1386
    \overload
 
1387
 
 
1388
    Prepends the string \a str to this byte array.
 
1389
*/
 
1390
 
 
1391
QByteArray &QByteArray::prepend(const char *str)
 
1392
{
 
1393
    if (str) {
 
1394
        int len = qstrlen(str);
 
1395
        if (d->ref != 1 || d->size + len > d->alloc)
 
1396
            realloc(qAllocMore(d->size + len, sizeof(Data)));
 
1397
        memmove(d->data+len, d->data, d->size);
 
1398
        memcpy(d->data, str, len);
 
1399
        d->size += len;
 
1400
        d->data[d->size] = '\0';
 
1401
    }
 
1402
    return *this;
 
1403
}
 
1404
 
 
1405
/*!
 
1406
    \overload
 
1407
 
 
1408
    Prepends the character \a ch to this byte array.
 
1409
*/
 
1410
 
 
1411
QByteArray &QByteArray::prepend(char ch)
 
1412
{
 
1413
    if (d->ref != 1 || d->size + 1 > d->alloc)
 
1414
        realloc(qAllocMore(d->size + 1, sizeof(Data)));
 
1415
    memmove(d->data+1, d->data, d->size);
 
1416
    d->data[0] = ch;
 
1417
    ++d->size;
 
1418
    d->data[d->size] = '\0';
 
1419
    return *this;
 
1420
}
 
1421
 
 
1422
/*!
 
1423
    Appends the byte array \a ba onto the end of this byte array.
 
1424
 
 
1425
    Example:
 
1426
    \code
 
1427
        QByteArray x("free");
 
1428
        QByteArray y("dom");
 
1429
        x.append(y);
 
1430
        // x == "freedom"
 
1431
    \endcode
 
1432
 
 
1433
    This is the same as insert(size(), \a ba).
 
1434
 
 
1435
    This operation is typically very fast (\l{constant time}),
 
1436
    because QByteArray preallocates extra space at the end of the
 
1437
    character data so it can grow without reallocating the entire
 
1438
    data each time.
 
1439
 
 
1440
    \sa operator+=(), prepend(), insert()
 
1441
*/
 
1442
 
 
1443
QByteArray &QByteArray::append(const QByteArray &ba)
 
1444
{
 
1445
    if (d == &shared_null || d == &shared_empty) {
 
1446
        *this = ba;
 
1447
    } else if (ba.d != &shared_null) {
 
1448
        if (d->ref != 1 || d->size + ba.d->size > d->alloc)
 
1449
            realloc(qAllocMore(d->size + ba.d->size, sizeof(Data)));
 
1450
        memcpy(d->data + d->size, ba.d->data, ba.d->size);
 
1451
        d->size += ba.d->size;
 
1452
        d->data[d->size] = '\0';
 
1453
    }
 
1454
    return *this;
 
1455
}
 
1456
 
 
1457
/*! \fn QByteArray &QByteArray::append(const QString &str)
 
1458
 
 
1459
    \overload
 
1460
 
 
1461
    Appends the string \a str to this byte array. The Unicode data is
 
1462
    converted into 8-bit characters using QString::toAscii().
 
1463
 
 
1464
    If the QString contains non-ASCII Unicode characters, using this
 
1465
    function can lead to loss of information. You can disable this
 
1466
    function by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
1467
    applications. You then need to call QString::toAscii() (or
 
1468
    QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
 
1469
    explicitly if you want to convert the data to \c{const char *}.
 
1470
*/
 
1471
 
 
1472
/*!
 
1473
    \overload
 
1474
 
 
1475
    Appends the string \a str to this byte array.
 
1476
*/
 
1477
 
 
1478
QByteArray& QByteArray::append(const char *str)
 
1479
{
 
1480
    if (str) {
 
1481
        int len = qstrlen(str);
 
1482
        if (d->ref != 1 || d->size + len > d->alloc)
 
1483
            realloc(qAllocMore(d->size + len, sizeof(Data)));
 
1484
        memcpy(d->data + d->size, str, len + 1); // include null terminator
 
1485
        d->size += len;
 
1486
    }
 
1487
    return *this;
 
1488
}
 
1489
 
 
1490
/*!
 
1491
    \overload
 
1492
 
 
1493
    Appends the character \a ch to this byte array.
 
1494
*/
 
1495
 
 
1496
QByteArray& QByteArray::append(char ch)
 
1497
{
 
1498
    if (d->ref != 1 || d->size + 1 > d->alloc)
 
1499
        realloc(qAllocMore(d->size + 1, sizeof(Data)));
 
1500
    d->data[d->size++] = ch;
 
1501
    d->data[d->size] = '\0';
 
1502
    return *this;
 
1503
}
 
1504
 
 
1505
/*!
 
1506
  \internal
 
1507
  Inserts \a len bytes from the array \a arr at position \a pos and returns a
 
1508
  reference the modified byte array.
 
1509
*/
 
1510
static inline QByteArray &qbytearray_insert(QByteArray *ba,
 
1511
                                            int pos, const char *arr, int len)
 
1512
{
 
1513
    Q_ASSERT(pos >= 0);
 
1514
 
 
1515
    if (pos < 0 || len <= 0 || arr == 0)
 
1516
        return *ba;
 
1517
 
 
1518
    int oldsize = ba->size();
 
1519
    ba->resize(qMax(pos, oldsize) + len);
 
1520
    char *dst = ba->data();
 
1521
    if (pos > oldsize)
 
1522
        ::memset(dst + oldsize, 0x20, pos - oldsize);
 
1523
    else
 
1524
        ::memmove(dst + pos + len, dst + pos,
 
1525
                  (oldsize - pos) * sizeof(char));
 
1526
    memcpy(dst + pos, arr, len * sizeof(char));
 
1527
    return *ba;
 
1528
}
 
1529
 
 
1530
/*!
 
1531
    Inserts the byte array \a ba at index position \a i and returns a
 
1532
    reference to this byte array.
 
1533
 
 
1534
    Example:
 
1535
    \code
 
1536
        QByteArray ba("Meal");
 
1537
        ba.insert(1, QByteArray("ontr"));
 
1538
        // ba == "Montreal"
 
1539
    \endcode
 
1540
 
 
1541
    \sa append(), prepend(), replace(), remove()
 
1542
*/
 
1543
 
 
1544
QByteArray &QByteArray::insert(int i, const QByteArray &ba)
 
1545
{
 
1546
    QByteArray copy(ba);
 
1547
    return qbytearray_insert(this, i, copy.d->data, copy.d->size);
 
1548
}
 
1549
 
 
1550
/*!
 
1551
    \fn QByteArray &QByteArray::insert(int i, const QString &str)
 
1552
 
 
1553
    \overload
 
1554
 
 
1555
    Inserts the string \a str at index position \a i in the byte
 
1556
    array. The Unicode data is converted into 8-bit characters using
 
1557
    QString::toAscii().
 
1558
 
 
1559
    If \a i is greater than size(), the array is first extended using
 
1560
    resize().
 
1561
 
 
1562
    If the QString contains non-ASCII Unicode characters, using this
 
1563
    function can lead to loss of information. You can disable this
 
1564
    function by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
1565
    applications. You then need to call QString::toAscii() (or
 
1566
    QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
 
1567
    explicitly if you want to convert the data to \c{const char *}.
 
1568
*/
 
1569
 
 
1570
/*!
 
1571
    \overload
 
1572
 
 
1573
    Inserts the string \a str at position \a i in the byte array.
 
1574
 
 
1575
    If \a i is greater than size(), the array is first extended using
 
1576
    resize().
 
1577
*/
 
1578
 
 
1579
QByteArray &QByteArray::insert(int i, const char *str)
 
1580
{
 
1581
    return qbytearray_insert(this, i, str, qstrlen(str));
 
1582
}
 
1583
 
 
1584
/*!
 
1585
    \overload
 
1586
 
 
1587
    Inserts character \a ch at index position \a i in the byte array.
 
1588
    If \a i is greater than size(), the array is first extended using
 
1589
    resize().
 
1590
*/
 
1591
 
 
1592
QByteArray &QByteArray::insert(int i, char ch)
 
1593
{
 
1594
    return qbytearray_insert(this, i, &ch, 1);
 
1595
}
 
1596
 
 
1597
/*!
 
1598
    Removes \a len bytes from the array, starting at index position \a
 
1599
    pos, and returns a reference to the array.
 
1600
 
 
1601
    If \a pos is out of range, nothing happens. If \a pos is valid,
 
1602
    but \a pos + \a len is larger than the size of the array, the
 
1603
    array is truncated at position \a pos.
 
1604
 
 
1605
    Example:
 
1606
    \code
 
1607
        QByteArray ba("Montreal");
 
1608
        ba.remove(1, 4);
 
1609
        // ba == "Meal"
 
1610
    \endcode
 
1611
 
 
1612
    \sa insert(), replace()
 
1613
*/
 
1614
 
 
1615
QByteArray &QByteArray::remove(int pos, int len)
 
1616
{
 
1617
    if (len <= 0  || pos >= d->size || pos < 0)
 
1618
        return *this;
 
1619
    detach();
 
1620
    if (pos + len >= d->size) {
 
1621
        resize(pos);
 
1622
    } else {
 
1623
        memmove(d->data + pos, d->data + pos + len, d->size - pos - len);
 
1624
        resize(d->size - len);
 
1625
    }
 
1626
    return *this;
 
1627
}
 
1628
 
 
1629
/*!
 
1630
    Replaces \a len bytes from index position \a pos with the byte
 
1631
    array \a after, and returns a reference to this byte array.
 
1632
 
 
1633
    Example:
 
1634
    \code
 
1635
        QByteArray x("Say yes!");
 
1636
        QByteArray y("no");
 
1637
        x.replace(4, 3, y);
 
1638
        // x == "Say no!"
 
1639
    \endcode
 
1640
 
 
1641
    \sa insert(), remove()
 
1642
*/
 
1643
 
 
1644
QByteArray &QByteArray::replace(int pos, int len, const QByteArray &after)
 
1645
{
 
1646
    QByteArray copy(after);
 
1647
    remove(pos, len);
 
1648
    return insert(pos, copy);
 
1649
}
 
1650
 
 
1651
/*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after)
 
1652
 
 
1653
    \overload
 
1654
*/
 
1655
 
 
1656
/*!
 
1657
    \overload
 
1658
 
 
1659
    Replaces every occurrence of the byte array \a before with the
 
1660
    byte array \a after.
 
1661
 
 
1662
    Example:
 
1663
    \code
 
1664
        QByteArray ba("colour behaviour flavour neighbour");
 
1665
        ba.replace(QByteArray("ou"), QByteArray("o"));
 
1666
        // ba == "color behavior flavor neighbor"
 
1667
    \endcode
 
1668
*/
 
1669
 
 
1670
QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &after)
 
1671
{
 
1672
    if (isNull() || before == after)
 
1673
        return *this;
 
1674
 
 
1675
    QByteArrayMatcher matcher(before);
 
1676
    int index = 0;
 
1677
    const int bl = before.d->size;
 
1678
    const int al = after.d->size;
 
1679
    int len = d->size;
 
1680
    char *d = data();
 
1681
 
 
1682
    if (bl == al) {
 
1683
        if (bl) {
 
1684
            while ((index = matcher.indexIn(*this, index)) != -1) {
 
1685
                memcpy(d + index, after, al);
 
1686
                index += bl;
 
1687
            }
 
1688
        }
 
1689
    } else if (al < bl) {
 
1690
        uint to = 0;
 
1691
        uint movestart = 0;
 
1692
        uint num = 0;
 
1693
        while ((index = matcher.indexIn(*this, index)) != -1) {
 
1694
            if (num) {
 
1695
                int msize = index - movestart;
 
1696
                if (msize > 0) {
 
1697
                    memmove(d + to, d + movestart, msize);
 
1698
                    to += msize;
 
1699
                }
 
1700
            } else {
 
1701
                to = index;
 
1702
            }
 
1703
            if (al) {
 
1704
                memcpy(d + to, after, al);
 
1705
                to += al;
 
1706
            }
 
1707
            index += bl;
 
1708
            movestart = index;
 
1709
            num++;
 
1710
        }
 
1711
        if (num) {
 
1712
            int msize = len - movestart;
 
1713
            if (msize > 0)
 
1714
                memmove(d + to, d + movestart, msize);
 
1715
            resize(len - num*(bl-al));
 
1716
        }
 
1717
    } else {
 
1718
        // the most complex case. We don't want to lose performance by doing repeated
 
1719
        // copies and reallocs of the string.
 
1720
        while (index != -1) {
 
1721
            uint indices[4096];
 
1722
            uint pos = 0;
 
1723
            while(pos < 4095) {
 
1724
                index = matcher.indexIn(*this, index);
 
1725
                if (index == -1)
 
1726
                    break;
 
1727
                indices[pos++] = index;
 
1728
                index += bl;
 
1729
                // avoid infinite loop
 
1730
                if (!bl)
 
1731
                    index++;
 
1732
            }
 
1733
            if (!pos)
 
1734
                break;
 
1735
 
 
1736
            // we have a table of replacement positions, use them for fast replacing
 
1737
            int adjust = pos*(al-bl);
 
1738
            // index has to be adjusted in case we get back into the loop above.
 
1739
            if (index != -1)
 
1740
                index += adjust;
 
1741
            int newlen = len + adjust;
 
1742
            int moveend = len;
 
1743
            if (newlen > len) {
 
1744
                resize(newlen);
 
1745
                len = newlen;
 
1746
            }
 
1747
            d = this->d->data;
 
1748
 
 
1749
            while(pos) {
 
1750
                pos--;
 
1751
                int movestart = indices[pos] + bl;
 
1752
                int insertstart = indices[pos] + pos*(al-bl);
 
1753
                int moveto = insertstart + al;
 
1754
                memmove(d + moveto, d + movestart, (moveend - movestart));
 
1755
                if (after.size())
 
1756
                    memcpy(d + insertstart, after, al);
 
1757
                moveend = movestart - bl;
 
1758
            }
 
1759
        }
 
1760
    }
 
1761
    return *this;
 
1762
}
 
1763
 
 
1764
 
 
1765
/*! \fn QByteArray &QByteArray::replace(const char *before, const QByteArray &after)
 
1766
    \overload
 
1767
 
 
1768
    Replaces every occurrence of the string \a before with the
 
1769
    byte array \a after.
 
1770
*/
 
1771
 
 
1772
 
 
1773
/*! \fn QByteArray &QByteArray::replace(const QByteArray &before, const char *after)
 
1774
    \overload
 
1775
 
 
1776
    Replaces every occurrence of the byte array \a before with the
 
1777
    string \a after.
 
1778
*/
 
1779
 
 
1780
/*! \fn QByteArray &QByteArray::replace(const QString &before, const QByteArray &after)
 
1781
 
 
1782
    \overload
 
1783
 
 
1784
    Replaces every occurrence of the string \a before with the byte
 
1785
    array \a after. The Unicode data is converted into 8-bit
 
1786
    characters using QString::toAscii().
 
1787
 
 
1788
    If the QString contains non-ASCII Unicode characters, using this
 
1789
    function can lead to loss of information. You can disable this
 
1790
    function by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
1791
    applications. You then need to call QString::toAscii() (or
 
1792
    QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
 
1793
    explicitly if you want to convert the data to \c{const char *}.
 
1794
*/
 
1795
 
 
1796
/*! \fn QByteArray &QByteArray::replace(const QString &before, const char *after)
 
1797
    \overload
 
1798
 
 
1799
    Replaces every occurrence of the string \a before with the string
 
1800
    \a after.
 
1801
*/
 
1802
 
 
1803
/*! \fn QByteArray &QByteArray::replace(const char *before, const char *after)
 
1804
 
 
1805
    \overload
 
1806
 
 
1807
    Replaces every occurrence of the string \a before with the string
 
1808
    \a after.
 
1809
*/
 
1810
 
 
1811
/*!
 
1812
    \overload
 
1813
 
 
1814
    Replaces every occurrence of the character \a before with the
 
1815
    byte array \a after.
 
1816
*/
 
1817
 
 
1818
QByteArray &QByteArray::replace(char before, const QByteArray &after)
 
1819
{
 
1820
    char b[2] = { before, '\0' };
 
1821
    QByteArray cb = fromRawData(b, 1);
 
1822
    return replace(cb, after);
 
1823
}
 
1824
 
 
1825
/*! \fn QByteArray &QByteArray::replace(char before, const QString &after)
 
1826
 
 
1827
    \overload
 
1828
 
 
1829
    Replaces every occurrence of the character \a before with the
 
1830
    string \a after. The Unicode data is converted into 8-bit
 
1831
    characters using QString::toAscii().
 
1832
 
 
1833
    If the QString contains non-ASCII Unicode characters, using this
 
1834
    function can lead to loss of information. You can disable this
 
1835
    function by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
1836
    applications. You then need to call QString::toAscii() (or
 
1837
    QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
 
1838
    explicitly if you want to convert the data to \c{const char *}.
 
1839
*/
 
1840
 
 
1841
/*! \fn QByteArray &QByteArray::replace(char before, const char *after)
 
1842
 
 
1843
    \overload
 
1844
 
 
1845
    Replaces every occurrence of the character \a before with the
 
1846
    string \a after.
 
1847
*/
 
1848
 
 
1849
/*!
 
1850
    \overload
 
1851
 
 
1852
    Replaces every occurrence of the character \a before with the
 
1853
    character \a after.
 
1854
*/
 
1855
 
 
1856
QByteArray &QByteArray::replace(char before, char after)
 
1857
{
 
1858
    if (d->size) {
 
1859
        char *i = data();
 
1860
        char *e = i + d->size;
 
1861
        for (; i != e; ++i)
 
1862
            if (*i == before)
 
1863
                * i = after;
 
1864
    }
 
1865
    return *this;
 
1866
}
 
1867
 
 
1868
/*!
 
1869
    Splits the byte array into subarrays wherever \a sep occurs, and
 
1870
    returns the list of those arrays. If \a sep does not match
 
1871
    anywhere in the byte array, split() returns a single-element list
 
1872
    containing this byte array.
 
1873
*/
 
1874
 
 
1875
QList<QByteArray> QByteArray::split(char sep) const
 
1876
{
 
1877
    QList<QByteArray> list;
 
1878
    int start = 0;
 
1879
    int end;
 
1880
    while ((end = indexOf(sep, start)) != -1) {
 
1881
        list.append(mid(start, end - start));
 
1882
        start = end + 1;
 
1883
    }
 
1884
    list.append(mid(start));
 
1885
    return list;
 
1886
}
 
1887
 
 
1888
#define REHASH(a) \
 
1889
    if (ol_minus_1 < sizeof(uint) * CHAR_BIT) \
 
1890
        hashHaystack -= (a) << ol_minus_1; \
 
1891
    hashHaystack <<= 1
 
1892
 
 
1893
/*!
 
1894
    Returns the index position of the first occurrence of the byte
 
1895
    array \a ba in this byte array, searching forward from index
 
1896
    position \a from. Returns -1 if \a ba could not be found.
 
1897
 
 
1898
    Example:
 
1899
    \code
 
1900
        QByteArray x("sticky question");
 
1901
        QByteArray y("sti");
 
1902
        x.indexOf(y);               // returns 0
 
1903
        x.indexOf(y, 1);            // returns 10
 
1904
        x.indexOf(y, 10);           // returns 10
 
1905
        x.indexOf(y, 11);           // returns -1
 
1906
    \endcode
 
1907
 
 
1908
    \sa lastIndexOf(), contains(), count()
 
1909
*/
 
1910
 
 
1911
int QByteArray::indexOf(const QByteArray &ba, int from) const
 
1912
{
 
1913
    const int l = d->size;
 
1914
    const int ol = ba.d->size;
 
1915
    if (from > d->size || ol + from > l)
 
1916
        return -1;
 
1917
    if (ol == 0)
 
1918
        return from;
 
1919
    if (ol == 1)
 
1920
        return indexOf(*ba.d->data, from);
 
1921
 
 
1922
    if (l > 500 && ol > 5)
 
1923
        return QByteArrayMatcher(ba).indexIn(*this, from);
 
1924
 
 
1925
    const char *needle = ba.d->data;
 
1926
    const char *haystack = d->data + from;
 
1927
    const char *end = d->data + (l - ol);
 
1928
    const uint ol_minus_1 = ol - 1;
 
1929
    uint hashNeedle = 0, hashHaystack = 0;
 
1930
    int idx;
 
1931
    for (idx = 0; idx < ol; ++idx) {
 
1932
        hashNeedle = ((hashNeedle<<1) + needle[idx]);
 
1933
        hashHaystack = ((hashHaystack<<1) + haystack[idx]);
 
1934
    }
 
1935
    hashHaystack -= *(haystack + ol_minus_1);
 
1936
 
 
1937
    while (haystack <= end) {
 
1938
        hashHaystack += *(haystack + ol_minus_1);
 
1939
        if (hashHaystack == hashNeedle  && *needle == *haystack
 
1940
             && strncmp(needle, haystack, ol) == 0)
 
1941
            return haystack - d->data;
 
1942
 
 
1943
        REHASH(*haystack);
 
1944
        ++haystack;
 
1945
    }
 
1946
    return -1;
 
1947
}
 
1948
 
 
1949
/*! \fn int QByteArray::indexOf(const QString &str, int from) const
 
1950
 
 
1951
    \overload
 
1952
 
 
1953
    Returns the index position of the first occurrence of the string
 
1954
    \a str in the byte array, searching forward from index position
 
1955
    \a from. Returns -1 if \a str could not be found.
 
1956
 
 
1957
    The Unicode data is converted into 8-bit characters using
 
1958
    QString::toAscii().
 
1959
 
 
1960
    If the QString contains non-ASCII Unicode characters, using this
 
1961
    function can lead to loss of information. You can disable this
 
1962
    function by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
1963
    applications. You then need to call QString::toAscii() (or
 
1964
    QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
 
1965
    explicitly if you want to convert the data to \c{const char *}.
 
1966
*/
 
1967
 
 
1968
/*! \fn int QByteArray::indexOf(const char *str, int from) const
 
1969
 
 
1970
    \overload
 
1971
 
 
1972
    Returns the index position of the first occurrence of the string
 
1973
    \a str in the byte array, searching forward from index position \a
 
1974
    from. Returns -1 if \a str could not be found.
 
1975
*/
 
1976
 
 
1977
/*!
 
1978
    \overload
 
1979
 
 
1980
    Returns the index position of the first occurrence of the
 
1981
    character \a ch in the byte array, searching forward from index
 
1982
    position \a from. Returns -1 if \a ch could not be found.
 
1983
 
 
1984
    Example:
 
1985
    \code
 
1986
        QByteArray ba("ABCBA");
 
1987
        ba.indexOf("B");            // returns 1
 
1988
        ba.indexOf("B", 1);         // returns 1
 
1989
        ba.indexOf("B", 2);         // returns 3
 
1990
        ba.indexOf("X");            // returns -1
 
1991
    \endcode
 
1992
 
 
1993
    \sa lastIndexOf(), contains()
 
1994
*/
 
1995
 
 
1996
int QByteArray::indexOf(char ch, int from) const
 
1997
{
 
1998
    if (from < 0)
 
1999
        from = qMax(from + d->size, 0);
 
2000
    if (from < d->size) {
 
2001
        const char *n = d->data + from - 1;
 
2002
        const char *e = d->data + d->size;
 
2003
        while (++n != e)
 
2004
        if (*n == ch)
 
2005
            return  n - d->data;
 
2006
    }
 
2007
    return -1;
 
2008
}
 
2009
 
 
2010
/*!
 
2011
    Returns the index position of the last occurrence of the byte
 
2012
    array \a ba in this byte array, searching backward from index
 
2013
    position \a from. If \a from is -1 (the default), the search
 
2014
    starts at the last byte. Returns -1 if \a ba could not be found.
 
2015
 
 
2016
    Example:
 
2017
    \code
 
2018
        QByteArray x("crazy azimuths");
 
2019
        QByteArray y("azy");
 
2020
        x.lastIndexOf(y);           // returns 6
 
2021
        x.lastIndexOf(y, 6);        // returns 6
 
2022
        x.lastIndexOf(y, 5);        // returns 2
 
2023
        x.lastIndexOf(y, 1);        // returns -1
 
2024
    \endcode
 
2025
 
 
2026
    \sa indexOf(), contains(), count()
 
2027
*/
 
2028
 
 
2029
int QByteArray::lastIndexOf(const QByteArray &ba, int from) const
 
2030
{
 
2031
    const int ol = ba.d->size;
 
2032
    const int l = d->size;
 
2033
    int delta = l - ol;
 
2034
    if (from < 0)
 
2035
        from = delta;
 
2036
    if (from < 0 || from > l)
 
2037
        return -1;
 
2038
    if (from > delta)
 
2039
        from = delta;
 
2040
    if (ol == 1)
 
2041
        return lastIndexOf(*ba.d->data, from);
 
2042
 
 
2043
    const char *needle = ba.d->data;
 
2044
    const char *haystack = d->data + from;
 
2045
    const char *end = d->data;
 
2046
    const uint ol_minus_1 = ol - 1;
 
2047
    const char *n = needle + ol_minus_1;
 
2048
    const char *h = haystack + ol_minus_1;
 
2049
    uint hashNeedle = 0, hashHaystack = 0;
 
2050
    int idx;
 
2051
    for (idx = 0; idx < ol; ++idx) {
 
2052
        hashNeedle = ((hashNeedle<<1) + *(n-idx));
 
2053
        hashHaystack = ((hashHaystack<<1) + *(h-idx));
 
2054
    }
 
2055
    hashHaystack -= *haystack;
 
2056
    while (haystack >= end) {
 
2057
        hashHaystack += *haystack;
 
2058
        if (hashHaystack == hashNeedle  && strncmp(needle, haystack, ol) == 0)
 
2059
            return haystack-d->data;
 
2060
        --haystack;
 
2061
        REHASH(*(haystack + ol));
 
2062
    }
 
2063
    return -1;
 
2064
 
 
2065
}
 
2066
 
 
2067
/*! \fn int QByteArray::lastIndexOf(const QString &str, int from) const
 
2068
 
 
2069
    \overload
 
2070
 
 
2071
    Returns the index position of the last occurrence of the string \a
 
2072
    str in the byte array, searching backward from index position \a
 
2073
    from. If \a from is -1 (the default), the search starts at the
 
2074
    last (size() - 1) byte. Returns -1 if \a str could not be found.
 
2075
 
 
2076
    The Unicode data is converted into 8-bit characters using
 
2077
    QString::toAscii().
 
2078
 
 
2079
    If the QString contains non-ASCII Unicode characters, using this
 
2080
    function can lead to loss of information. You can disable this
 
2081
    function by defining \c QT_NO_CAST_TO_ASCII when you compile your
 
2082
    applications. You then need to call QString::toAscii() (or
 
2083
    QString::toLatin1() or QString::toUtf8() or QString::toLocal8Bit())
 
2084
    explicitly if you want to convert the data to \c{const char *}.
 
2085
*/
 
2086
 
 
2087
/*! \fn int QByteArray::lastIndexOf(const char *str, int from) const
 
2088
    \overload
 
2089
 
 
2090
    Returns the index position of the last occurrence of the string \a
 
2091
    str in the byte array, searching backward from index position \a
 
2092
    from. If \a from is -1 (the default), the search starts at the
 
2093
    last (size() - 1) byte. Returns -1 if \a str could not be found.
 
2094
*/
 
2095
 
 
2096
/*!
 
2097
    \overload
 
2098
 
 
2099
    Returns the index position of the last occurrence of character \a
 
2100
    ch in the byte array, searching backward from index position \a
 
2101
    from. If \a from is -1 (the default), the search starts at the
 
2102
    last (size() - 1) byte. Returns -1 if \a ch could not be found.
 
2103
 
 
2104
    Example:
 
2105
    \code
 
2106
        QByteArray ba("ABCBA");
 
2107
        ba.lastIndexOf("B");        // returns 3
 
2108
        ba.lastIndexOf("B", 3);     // returns 3
 
2109
        ba.lastIndexOf("B", 2);     // returns 1
 
2110
        ba.lastIndexOf("X");        // returns -1
 
2111
    \endcode
 
2112
 
 
2113
    \sa indexOf(), contains()
 
2114
*/
 
2115
 
 
2116
int QByteArray::lastIndexOf(char ch, int from) const
 
2117
{
 
2118
    if (from < 0)
 
2119
        from += d->size;
 
2120
    else if (from > d->size)
 
2121
        from = d->size-1;
 
2122
    if (from >= 0) {
 
2123
        const char *b = d->data;
 
2124
        const char *n = d->data + from + 1;
 
2125
        while (n-- != b)
 
2126
            if (*n == ch)
 
2127
                return  n - b;
 
2128
    }
 
2129
    return -1;
 
2130
}
 
2131
 
 
2132
/*!
 
2133
    Returns the number of (potentially overlapping) occurrences of
 
2134
    byte array \a ba in this byte array.
 
2135
 
 
2136
    \sa contains(), indexOf()
 
2137
*/
 
2138
 
 
2139
int QByteArray::count(const QByteArray &ba) const
 
2140
{
 
2141
    int num = 0;
 
2142
    int i = -1;
 
2143
    if (d->size > 500 && ba.d->size > 5) {
 
2144
        QByteArrayMatcher matcher(ba);
 
2145
        while ((i = matcher.indexIn(*this, i + 1)) != -1)
 
2146
            ++num;
 
2147
    } else {
 
2148
        while ((i = indexOf(ba, i + 1)) != -1)
 
2149
            ++num;
 
2150
    }
 
2151
    return num;
 
2152
}
 
2153
 
 
2154
/*!
 
2155
    \overload
 
2156
 
 
2157
    Returns the number of (potentially overlapping) occurrences of
 
2158
    string \a str in the byte array.
 
2159
*/
 
2160
 
 
2161
int QByteArray::count(const char *str) const
 
2162
{
 
2163
    int num = 0;
 
2164
    int i = -1;
 
2165
    while ((i = indexOf(str, i + 1)) != -1)
 
2166
        ++num;
 
2167
    return num;
 
2168
}
 
2169
 
 
2170
/*!
 
2171
    \overload
 
2172
 
 
2173
    Returns the number of occurrences of character \a ch in the byte
 
2174
    array.
 
2175
 
 
2176
    \sa contains(), indexOf()
 
2177
*/
 
2178
 
 
2179
int QByteArray::count(char ch) const
 
2180
{
 
2181
    int num = 0;
 
2182
    const char *i = d->data + d->size;
 
2183
    const char *b = d->data;
 
2184
    while (i != b)
 
2185
        if (*--i == ch)
 
2186
            ++num;
 
2187
    return num;
 
2188
}
 
2189
 
 
2190
/*! \fn int QByteArray::count() const
 
2191
 
 
2192
    \overload
 
2193
 
 
2194
    Same as size().
 
2195
*/
 
2196
 
 
2197
/*!
 
2198
    Returns true if this byte array starts with byte array \a ba;
 
2199
    otherwise returns false.
 
2200
 
 
2201
    Example:
 
2202
    \code
 
2203
        QByteArray url("ftp://ftp.trolltech.com/");
 
2204
        if (url.startsWith("ftp:"))
 
2205
            ...
 
2206
    \endcode
 
2207
 
 
2208
    \sa endsWith(), left()
 
2209
*/
 
2210
bool QByteArray::startsWith(const QByteArray &ba) const
 
2211
{
 
2212
    if (d == ba.d || ba.d->size == 0)
 
2213
        return true;
 
2214
    if (d->size < ba.d->size)
 
2215
        return false;
 
2216
    return memcmp(d->data, ba.d->data, ba.d->size) == 0;
 
2217
}
 
2218
 
 
2219
/*! \overload
 
2220
 
 
2221
    Returns true if this byte array starts with string \a str;
 
2222
    otherwise returns false.
 
2223
*/
 
2224
bool QByteArray::startsWith(const char *str) const
 
2225
{
 
2226
    if (!str || !*str)
 
2227
        return true;
 
2228
    int len = qstrlen(str);
 
2229
    if (d->size < len)
 
2230
        return false;
 
2231
    return qstrncmp(d->data, str, len) == 0;
 
2232
}
 
2233
 
 
2234
/*! \overload
 
2235
 
 
2236
    Returns true if this byte array starts with character \a ch;
 
2237
    otherwise returns false.
 
2238
*/
 
2239
bool QByteArray::startsWith(char ch) const
 
2240
{
 
2241
    if (d->size == 0)
 
2242
        return false;
 
2243
    return d->data[0] == ch;
 
2244
}
 
2245
 
 
2246
/*!
 
2247
    Returns true if this byte array ends with byte array \a ba;
 
2248
    otherwise returns false.
 
2249
 
 
2250
    Example:
 
2251
    \code
 
2252
        QByteArray url("http://www.trolltech.com/index.html");
 
2253
        if (url.endsWith(".html"))
 
2254
            ...
 
2255
    \endcode
 
2256
 
 
2257
    \sa startsWith(), right()
 
2258
*/
 
2259
bool QByteArray::endsWith(const QByteArray &ba) const
 
2260
{
 
2261
    if (d == ba.d || ba.d->size == 0)
 
2262
        return true;
 
2263
    if (d->size < ba.d->size)
 
2264
        return false;
 
2265
    return memcmp(d->data + d->size - ba.d->size, ba.d->data, ba.d->size) == 0;
 
2266
}
 
2267
 
 
2268
/*! \overload
 
2269
 
 
2270
    Returns true if this byte array ends with string \a str; otherwise
 
2271
    returns false.
 
2272
*/
 
2273
bool QByteArray::endsWith(const char *str) const
 
2274
{
 
2275
    if (!str || !*str)
 
2276
        return true;
 
2277
    int len = qstrlen(str);
 
2278
    if (d->size < len)
 
2279
        return false;
 
2280
    return qstrncmp(d->data + d->size - len, str, len) == 0;
 
2281
}
 
2282
 
 
2283
/*! \overload
 
2284
 
 
2285
    Returns true if this byte array ends with character \a ch;
 
2286
    otherwise returns false.
 
2287
*/
 
2288
bool QByteArray::endsWith(char ch) const
 
2289
{
 
2290
    if (d->size == 0)
 
2291
        return false;
 
2292
    return d->data[d->size - 1] == ch;
 
2293
}
 
2294
 
 
2295
/*!
 
2296
    Returns a byte array that contains the leftmost \a len bytes of
 
2297
    this byte array.
 
2298
 
 
2299
    The entire byte array is returned if \a len is greater than
 
2300
    size().
 
2301
 
 
2302
    Example:
 
2303
    \code
 
2304
        QByteArray x("Pineapple");
 
2305
        QByteArray y = x.left(4);
 
2306
        // y == "Pine"
 
2307
    \endcode
 
2308
 
 
2309
    \sa right(), mid(), startsWith(), truncate()
 
2310
*/
 
2311
 
 
2312
QByteArray QByteArray::left(int len)  const
 
2313
{
 
2314
    if (len >= d->size)
 
2315
        return *this;
 
2316
    if (len < 0)
 
2317
        len = 0;
 
2318
    return QByteArray(d->data, len);
 
2319
}
 
2320
 
 
2321
/*!
 
2322
    Returns a byte array that contains the rightmost \a len bytes of
 
2323
    this byte array.
 
2324
 
 
2325
    The entire byte array is returned if \a len is greater than
 
2326
    size().
 
2327
 
 
2328
    Example:
 
2329
    \code
 
2330
        QByteArray x("Pineapple");
 
2331
        QByteArray y = x.right(5);
 
2332
        // y == "apple"
 
2333
    \endcode
 
2334
 
 
2335
    \sa endsWith(), left(), mid()
 
2336
*/
 
2337
 
 
2338
QByteArray QByteArray::right(int len) const
 
2339
{
 
2340
    if (len >= d->size)
 
2341
        return *this;
 
2342
    if (len < 0)
 
2343
        len = 0;
 
2344
    return QByteArray(d->data + d->size - len, len);
 
2345
}
 
2346
 
 
2347
/*!
 
2348
    Returns a byte array containing \a len bytes from this byte array,
 
2349
    starting at position \a pos.
 
2350
 
 
2351
    If \a len is -1 (the default), or \a pos + \a len >= size(),
 
2352
    returns a byte array containing all bytes starting at position \a
 
2353
    pos until the end of the byte array.
 
2354
 
 
2355
    Example:
 
2356
    \code
 
2357
        QByteArray x("Five pineapples");
 
2358
        QByteArray y = x.mid(5, 4);     // y == "pine"
 
2359
        QByteArray z = x.mid(5);        // z == "pineapples"
 
2360
    \endcode
 
2361
 
 
2362
    \sa left(), right()
 
2363
*/
 
2364
 
 
2365
QByteArray QByteArray::mid(int pos, int len) const
 
2366
{
 
2367
    if (d == &shared_null || d == &shared_empty || pos >= d->size)
 
2368
        return QByteArray();
 
2369
    if (len < 0)
 
2370
        len = d->size - pos;
 
2371
    if (pos < 0) {
 
2372
        len += pos;
 
2373
        pos = 0;
 
2374
    }
 
2375
    if (len + pos > d->size)
 
2376
        len = d->size - pos;
 
2377
    if (pos == 0 && len == d->size)
 
2378
        return *this;
 
2379
    return QByteArray(d->data + pos, len);
 
2380
}
 
2381
 
 
2382
/*!
 
2383
    Returns a lowercase copy of the byte array. The bytearray is
 
2384
    interpreted as a Latin-1 encoded string.
 
2385
 
 
2386
    Example:
 
2387
    \code
 
2388
        QByteArray x("TROlltECH");
 
2389
        QByteArray y = x.toLower();
 
2390
        // y == "trolltech"
 
2391
    \endcode
 
2392
 
 
2393
    \sa toUpper(), {Note on 8-bit character comparisons}
 
2394
*/
 
2395
QByteArray QByteArray::toLower() const
 
2396
{
 
2397
    QByteArray s(*this);
 
2398
    register uchar *p = reinterpret_cast<uchar *>(s.data());
 
2399
    if (p) {
 
2400
        while (*p) {
 
2401
            *p = QUnicodeTables::lower(*p);
 
2402
            p++;
 
2403
        }
 
2404
    }
 
2405
    return s;
 
2406
}
 
2407
 
 
2408
/*!
 
2409
    Returns an uppercase copy of the byte array. The bytearray is
 
2410
    interpreted as a Latin-1 encoded string.
 
2411
 
 
2412
    Example:
 
2413
    \code
 
2414
        QByteArray x("TROlltECH");
 
2415
        QByteArray y = x.toUpper();
 
2416
        // y == "TROLLTECH"
 
2417
    \endcode
 
2418
 
 
2419
    \sa toLower(), {Note on 8-bit character comparisons}
 
2420
*/
 
2421
 
 
2422
QByteArray QByteArray::toUpper() const
 
2423
{
 
2424
    QByteArray s(*this);
 
2425
    register char *p = s.data();
 
2426
    if (p) {
 
2427
        while (*p) {
 
2428
            *p = QUnicodeTables::upper(*p);
 
2429
            p++;
 
2430
        }
 
2431
    }
 
2432
    return s;
 
2433
}
 
2434
 
 
2435
/*! \fn void QByteArray::clear()
 
2436
 
 
2437
    Clears the contents of the byte array and makes it empty.
 
2438
 
 
2439
    \sa resize(), isEmpty()
 
2440
*/
 
2441
 
 
2442
void QByteArray::clear()
 
2443
{
 
2444
    if (!d->ref.deref())
 
2445
        qFree(d);
 
2446
    d = &shared_null;
 
2447
    d->ref.ref();
 
2448
}
 
2449
 
 
2450
/*! \relates QByteArray
 
2451
 
 
2452
    Writes byte array \a ba to the stream \a out and returns a reference
 
2453
    to the stream.
 
2454
 
 
2455
    \sa {Format of the QDataStream operators}
 
2456
*/
 
2457
#ifndef QT_NO_DATASTREAM
 
2458
 
 
2459
QDataStream &operator<<(QDataStream &out, const QByteArray &ba)
 
2460
{
 
2461
    if (ba.isNull() && out.version() >= 6) {
 
2462
        out << (quint32)0xffffffff;
 
2463
        return out;
 
2464
    }
 
2465
    return out.writeBytes(ba, ba.size());
 
2466
}
 
2467
 
 
2468
/*! \relates QByteArray
 
2469
 
 
2470
    Reads a byte array into \a ba from the stream \a in and returns a
 
2471
    reference to the stream.
 
2472
 
 
2473
    \sa {Format of the QDataStream operators}
 
2474
*/
 
2475
 
 
2476
QDataStream &operator>>(QDataStream &in, QByteArray &ba)
 
2477
{
 
2478
    ba.clear();
 
2479
    quint32 len;
 
2480
    in >> len;
 
2481
    if (len == 0xffffffff)
 
2482
        return in;
 
2483
 
 
2484
    const quint32 Step = 1024 * 1024;
 
2485
    quint32 allocated = 0;
 
2486
 
 
2487
    do {
 
2488
        int blockSize = qMin(Step, len - allocated);
 
2489
        ba.resize(allocated + blockSize);
 
2490
        if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
 
2491
            ba.clear();
 
2492
            in.setStatus(QDataStream::ReadPastEnd);
 
2493
            return in;
 
2494
        }
 
2495
        allocated += blockSize;
 
2496
    } while (allocated < len);
 
2497
 
 
2498
    return in;
 
2499
}
 
2500
#endif //QT_NO_DATASTREAM
 
2501
 
 
2502
/*! \fn bool QByteArray::operator==(const QString &str) const
 
2503
 
 
2504
    Returns true if this byte array is equal to string \a str;
 
2505
    otherwise returns false.
 
2506
 
 
2507
    The Unicode data is converted into 8-bit characters using
 
2508
    QString::toAscii().
 
2509
 
 
2510
    The comparison is case sensitive.
 
2511
 
 
2512
    You can disable this operator by defining \c
 
2513
    QT_NO_CAST_FROM_ASCII when you compile your applications. You
 
2514
    then need to call QString::fromAscii(), QString::fromLatin1(),
 
2515
    QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if
 
2516
    you want to convert the byte array to a QString before doing the
 
2517
    comparison.
 
2518
*/
 
2519
 
 
2520
/*! \fn bool QByteArray::operator!=(const QString &str) const
 
2521
 
 
2522
    Returns true if this byte array is not equal to string \a str;
 
2523
    otherwise returns false.
 
2524
 
 
2525
    The Unicode data is converted into 8-bit characters using
 
2526
    QString::toAscii().
 
2527
 
 
2528
    The comparison is case sensitive.
 
2529
 
 
2530
    You can disable this operator by defining \c
 
2531
    QT_NO_CAST_FROM_ASCII when you compile your applications. You
 
2532
    then need to call QString::fromAscii(), QString::fromLatin1(),
 
2533
    QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if
 
2534
    you want to convert the byte array to a QString before doing the
 
2535
    comparison.
 
2536
*/
 
2537
 
 
2538
/*! \fn bool QByteArray::operator<(const QString &str) const
 
2539
 
 
2540
    Returns true if this byte array is lexically less than string \a
 
2541
    str; otherwise returns false.
 
2542
 
 
2543
    The Unicode data is converted into 8-bit characters using
 
2544
    QString::toAscii().
 
2545
 
 
2546
    The comparison is case sensitive.
 
2547
 
 
2548
    You can disable this operator by defining \c
 
2549
    QT_NO_CAST_FROM_ASCII when you compile your applications. You
 
2550
    then need to call QString::fromAscii(), QString::fromLatin1(),
 
2551
    QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if
 
2552
    you want to convert the byte array to a QString before doing the
 
2553
    comparison.
 
2554
*/
 
2555
 
 
2556
/*! \fn bool QByteArray::operator>(const QString &str) const
 
2557
 
 
2558
    Returns true if this byte array is lexically greater than string
 
2559
    \a str; otherwise returns false.
 
2560
 
 
2561
    The Unicode data is converted into 8-bit characters using
 
2562
    QString::toAscii().
 
2563
 
 
2564
    The comparison is case sensitive.
 
2565
 
 
2566
    You can disable this operator by defining \c
 
2567
    QT_NO_CAST_FROM_ASCII when you compile your applications. You
 
2568
    then need to call QString::fromAscii(), QString::fromLatin1(),
 
2569
    QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if
 
2570
    you want to convert the byte array to a QString before doing the
 
2571
    comparison.
 
2572
*/
 
2573
 
 
2574
/*! \fn bool QByteArray::operator<=(const QString &str) const
 
2575
 
 
2576
    Returns true if this byte array is lexically less than or equal
 
2577
    to string \a str; otherwise returns false.
 
2578
 
 
2579
    The Unicode data is converted into 8-bit characters using
 
2580
    QString::toAscii().
 
2581
 
 
2582
    The comparison is case sensitive.
 
2583
 
 
2584
    You can disable this operator by defining \c
 
2585
    QT_NO_CAST_FROM_ASCII when you compile your applications. You
 
2586
    then need to call QString::fromAscii(), QString::fromLatin1(),
 
2587
    QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if
 
2588
    you want to convert the byte array to a QString before doing the
 
2589
    comparison.
 
2590
*/
 
2591
 
 
2592
/*! \fn bool QByteArray::operator>=(const QString &str) const
 
2593
 
 
2594
    Returns true if this byte array is greater than or equal to string
 
2595
    \a str; otherwise returns false.
 
2596
 
 
2597
    The Unicode data is converted into 8-bit characters using
 
2598
    QString::toAscii().
 
2599
 
 
2600
    The comparison is case sensitive.
 
2601
 
 
2602
    You can disable this operator by defining \c
 
2603
    QT_NO_CAST_FROM_ASCII when you compile your applications. You
 
2604
    then need to call QString::fromAscii(), QString::fromLatin1(),
 
2605
    QString::fromUtf8(), or QString::fromLocal8Bit() explicitly if
 
2606
    you want to convert the byte array to a QString before doing the
 
2607
    comparison.
 
2608
*/
 
2609
 
 
2610
/*! \fn bool operator==(const QByteArray &a1, const QByteArray &a2)
 
2611
    \relates QByteArray
 
2612
 
 
2613
    \overload
 
2614
 
 
2615
    Returns true if byte array \a a1 is equal to byte array \a a2;
 
2616
    otherwise returns false.
 
2617
*/
 
2618
 
 
2619
/*! \fn bool operator==(const QByteArray &a1, const char *a2)
 
2620
    \relates QByteArray
 
2621
 
 
2622
    \overload
 
2623
 
 
2624
    Returns true if byte array \a a1 is equal to string \a a2;
 
2625
    otherwise returns false.
 
2626
*/
 
2627
 
 
2628
/*! \fn bool operator==(const char *a1, const QByteArray &a2)
 
2629
    \relates QByteArray
 
2630
 
 
2631
    \overload
 
2632
 
 
2633
    Returns true if string \a a1 is equal to byte array \a a2;
 
2634
    otherwise returns false.
 
2635
*/
 
2636
 
 
2637
/*! \fn bool operator!=(const QByteArray &a1, const QByteArray &a2)
 
2638
    \relates QByteArray
 
2639
 
 
2640
    \overload
 
2641
 
 
2642
    Returns true if byte array \a a1 is not equal to byte array \a a2;
 
2643
    otherwise returns false.
 
2644
*/
 
2645
 
 
2646
/*! \fn bool operator!=(const QByteArray &a1, const char *a2)
 
2647
    \relates QByteArray
 
2648
 
 
2649
    \overload
 
2650
 
 
2651
    Returns true if byte array \a a1 is not equal to string \a a2;
 
2652
    otherwise returns false.
 
2653
*/
 
2654
 
 
2655
/*! \fn bool operator!=(const char *a1, const QByteArray &a2)
 
2656
    \relates QByteArray
 
2657
 
 
2658
    \overload
 
2659
 
 
2660
    Returns true if string \a a1 is not equal to byte array \a a2;
 
2661
    otherwise returns false.
 
2662
*/
 
2663
 
 
2664
/*! \fn bool operator<(const QByteArray &a1, const QByteArray &a2)
 
2665
    \relates QByteArray
 
2666
 
 
2667
    \overload
 
2668
 
 
2669
    Returns true if byte array \a a1 is lexically less than byte array
 
2670
    \a a2; otherwise returns false.
 
2671
*/
 
2672
 
 
2673
/*! \fn inline bool operator<(const QByteArray &a1, const char *a2)
 
2674
    \relates QByteArray
 
2675
 
 
2676
    \overload
 
2677
 
 
2678
    Returns true if byte array \a a1 is lexically less than string
 
2679
    \a a2; otherwise returns false.
 
2680
*/
 
2681
 
 
2682
/*! \fn bool operator<(const char *a1, const QByteArray &a2)
 
2683
    \relates QByteArray
 
2684
 
 
2685
    \overload
 
2686
 
 
2687
    Returns true if string \a a1 is lexically less than byte array
 
2688
    \a a2; otherwise returns false.
 
2689
*/
 
2690
 
 
2691
/*! \fn bool operator<=(const QByteArray &a1, const QByteArray &a2)
 
2692
    \relates QByteArray
 
2693
 
 
2694
    \overload
 
2695
 
 
2696
    Returns true if byte array \a a1 is lexically less than or equal
 
2697
    to byte array \a a2; otherwise returns false.
 
2698
*/
 
2699
 
 
2700
/*! \fn bool operator<=(const QByteArray &a1, const char *a2)
 
2701
    \relates QByteArray
 
2702
 
 
2703
    \overload
 
2704
 
 
2705
    Returns true if byte array \a a1 is lexically less than or equal
 
2706
    to string \a a2; otherwise returns false.
 
2707
*/
 
2708
 
 
2709
/*! \fn bool operator<=(const char *a1, const QByteArray &a2)
 
2710
    \relates QByteArray
 
2711
 
 
2712
    \overload
 
2713
 
 
2714
    Returns true if string \a a1 is lexically less than or equal
 
2715
    to byte array \a a2; otherwise returns false.
 
2716
*/
 
2717
 
 
2718
/*! \fn bool operator>(const QByteArray &a1, const QByteArray &a2)
 
2719
    \relates QByteArray
 
2720
 
 
2721
    \overload
 
2722
 
 
2723
    Returns true if byte array \a a1 is lexically greater than byte
 
2724
    array \a a2; otherwise returns false.
 
2725
*/
 
2726
 
 
2727
/*! \fn bool operator>(const QByteArray &a1, const char *a2)
 
2728
    \relates QByteArray
 
2729
 
 
2730
    \overload
 
2731
 
 
2732
    Returns true if byte array \a a1 is lexically greater than string
 
2733
    \a a2; otherwise returns false.
 
2734
*/
 
2735
 
 
2736
/*! \fn bool operator>(const char *a1, const QByteArray &a2)
 
2737
    \relates QByteArray
 
2738
 
 
2739
    \overload
 
2740
 
 
2741
    Returns true if string \a a1 is lexically greater than byte array
 
2742
    \a a2; otherwise returns false.
 
2743
*/
 
2744
 
 
2745
/*! \fn bool operator>=(const QByteArray &a1, const QByteArray &a2)
 
2746
    \relates QByteArray
 
2747
 
 
2748
    \overload
 
2749
 
 
2750
    Returns true if byte array \a a1 is lexically greater than or
 
2751
    equal to byte array \a a2; otherwise returns false.
 
2752
*/
 
2753
 
 
2754
/*! \fn bool operator>=(const QByteArray &a1, const char *a2)
 
2755
    \relates QByteArray
 
2756
 
 
2757
    \overload
 
2758
 
 
2759
    Returns true if byte array \a a1 is lexically greater than or
 
2760
    equal to string \a a2; otherwise returns false.
 
2761
*/
 
2762
 
 
2763
/*! \fn bool operator>=(const char *a1, const QByteArray &a2)
 
2764
    \relates QByteArray
 
2765
 
 
2766
    \overload
 
2767
 
 
2768
    Returns true if string \a a1 is lexically greater than or
 
2769
    equal to byte array \a a2; otherwise returns false.
 
2770
*/
 
2771
 
 
2772
/*! \fn const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
 
2773
    \relates QByteArray
 
2774
 
 
2775
    Returns a byte array that is the result of concatenating byte
 
2776
    array \a a1 and byte array \a a2.
 
2777
 
 
2778
    \sa QByteArray::operator+=()
 
2779
*/
 
2780
 
 
2781
/*! \fn const QByteArray operator+(const QByteArray &a1, const char *a2)
 
2782
    \relates QByteArray
 
2783
 
 
2784
    \overload
 
2785
 
 
2786
    Returns a byte array that is the result of concatenating byte
 
2787
    array \a a1 and string \a a2.
 
2788
*/
 
2789
 
 
2790
/*! \fn const QByteArray operator+(const QByteArray &a1, char a2)
 
2791
    \relates QByteArray
 
2792
 
 
2793
    \overload
 
2794
 
 
2795
    Returns a byte array that is the result of concatenating byte
 
2796
    array \a a1 and character \a a2.
 
2797
*/
 
2798
 
 
2799
/*! \fn const QByteArray operator+(const char *a1, const QByteArray &a2)
 
2800
    \relates QByteArray
 
2801
 
 
2802
    \overload
 
2803
 
 
2804
    Returns a byte array that is the result of concatenating string
 
2805
    \a a1 and byte array \a a2.
 
2806
*/
 
2807
 
 
2808
/*! \fn const QByteArray operator+(char a1, const QByteArray &a2)
 
2809
    \relates QByteArray
 
2810
 
 
2811
    \overload
 
2812
 
 
2813
    Returns a byte array that is the result of concatenating character
 
2814
    \a a1 and byte array \a a2.
 
2815
*/
 
2816
 
 
2817
/*!
 
2818
    Returns a byte array that has whitespace removed from the start
 
2819
    and the end, and which has each sequence of internal whitespace
 
2820
    replaced with a single space.
 
2821
 
 
2822
    Whitespace means any character for which the standard C++
 
2823
    isspace() function returns true. This includes the ASCII
 
2824
    characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '.
 
2825
 
 
2826
    Example:
 
2827
    \code
 
2828
        QByteArray ba("  lots\t of\nwhitespace\r\n ");
 
2829
        ba = ba.trimmed();
 
2830
        // ba == "lots of whitespace";
 
2831
    \endcode
 
2832
 
 
2833
    \sa trimmed()
 
2834
*/
 
2835
QByteArray QByteArray::simplified() const
 
2836
{
 
2837
    if (d->size == 0)
 
2838
        return *this;
 
2839
    QByteArray result;
 
2840
    result.resize(d->size);
 
2841
    const char *from = d->data;
 
2842
    const char *fromend = from + d->size;
 
2843
    int outc=0;
 
2844
    char *to = result.d->data;
 
2845
    for (;;) {
 
2846
        while (from!=fromend && isspace(uchar(*from)))
 
2847
            from++;
 
2848
        while (from!=fromend && !isspace(uchar(*from)))
 
2849
            to[outc++] = *from++;
 
2850
        if (from!=fromend)
 
2851
            to[outc++] = ' ';
 
2852
        else
 
2853
            break;
 
2854
    }
 
2855
    if (outc > 0 && to[outc-1] == ' ')
 
2856
        outc--;
 
2857
    result.resize(outc);
 
2858
    return result;
 
2859
}
 
2860
 
 
2861
/*!
 
2862
    Returns a byte array that has whitespace removed from the start
 
2863
    and the end.
 
2864
 
 
2865
    Whitespace means any character for which the standard C++
 
2866
    isspace() function returns true. This includes the ASCII
 
2867
    characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '.
 
2868
 
 
2869
    Example:
 
2870
    \code
 
2871
        QByteArray ba("  lots\t of\nwhitespace\r\n ");
 
2872
        ba = ba.trimmed();
 
2873
        // ba == "lots\t of\nwhitespace";
 
2874
    \endcode
 
2875
 
 
2876
    Unlike simplified(), trimmed() leaves internal whitespace alone.
 
2877
 
 
2878
    \sa simplified()
 
2879
*/
 
2880
QByteArray QByteArray::trimmed() const
 
2881
{
 
2882
    if (d->size == 0)
 
2883
        return *this;
 
2884
    const char *s = d->data;
 
2885
    if (!isspace(uchar(*s)) && !isspace(uchar(s[d->size-1])))
 
2886
        return *this;
 
2887
    int start = 0;
 
2888
    int end = d->size - 1;
 
2889
    while (start<=end && isspace(uchar(s[start])))  // skip white space from start
 
2890
        start++;
 
2891
    if (start <= end) {                          // only white space
 
2892
        while (end && isspace(uchar(s[end])))           // skip white space from end
 
2893
            end--;
 
2894
    }
 
2895
    int l = end - start + 1;
 
2896
    if (l <= 0) {
 
2897
        shared_empty.ref.ref();
 
2898
        return QByteArray(&shared_empty, 0, 0);
 
2899
    }
 
2900
    return QByteArray(s+start, l);
 
2901
}
 
2902
 
 
2903
/*!
 
2904
    Returns a byte array of size \a width that contains this byte
 
2905
    array padded by the \a fill character.
 
2906
 
 
2907
    If \a truncate is false and the size() of the byte array is more
 
2908
    than \a width, then the returned byte array is a copy of this byte
 
2909
    array.
 
2910
 
 
2911
    If \a truncate is true and the size() of the byte array is more
 
2912
    than \a width, then any bytes in a copy of the byte array
 
2913
    after position \a width are removed, and the copy is returned.
 
2914
 
 
2915
    Example:
 
2916
    \code
 
2917
        QByteArray x("apple");
 
2918
        QByteArray y = x.leftJustified(8, '.');   // y == "apple..."
 
2919
    \endcode
 
2920
 
 
2921
    \sa rightJustified()
 
2922
*/
 
2923
 
 
2924
QByteArray QByteArray::leftJustified(int width, char fill, bool truncate) const
 
2925
{
 
2926
    QByteArray result;
 
2927
    int len = qstrlen(d->data);
 
2928
    int padlen = width - len;
 
2929
    if (padlen > 0) {
 
2930
        result.resize(len+padlen);
 
2931
        if (len)
 
2932
            memcpy(result.d->data, d->data, sizeof(char)*len);
 
2933
        memset(result.d->data+len, fill, padlen);
 
2934
    } else {
 
2935
        if (truncate)
 
2936
            result = left(width);
 
2937
        else
 
2938
            result = *this;
 
2939
    }
 
2940
    return result;
 
2941
}
 
2942
 
 
2943
/*!
 
2944
    Returns a byte array of size \a width that contains the \a fill
 
2945
    character followed by this byte array.
 
2946
 
 
2947
    If \a truncate is false and the size of the byte array is more
 
2948
    than \a width, then the returned byte array is a copy of this byte
 
2949
    array.
 
2950
 
 
2951
    If \a truncate is true and the size of the byte array is more
 
2952
    than \a width, then the resulting byte array is truncated at
 
2953
    position \a width.
 
2954
 
 
2955
    Example:
 
2956
    \code
 
2957
        QByteArray x("apple");
 
2958
        QByteArray y = x.rightJustified(8, '.');    // y == "...apple"
 
2959
    \endcode
 
2960
 
 
2961
    \sa leftJustified()
 
2962
*/
 
2963
 
 
2964
QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const
 
2965
{
 
2966
    QByteArray result;
 
2967
    int len = qstrlen(d->data);
 
2968
    int padlen = width - len;
 
2969
    if (padlen > 0) {
 
2970
        result.resize(len+padlen);
 
2971
        if (len)
 
2972
            memcpy(result.d->data+padlen, data(), len);
 
2973
        memset(result.d->data, fill, padlen);
 
2974
    } else {
 
2975
        if (truncate)
 
2976
            result = left(width);
 
2977
        else
 
2978
            result = *this;
 
2979
    }
 
2980
    return result;
 
2981
}
 
2982
 
 
2983
bool QByteArray::isNull() const { return d == &shared_null; }
 
2984
 
 
2985
 
 
2986
/*!
 
2987
    Returns the byte array converted to a \c {long long} using base \a
 
2988
    base, which is 10 by default and must be between 2 and 36, or 0.
 
2989
 
 
2990
    If \a base is 0, the base is determined automatically using the
 
2991
    following rules: If the byte array begins with "0x", it is assumed to
 
2992
    be hexadecimal; if it begins with "0", it is assumed to be octal;
 
2993
    otherwise it is assumed to be decimal.
 
2994
 
 
2995
    Returns 0 if the conversion fails.
 
2996
 
 
2997
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
2998
    false; otherwise *\a{ok} is set to true.
 
2999
 
 
3000
    \sa number()
 
3001
*/
 
3002
 
 
3003
qlonglong QByteArray::toLongLong(bool *ok, int base) const
 
3004
{
 
3005
#if defined(QT_CHECK_RANGE)
 
3006
    if (base != 0 && (base < 2 || base > 36)) {
 
3007
        qWarning("QByteArray::toLongLong: Invalid base %d", base);
 
3008
        base = 10;
 
3009
    }
 
3010
#endif
 
3011
 
 
3012
    return QLocalePrivate::bytearrayToLongLong(constData(), base, ok);
 
3013
}
 
3014
 
 
3015
/*!
 
3016
    Returns the byte array converted to an \c {unsigned long long}
 
3017
    using base \a base, which is 10 by default and must be between 2
 
3018
    and 36, or 0.
 
3019
 
 
3020
    If \a base is 0, the base is determined automatically using the
 
3021
    following rules: If the byte array begins with "0x", it is assumed to
 
3022
    be hexadecimal; if it begins with "0", it is assumed to be octal;
 
3023
    otherwise it is assumed to be decimal.
 
3024
 
 
3025
    Returns 0 if the conversion fails.
 
3026
 
 
3027
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
3028
    false; otherwise *\a{ok} is set to true.
 
3029
 
 
3030
    \sa number()
 
3031
*/
 
3032
 
 
3033
qulonglong QByteArray::toULongLong(bool *ok, int base) const
 
3034
{
 
3035
#if defined(QT_CHECK_RANGE)
 
3036
    if (base != 0 && (base < 2 || base > 36)) {
 
3037
        qWarning("QByteArray::toULongLong: Invalid base %d", base);
 
3038
        base = 10;
 
3039
    }
 
3040
#endif
 
3041
 
 
3042
    return QLocalePrivate::bytearrayToUnsLongLong(constData(), base, ok);
 
3043
}
 
3044
 
 
3045
 
 
3046
/*!
 
3047
    Returns the byte array converted to an \c int using base \a
 
3048
    base, which is 10 by default and must be between 2 and 36, or 0.
 
3049
 
 
3050
    If \a base is 0, the base is determined automatically using the
 
3051
    following rules: If the byte array begins with "0x", it is assumed to
 
3052
    be hexadecimal; if it begins with "0", it is assumed to be octal;
 
3053
    otherwise it is assumed to be decimal.
 
3054
 
 
3055
    Returns 0 if the conversion fails.
 
3056
 
 
3057
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
3058
    false; otherwise *\a{ok} is set to true.
 
3059
 
 
3060
    \code
 
3061
        QByteArray str("FF");
 
3062
        bool ok;
 
3063
        int hex = str.toInt(&ok, 16);     // hex == 255, ok == true
 
3064
        int dec = str.toInt(&ok, 10);     // dec == 0, ok == false
 
3065
    \endcode
 
3066
 
 
3067
    \sa number()
 
3068
*/
 
3069
 
 
3070
int QByteArray::toInt(bool *ok, int base) const
 
3071
{
 
3072
    long v = toLongLong(ok, base);
 
3073
    if (v < INT_MIN || v > INT_MAX) {
 
3074
        if (ok)
 
3075
            *ok = false;
 
3076
        v = 0;
 
3077
    }
 
3078
    return int(v);
 
3079
}
 
3080
 
 
3081
/*!
 
3082
    Returns the byte array converted to an \c {unsigned int} using base \a
 
3083
    base, which is 10 by default and must be between 2 and 36, or 0.
 
3084
 
 
3085
    If \a base is 0, the base is determined automatically using the
 
3086
    following rules: If the byte array begins with "0x", it is assumed to
 
3087
    be hexadecimal; if it begins with "0", it is assumed to be octal;
 
3088
    otherwise it is assumed to be decimal.
 
3089
 
 
3090
    Returns 0 if the conversion fails.
 
3091
 
 
3092
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
3093
    false; otherwise *\a{ok} is set to true.
 
3094
 
 
3095
    \sa number()
 
3096
*/
 
3097
 
 
3098
uint QByteArray::toUInt(bool *ok, int base) const
 
3099
{
 
3100
    ulong v = toULongLong(ok, base);
 
3101
    if (v > UINT_MAX) {
 
3102
        if (ok)
 
3103
            *ok = false;
 
3104
        v = 0;
 
3105
    }
 
3106
    return uint(v);
 
3107
}
 
3108
 
 
3109
/*!
 
3110
    Returns the byte array converted to a \c short using base \a
 
3111
    base, which is 10 by default and must be between 2 and 36, or 0.
 
3112
 
 
3113
    If \a base is 0, the base is determined automatically using the
 
3114
    following rules: If the byte array begins with "0x", it is assumed to
 
3115
    be hexadecimal; if it begins with "0", it is assumed to be octal;
 
3116
    otherwise it is assumed to be decimal.
 
3117
 
 
3118
    Returns 0 if the conversion fails.
 
3119
 
 
3120
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
3121
    false; otherwise *\a{ok} is set to true.
 
3122
 
 
3123
    \sa number()
 
3124
*/
 
3125
 
 
3126
short QByteArray::toShort(bool *ok, int base) const
 
3127
{
 
3128
    long v = toLongLong(ok, base);
 
3129
    if (v < SHRT_MIN || v > SHRT_MAX) {
 
3130
        if (ok)
 
3131
            *ok = false;
 
3132
        v = 0;
 
3133
    }
 
3134
    return short(v);
 
3135
}
 
3136
 
 
3137
/*!
 
3138
    Returns the byte array converted to an \c {unsigned short} using base \a
 
3139
    base, which is 10 by default and must be between 2 and 36, or 0.
 
3140
 
 
3141
    If \a base is 0, the base is determined automatically using the
 
3142
    following rules: If the byte array begins with "0x", it is assumed to
 
3143
    be hexadecimal; if it begins with "0", it is assumed to be octal;
 
3144
    otherwise it is assumed to be decimal.
 
3145
 
 
3146
    Returns 0 if the conversion fails.
 
3147
 
 
3148
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
3149
    false; otherwise *\a{ok} is set to true.
 
3150
 
 
3151
    \sa number()
 
3152
*/
 
3153
 
 
3154
ushort QByteArray::toUShort(bool *ok, int base) const
 
3155
{
 
3156
    ulong v = toULongLong(ok, base);
 
3157
    if (v > USHRT_MAX) {
 
3158
        if (ok)
 
3159
            *ok = false;
 
3160
        v = 0;
 
3161
    }
 
3162
    return ushort(v);
 
3163
}
 
3164
 
 
3165
 
 
3166
/*!
 
3167
    Returns the byte array converted to a \c double value.
 
3168
 
 
3169
    Returns 0.0 if the conversion fails.
 
3170
 
 
3171
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
3172
    false; otherwise *\a{ok} is set to true.
 
3173
 
 
3174
    \code
 
3175
        QByteArray string("1234.56");
 
3176
        double a = string.toDouble();   // a == 1234.56
 
3177
    \endcode
 
3178
 
 
3179
    \sa number()
 
3180
*/
 
3181
 
 
3182
double QByteArray::toDouble(bool *ok) const
 
3183
{
 
3184
    return QLocalePrivate::bytearrayToDouble(constData(), ok);
 
3185
}
 
3186
 
 
3187
/*!
 
3188
    Returns the byte array converted to a \c float value.
 
3189
 
 
3190
    Returns 0.0 if the conversion fails.
 
3191
 
 
3192
    If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
 
3193
    false; otherwise *\a{ok} is set to true.
 
3194
 
 
3195
    \sa number()
 
3196
*/
 
3197
 
 
3198
float QByteArray::toFloat(bool *ok) const
 
3199
{
 
3200
    return float(toDouble(ok));
 
3201
}
 
3202
 
 
3203
/*!
 
3204
    Returns a copy of the byte array, encoded as Base64.
 
3205
 
 
3206
    \code
 
3207
        QByteArray text("Qt is great!");
 
3208
        text.toBase64();        // returns "UXQgaXMgZ3JlYXRcIQo="
 
3209
    \endcode
 
3210
 
 
3211
    \sa fromBase64()
 
3212
*/
 
3213
QByteArray QByteArray::toBase64() const
 
3214
{
 
3215
    const char alphabet[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
 
3216
                            "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
 
3217
    const char padchar = '=';
 
3218
    int padlen = 0;
 
3219
 
 
3220
    QByteArray tmp;
 
3221
    tmp.resize(((d->size * 4) / 3) + 3);
 
3222
 
 
3223
    int i = 0;
 
3224
    char *out = tmp.data();
 
3225
    while (i < d->size) {
 
3226
        int chunk = 0;
 
3227
        chunk |= int(uchar(d->data[i++])) << 16;
 
3228
        if (i == d->size) {
 
3229
            padlen = 2;
 
3230
        } else {
 
3231
            chunk |= int(uchar(d->data[i++])) << 8;
 
3232
            if (i == d->size) padlen = 1;
 
3233
            else chunk |= int(uchar(d->data[i++]));
 
3234
        }
 
3235
 
 
3236
        int j = (chunk & 0x00fc0000) >> 18;
 
3237
        int k = (chunk & 0x0003f000) >> 12;
 
3238
        int l = (chunk & 0x00000fc0) >> 6;
 
3239
        int m = (chunk & 0x0000003f);
 
3240
        *out++ = alphabet[j];
 
3241
        *out++ = alphabet[k];
 
3242
        if (padlen > 1) *out++ = padchar;
 
3243
        else *out++ = alphabet[l];
 
3244
        if (padlen > 0) *out++ = padchar;
 
3245
        else *out++ = alphabet[m];
 
3246
    }
 
3247
 
 
3248
    tmp.truncate(out - tmp.data());
 
3249
    return tmp;
 
3250
}
 
3251
 
 
3252
/*! \fn QByteArray &QByteArray::setNum(int n, int base)
 
3253
 
 
3254
    Sets the byte array to the printed value of \a n in base \a base (10
 
3255
    by default) and returns a reference to the byte array. The \a base can
 
3256
    be any value between 2 and 36.
 
3257
 
 
3258
    Example:
 
3259
    \code
 
3260
        QByteArray ba;
 
3261
        int n = 63;
 
3262
        ba.setNum(n);           // ba == "63"
 
3263
        ba.setNum(n, 16);       // ba == "3f"
 
3264
    \endcode
 
3265
 
 
3266
    \sa number(), toInt()
 
3267
*/
 
3268
 
 
3269
/*! \fn QByteArray &QByteArray::setNum(uint n, int base)
 
3270
    \overload
 
3271
 
 
3272
    \sa toUInt()
 
3273
*/
 
3274
 
 
3275
/*! \fn QByteArray &QByteArray::setNum(short n, int base)
 
3276
    \overload
 
3277
 
 
3278
    \sa toShort()
 
3279
*/
 
3280
 
 
3281
/*! \fn QByteArray &QByteArray::setNum(ushort n, int base)
 
3282
    \overload
 
3283
 
 
3284
    \sa toUShort()
 
3285
*/
 
3286
 
 
3287
/*!
 
3288
    \overload
 
3289
 
 
3290
    \sa toLongLong()
 
3291
*/
 
3292
 
 
3293
QByteArray &QByteArray::setNum(qlonglong n, int base)
 
3294
{
 
3295
#if defined(QT_CHECK_RANGE)
 
3296
    if (base < 2 || base > 36) {
 
3297
        qWarning("QByteArray::setNum: Invalid base %d", base);
 
3298
        base = 10;
 
3299
    }
 
3300
#endif
 
3301
    QLocale locale(QLocale::C);
 
3302
    *this = locale.d->longLongToString(n, -1, base).toLatin1();
 
3303
    return *this;
 
3304
}
 
3305
 
 
3306
/*!
 
3307
    \overload
 
3308
 
 
3309
    \sa toULongLong()
 
3310
*/
 
3311
 
 
3312
QByteArray &QByteArray::setNum(qulonglong n, int base)
 
3313
{
 
3314
#if defined(QT_CHECK_RANGE)
 
3315
    if (base < 2 || base > 36) {
 
3316
        qWarning("QByteArray::setNum: Invalid base %d", base);
 
3317
        base = 10;
 
3318
    }
 
3319
#endif
 
3320
    QLocale locale(QLocale::C);
 
3321
    *this = locale.d->unsLongLongToString(n, -1, base).toLatin1();
 
3322
    return *this;
 
3323
}
 
3324
 
 
3325
/*! \overload
 
3326
 
 
3327
    Sets the byte array to the printed value of \a n, formatted in format
 
3328
    \a f with precision \a prec, and returns a reference to the
 
3329
    byte array.
 
3330
 
 
3331
    The format \a f can be any of the following:
 
3332
 
 
3333
    \table
 
3334
    \header \i Format \i Meaning
 
3335
    \row \i \c e \i format as [-]9.9e[+|-]999
 
3336
    \row \i \c E \i format as [-]9.9E[+|-]999
 
3337
    \row \i \c f \i format as [-]9.9
 
3338
    \row \i \c g \i use \c e or \c f format, whichever is the most concise
 
3339
    \row \i \c G \i use \c E or \c f format, whichever is the most concise
 
3340
    \endtable
 
3341
 
 
3342
    With 'e', 'E', and 'f', \a prec is the number of digits after the
 
3343
    decimal point. With 'g' and 'G', \a prec is the maximum number of
 
3344
    significant digits (trailing zeroes are omitted).
 
3345
 
 
3346
    \sa toDouble()
 
3347
*/
 
3348
 
 
3349
QByteArray &QByteArray::setNum(double n, char f, int prec)
 
3350
{
 
3351
    QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
 
3352
    uint flags = 0;
 
3353
 
 
3354
    if (qIsUpper(f))
 
3355
        flags = QLocalePrivate::CapitalEorX;
 
3356
    f = qToLower(f);
 
3357
 
 
3358
    switch (f) {
 
3359
        case 'f':
 
3360
            form = QLocalePrivate::DFDecimal;
 
3361
            break;
 
3362
        case 'e':
 
3363
            form = QLocalePrivate::DFExponent;
 
3364
            break;
 
3365
        case 'g':
 
3366
            form = QLocalePrivate::DFSignificantDigits;
 
3367
            break;
 
3368
        default:
 
3369
#if defined(QT_CHECK_RANGE)
 
3370
            qWarning("QByteArray::setNum: Invalid format char '%c'", f);
 
3371
#endif
 
3372
            break;
 
3373
    }
 
3374
 
 
3375
    QLocale locale(QLocale::C);
 
3376
    *this = locale.d->doubleToString(n, prec, form, -1, flags).toLatin1();
 
3377
    return *this;
 
3378
}
 
3379
 
 
3380
/*! \fn QByteArray &QByteArray::setNum(float n, char f, int prec)
 
3381
    \overload
 
3382
 
 
3383
    Sets the byte array to the printed value of \a n, formatted in format
 
3384
    \a f with precision \a prec, and returns a reference to the
 
3385
    byte array.
 
3386
 
 
3387
    \sa toFloat()
 
3388
*/
 
3389
 
 
3390
/*!
 
3391
    Returns a byte array containing the string equivalent of the
 
3392
    number \a n to base \a base (10 by default). The \a base can be
 
3393
    any value between 2 and 36.
 
3394
 
 
3395
    Example:
 
3396
    \code
 
3397
        int n = 63;
 
3398
        QByteArray::number(n);              // returns "63"
 
3399
        QByteArray::number(n, 16);          // returns "3f"
 
3400
        QByteArray::number(n, 16).upper();  // returns "3F"
 
3401
    \endcode
 
3402
 
 
3403
    \sa setNum(), toInt()
 
3404
*/
 
3405
QByteArray QByteArray::number(int n, int base)
 
3406
{
 
3407
    QByteArray s;
 
3408
    s.setNum(n, base);
 
3409
    return s;
 
3410
}
 
3411
 
 
3412
/*!
 
3413
    \overload
 
3414
 
 
3415
    \sa toUInt()
 
3416
*/
 
3417
QByteArray QByteArray::number(uint n, int base)
 
3418
{
 
3419
    QByteArray s;
 
3420
    s.setNum(n, base);
 
3421
    return s;
 
3422
}
 
3423
 
 
3424
/*!
 
3425
    \overload
 
3426
 
 
3427
    \sa toLongLong()
 
3428
*/
 
3429
QByteArray QByteArray::number(qlonglong n, int base)
 
3430
{
 
3431
    QByteArray s;
 
3432
    s.setNum(n, base);
 
3433
    return s;
 
3434
}
 
3435
 
 
3436
/*!
 
3437
    \overload
 
3438
 
 
3439
    \sa toULongLong()
 
3440
*/
 
3441
QByteArray QByteArray::number(qulonglong n, int base)
 
3442
{
 
3443
    QByteArray s;
 
3444
    s.setNum(n, base);
 
3445
    return s;
 
3446
}
 
3447
 
 
3448
/*! \overload
 
3449
 
 
3450
    Returns a byte array that contains the printed value of \a n,
 
3451
    formatted in format \a f with precision \a prec.
 
3452
 
 
3453
    Argument \a n is formatted according to the \a f format specified,
 
3454
    which is \c g by default, and can be any of the following:
 
3455
 
 
3456
    \table
 
3457
    \header \i Format \i Meaning
 
3458
    \row \i \c e \i format as [-]9.9e[+|-]999
 
3459
    \row \i \c E \i format as [-]9.9E[+|-]999
 
3460
    \row \i \c f \i format as [-]9.9
 
3461
    \row \i \c g \i use \c e or \c f format, whichever is the most concise
 
3462
    \row \i \c G \i use \c E or \c f format, whichever is the most concise
 
3463
    \endtable
 
3464
 
 
3465
    With 'e', 'E', and 'f', \a prec is the number of digits after the
 
3466
    decimal point. With 'g' and 'G', \a prec is the maximum number of
 
3467
    significant digits (trailing zeroes are omitted).
 
3468
 
 
3469
    \code
 
3470
        QByteArray ba = QByteArray::number(12.3456, 'E', 3);
 
3471
        // ba == 1.235E+01
 
3472
    \endcode
 
3473
 
 
3474
    \sa toDouble()
 
3475
*/
 
3476
QByteArray QByteArray::number(double n, char f, int prec)
 
3477
{
 
3478
    QByteArray s;
 
3479
    s.setNum(n, f, prec);
 
3480
    return s;
 
3481
}
 
3482
 
 
3483
/*!
 
3484
    Constructs a QByteArray that uses the first \a size characters in
 
3485
    the array \a data. The bytes in \a data are \e not copied. The
 
3486
    caller must be able to guarantee that \a data will not be deleted
 
3487
    or modified as long as the QByteArray (or an unmodified copy of
 
3488
    it) exists.
 
3489
 
 
3490
    Any attempts to modify the QByteArray or copies of it will cause
 
3491
    it to create a deep copy of the data, ensuring that the raw data
 
3492
    isn't modified.
 
3493
 
 
3494
    Here's an example of how we can read data using a QDataStream on
 
3495
    raw data in memory without requiring to copy the data into a
 
3496
    QByteArray:
 
3497
 
 
3498
    \code
 
3499
         static const char mydata[] = {
 
3500
            0x00, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76,
 
3501
            0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99,
 
3502
            ...
 
3503
            0x6d, 0x5b
 
3504
        };
 
3505
 
 
3506
        QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
 
3507
        QDataStream in(&data, QIODevice::ReadOnly);
 
3508
        ...
 
3509
    \endcode
 
3510
 
 
3511
    \warning A byte array created with fromRawData() is \e not
 
3512
    null-terminated, unless the raw data contains a 0 character at
 
3513
    position \a size. While that does not matter for QDataStream or
 
3514
    functions like indexOf(), passing the byte array to a function
 
3515
    that accepts a \c{const char *} and expects it to be
 
3516
    '\\0'-terminated leads into trouble.
 
3517
 
 
3518
    \sa data(), constData()
 
3519
*/
 
3520
 
 
3521
QByteArray QByteArray::fromRawData(const char *data, int size)
 
3522
{
 
3523
    Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
 
3524
    if (data) {
 
3525
        x->data = const_cast<char *>(data);
 
3526
    } else {
 
3527
        x->data = x->array;
 
3528
        size = 0;
 
3529
    }
 
3530
    x->ref.init(1);
 
3531
    x->alloc = x->size = size;
 
3532
    *x->array = '\0';
 
3533
    return QByteArray(x, 0, 0);
 
3534
}
 
3535
 
 
3536
/*!
 
3537
    Returns a decoded copy of the Base64 array \a base64. For example:
 
3538
 
 
3539
    \code
 
3540
        QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXRcIQo=");
 
3541
        text.data();            // returns "Qt is great!"
 
3542
    \endcode
 
3543
 
 
3544
    \sa toBase64()
 
3545
*/
 
3546
QByteArray QByteArray::fromBase64(const QByteArray &base64)
 
3547
{
 
3548
    unsigned int buf = 0;
 
3549
    int nbits = 0;
 
3550
    QByteArray tmp;
 
3551
    tmp.resize((base64.size() * 3) / 4);
 
3552
 
 
3553
    int offset = 0;
 
3554
    for (int i = 0; i < base64.size(); ++i) {
 
3555
        int ch = base64.at(i);
 
3556
        int d;
 
3557
 
 
3558
        if (ch >= 'A' && ch <= 'Z')
 
3559
            d = ch - 'A';
 
3560
        else if (ch >= 'a' && ch <= 'z')
 
3561
            d = ch - 'a' + 26;
 
3562
        else if (ch >= '0' && ch <= '9')
 
3563
            d = ch - '0' + 52;
 
3564
        else if (ch == '+')
 
3565
            d = 62;
 
3566
        else if (ch == '/')
 
3567
            d = 63;
 
3568
        else
 
3569
            d = -1;
 
3570
 
 
3571
        if (d != -1) {
 
3572
            buf = (buf << 6) | d;
 
3573
            nbits += 6;
 
3574
            if (nbits >= 8) {
 
3575
                nbits -= 8;
 
3576
                tmp[offset++] = buf >> nbits;
 
3577
                buf &= (1 << nbits) - 1;
 
3578
            }
 
3579
        }
 
3580
    }
 
3581
 
 
3582
    tmp.truncate(offset);
 
3583
    return tmp;
 
3584
}
 
3585
 
 
3586
/*! \typedef QByteArray::ConstIterator
 
3587
    \internal
 
3588
*/
 
3589
 
 
3590
/*! \typedef QByteArray::Iterator
 
3591
    \internal
 
3592
*/
 
3593
 
 
3594
/*! \typedef QByteArray::const_iterator
 
3595
    \internal
 
3596
*/
 
3597
 
 
3598
/*! \typedef QByteArray::iterator
 
3599
    \internal
 
3600
*/
 
3601
 
 
3602
/*!
 
3603
    \fn QByteArray::QByteArray(int size)
 
3604
 
 
3605
    Use QByteArray(int, char) instead.
 
3606
*/
 
3607
 
 
3608
 
 
3609
/*!
 
3610
    \fn QByteArray QByteArray::leftJustify(uint width, char fill, bool truncate) const
 
3611
 
 
3612
    Use leftJustified() instead.
 
3613
*/
 
3614
 
 
3615
/*!
 
3616
    \fn QByteArray QByteArray::rightJustify(uint width, char fill, bool truncate) const
 
3617
 
 
3618
    Use rightJustified() instead.
 
3619
*/
 
3620
 
 
3621
/*!
 
3622
    \fn QByteArray& QByteArray::duplicate(const QByteArray& a)
 
3623
 
 
3624
    Use simple assignment instead. (QByteArray uses implicit sharing
 
3625
    so if you modify a copy, only the copy is changed.)
 
3626
*/
 
3627
 
 
3628
/*!
 
3629
    \fn QByteArray& QByteArray::duplicate(const char *a, uint n)
 
3630
 
 
3631
    Use simple assignment instead. (QByteArray uses implicit sharing
 
3632
    so if you modify a copy, only the copy is changed.)
 
3633
*/
 
3634
 
 
3635
/*!
 
3636
    \fn QByteArray& QByteArray::setRawData(const char *a, uint n)
 
3637
 
 
3638
    Use fromRawData() instead.
 
3639
*/
 
3640
 
 
3641
/*!
 
3642
    \fn void QByteArray::resetRawData(const char *data, uint n)
 
3643
 
 
3644
    Use clear() instead.
 
3645
*/
 
3646
 
 
3647
/*!
 
3648
    \fn QByteArray QByteArray::lower() const
 
3649
 
 
3650
    Use toLower() instead.
 
3651
*/
 
3652
 
 
3653
/*!
 
3654
    \fn QByteArray QByteArray::upper() const
 
3655
 
 
3656
    Use toUpper() instead.
 
3657
*/
 
3658
 
 
3659
/*!
 
3660
    \fn QByteArray QByteArray::stripWhiteSpace() const
 
3661
 
 
3662
    Use trimmed() instead.
 
3663
*/
 
3664
 
 
3665
/*!
 
3666
    \fn QByteArray QByteArray::simplifyWhiteSpace() const
 
3667
 
 
3668
    Use simplified() instead.
 
3669
*/
 
3670
 
 
3671
/*!
 
3672
    \fn int QByteArray::find(char c, int from = 0) const
 
3673
 
 
3674
    Use indexOf() instead.
 
3675
*/
 
3676
 
 
3677
/*!
 
3678
    \fn int QByteArray::find(const char *c, int from = 0) const
 
3679
 
 
3680
    Use indexOf() instead.
 
3681
*/
 
3682
 
 
3683
/*!
 
3684
    \fn int QByteArray::find(const QByteArray &ba, int from = 0) const
 
3685
 
 
3686
    Use indexOf() instead.
 
3687
*/
 
3688
 
 
3689
/*!
 
3690
    \fn int QByteArray::findRev(char c, int from = -1) const
 
3691
 
 
3692
    Use lastIndexOf() instead.
 
3693
*/
 
3694
 
 
3695
/*!
 
3696
    \fn int QByteArray::findRev(const char *c, int from = -1) const
 
3697
 
 
3698
    Use lastIndexOf() instead.
 
3699
*/
 
3700
 
 
3701
/*!
 
3702
    \fn int QByteArray::findRev(const QByteArray &ba, int from = -1) const
 
3703
 
 
3704
    Use lastIndexOf() instead.
 
3705
*/
 
3706
 
 
3707
/*!
 
3708
    \fn int QByteArray::find(const QString &s, int from = 0) const
 
3709
 
 
3710
    Use indexOf() instead.
 
3711
*/
 
3712
 
 
3713
/*!
 
3714
    \fn int QByteArray::findRev(const QString &s, int from = -1) const
 
3715
 
 
3716
    Use lastIndexOf() instead.
 
3717
*/
 
3718