⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qbytearray.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    QByteArray baunzip;    int res;    do {        baunzip.resize(len);        res = ::uncompress((uchar*)baunzip.data(), &len,                            (uchar*)data+4, nbytes-4);        switch (res) {        case Z_OK:            if ((int)len != baunzip.size())                baunzip.resize(len);            break;        case Z_MEM_ERROR:            qWarning("qUncompress: Z_MEM_ERROR: Not enough memory");            break;        case Z_BUF_ERROR:            len *= 2;            break;        case Z_DATA_ERROR:            qWarning("qUncompress: Z_DATA_ERROR: Input data is corrupted");            break;        }    } while (res == Z_BUF_ERROR);    if (res != Z_OK)        baunzip = QByteArray();    return baunzip;}#endifstatic inline bool qIsUpper(char c){    return c >= 'A' && c <= 'Z';}static inline char qToLower(char c){    if (c >= 'A' && c <= 'Z')        return c - 'A' + 'a';    else        return c;}Q_CORE_EXPORT QByteArray::Data QByteArray::shared_null = {Q_ATOMIC_INIT(1), 0, 0, shared_null.array, {0} };QByteArray::Data QByteArray::shared_empty = { Q_ATOMIC_INIT(1), 0, 0, shared_empty.array, {0} };/*!    \class QByteArray    \brief The QByteArray class provides an array of bytes.    \ingroup tools    \ingroup shared    \ingroup text    \mainclass    \reentrant    QByteArray can be used to store both raw bytes (including '\\0's)    and traditional 8-bit '\\0'-terminated strings. Using QByteArray    is much more convenient than using \c{const char *}. Behind the    scenes, it always ensures that the data is followed by a '\\0'    terminator, and uses \l{implicit sharing} (copy-on-write) to    reduce memory usage and avoid needless copying of data.    In addition to QByteArray, Qt also provides the QString class to    store string data. For most purposes, QString is the class you    want to use. It stores 16-bit Unicode characters, making it easy    to store non-ASCII/non-Latin-1 characters in your application.    Furthermore, QString is used throughout in the Qt API. The two    main cases where QByteArray is appropriate are when you need to    store raw binary data, and when memory conservation is critical    (e.g. with Qtopia Core).    One way to initialize a QByteArray is simply to pass a \c{const    char *} to its constructor. For example, the following code    creates a byte array of size 5 containing the data "Hello":    \code        QByteArray ba("Hello");    \endcode    Although the size() is 5, the byte array also maintains an extra    '\\0' character at the end so that if a function is used that    asks for a pointer to the underlying data (e.g. a call to    data()), the data pointed to is guaranteed to be    '\\0'-terminated.    QByteArray makes a deep copy of the \c{const char *} data, so you    can modify it later without experiencing side effects. (If for    performance reasons you don't want to take a deep copy of the    character data, use QByteArray::fromRawData() instead.)    Another approach is to set the size of the array using resize()    and to initialize the data byte per byte. QByteArray uses 0-based    indexes, just like C++ arrays. To access the byte at a particular    index position, you can use operator[](). On non-const byte    arrays, operator[]() returns a reference to a byte that can be    used on the left side of an assignment. For example:    \code        QByteArray ba;        ba.resize(5);        ba[0] = 0x3c;        ba[1] = 0xb8;        ba[2] = 0x64;        ba[3] = 0x18;        ba[4] = 0xca;    \endcode    For read-only access, an alternative syntax is to use at():    \code        for (int i = 0; i < ba.size(); ++i) {            if (ba.at(i) >= 'a' && ba.at(i) <= 'f')                cout << "Found character in range [a-f]" << endl;        }    \endcode    at() can be faster than operator[](), because it never causes a    \l{deep copy} to occur.    To extract many bytes at a time, use left(), right(), or mid().    A QByteArray can embed '\\0' bytes. The size() function always    returns the size of the whole array, including embedded '\\0'    bytes. If you want to obtain the length of the data up to and    excluding the first '\\0' character, call qstrlen() on the byte    array.    After a call to resize(), newly allocated bytes have undefined    values. To set all the bytes to a particular value, call fill().    To obtain a pointer to the actual character data, call data() or    constData(). These functions return a pointer to the beginning of    the data. The pointer is guaranteed to remain valid until a    non-const function is called on the QByteArray. It is also    guaranteed that the data ends with a '\\0' byte. This '\\0' byte    is automatically provided by QByteArray and is not counted in    size().    QByteArray provides the following basic functions for modifying    the byte data: append(), prepend(), insert(), replace(), and    remove(). For example:    \code        QByteArray x("and");        x.prepend("rock ");         // x == "rock and"        x.append(" roll");          // x == "rock and roll"        x.replace(5, 3, "&");       // x == "rock & roll"    \endcode    The replace() and remove() functions' first two arguments are the    position from which to start erasing and the number of bytes that    should be erased.    When you append() data to a non-empty array, the array will be    reallocated and the new data copied to it. You can avoid this    behavior by calling reserve(), which preallocates a certain amount    of memory. You can also call capacity() to find out how much    memory QByteArray actually allocated. Data appended to an empty    array is not copied.    A frequent requirement is to remove whitespace characters from a    byte array ('\\n', '\\t', ' ', etc.). If you want to remove    whitespace from both ends of a QByteArray, use trimmed(). If you    want to remove whitespace from both ends and replace multiple    consecutive whitespaces with a single space character within the    byte array, use simplified().    If you want to find all occurrences of a particular character or    substring in a QByteArray, use indexOf() or lastIndexOf(). The    former searches forward starting from a given index position, the    latter searches backward. Both return the index position of the    character or substring if they find it; otherwise, they return -1.    For example, here's a typical loop that finds all occurrences of a    particular substring:    \code        QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");        int j = 0;        while ((j = ba.indexOf("<b>", j)) != -1) {            cout << "Found <b> tag at index position " << j << endl;            ++j;        }    \endcode    If you simply want to check whether a QByteArray contains a    particular character or substring, use contains(). If you want to    find out how many times a particular character or substring    occurs in the byte array, use count(). If you want to replace all    occurrences of a particular value with another, use one of the    two-parameter replace() overloads.    QByteArrays can be compared using overloaded operators such as    operator<(), operator<=(), operator==(), operator>=(), and so on.    The comparison is based exclusively on the numeric values    of the characters and is very fast, but is not what a human would    expect. QString::localeAwareCompare() is a better choice for    sorting user-interface strings.    For historical reasons, QByteArray distinguishes between a null    byte array and an empty byte array. A \e null byte array is a    byte array that is initialized using QByteArray's default    constructor or by passing (const char *)0 to the constructor. An    \e empty byte array is any byte array with size 0. A null byte    array is always empty, but an empty byte array isn't necessarily    null:    \code        QByteArray().isNull();          // returns true        QByteArray().isEmpty();         // returns true        QByteArray("").isNull();        // returns false        QByteArray("").isEmpty();       // returns true        QByteArray("abc").isNull();     // returns false        QByteArray("abc").isEmpty();    // returns false    \endcode    All functions except isNull() treat null byte arrays the same as    empty byte arrays. For example, data() returns a pointer to a    '\\0' character for a null byte array (\e not a null pointer),    and QByteArray() compares equal to QByteArray(""). We recommend    that you always use isEmpty() and avoid isNull().    \section1 Note on 8-bit Character Comparisons    In QByteArray, the notion of uppercase and lowercase and of which    character is greater than or less than another character is    locale dependent. This affects functions that support a case    insensitive option or that compare or lowercase or uppercase    their arguments. Case insensitive operations and comparisons will    be accurate if both strings contain only ASCII characters. (If \c    $LC_CTYPE is set, most Unix systems do "the right thing".)    Functions that this affects include contains(), indexOf(),    lastIndexOf(), operator<(), operator<=(), operator>(),    operator>=(), toLower() and toUpper().    This issue does not apply to QStrings since they represent    characters using Unicode.    \sa QString, QBitArray*//*! \fn QByteArray::iterator QByteArray::begin()    \internal*//*! \fn QByteArray::const_iterator QByteArray::begin() const    \internal*//*! \fn QByteArray::const_iterator QByteArray::constBegin() const    \internal*//*! \fn QByteArray::iterator QByteArray::end()    \internal*//*! \fn QByteArray::const_iterator QByteArray::end() const    \internal*//*! \fn QByteArray::const_iterator QByteArray::constEnd() const    \internal*//*! \fn void QByteArray::push_back(const QByteArray &other)    This function is provided for STL compatibility. It is equivalent    to append(\a other).*//*! \fn void QByteArray::push_back(const char *str)    \overload    Same as append(\a str).*//*! \fn void QByteArray::push_back(char ch)    \overload    Same as append(\a ch).*//*! \fn void QByteArray::push_front(const QByteArray &other)    This function is provided for STL compatibility. It is equivalent    to prepend(\a other).*//*! \fn void QByteArray::push_front(const char *str)    \overload    Same as prepend(\a str).*//*! \fn void QByteArray::push_front(char ch)    \overload    Same as prepend(\a ch).*//*! \fn QByteArray::QByteArray(const QByteArray &other)    Constructs a copy of \a other.    This operation takes \l{constant time}, because QByteArray is    \l{implicitly shared}. This makes returning a QByteArray from a    function very fast. If a shared instance is modified, it will be    copied (copy-on-write), and that takes \l{linear time}.    \sa operator=()*//*! \fn QByteArray::~QByteArray()    Destroys the byte array.*//*!    Assigns \a other to this byte array and returns a reference to    this byte array.*/QByteArray &QByteArray::operator=(const QByteArray & other){    Data *x = other.d;    x->ref.ref();    x = qAtomicSetPtr(&d, x);    if (!x->ref.deref())        qFree(x);    return *this;}/*!    \overload    Assigns \a str to this byte array.*/QByteArray &QByteArray::operator=(const char *str){    Data *x;    if (!str) {        x = &shared_null;    } else if (!*str) {        x = &shared_empty;    } else {        int len = qstrlen(str);        if (d->ref != 1 || len > d->alloc || (len < d->size && len < d->alloc >> 1))            realloc(len);        x = d;        memcpy(x->data, str, len + 1); // include null terminator        x->size = len;    }    x->ref.ref();    x = qAtomicSetPtr(&d, x);    if (!x->ref.deref())         qFree(x);    return *this;}/*! \fn int QByteArray::size() const    Returns the number of bytes in this byte array.    The last byte in the byte array is at position size() - 1. In    addition, QByteArray ensures that the byte at position size() is    always '\\0', so that you can use the return value of data() and    constData() as arguments to functions that expect    '\\0'-terminated strings.    Example:    \code        QByteArray ba("Hello");        int n = ba.size();          // n == 5        ba.data()[0];               // returns 'H'        ba.data()[4];               // returns 'o'        ba.data()[5];               // returns '\0'    \endcode    \sa isEmpty(), resize()*//*! \fn bool QByteArray::isEmpty() const    Returns true if the byte array has size 0; otherwise returns false.    Example:    \code        QByteArray().isEmpty();         // returns true        QByteArray("").isEmpty();       // returns true        QByteArray("abc").isEmpty();    // returns false    \endcode    \sa size()*//*! \fn int QByteArray::capacity() const    Returns the maximum number of bytes that can be stored in the    byte array without forcing a reallocation.    The sole purpose of this function is to provide a means of fine    tuning QByteArray's memory usage. In general, you will rarely    ever need to call this function. If you want to know how many    bytes are in the byte array, call size().    \sa reserve(), squeeze()*//*! \fn void QByteArray::reserve(int size)    Attempts to allocate memory for at least \a size bytes. If you    know in advance how large the byte array will be, you can call    this function, and if you call resize() often you are likely to    get better performance. If \a size is an underestimate, the worst    that will happen is that the QByteArray will be a bit slower.    The sole purpose of this function is to provide a means of fine    tuning QByteArray's memory usage. In general, you will rarely    ever need to call this function. If you want to change the size    of the byte array, call resize().    \sa squeeze(), capacity()*//*! \fn void QByteArray::squeeze()    Releases any memory not required to store the array's data.    The sole purpose of this function is to provide a means of fine    tuning QByteArray's memory usage. In general, you will rarely    ever need to call this function.    \sa reserve(), capacity()*//*! \fn QByteArray::operator const char *() const    Returns a pointer to the data stored in the byte array. The    pointer can be used to access the bytes that compose the array.    The data is '\\0'-terminated. The pointer remains valid as long    as the array isn't reallocated.    This operator is mostly useful to pass a byte array to a function    that accepts a \c{const char *}.    Note: A QByteArray can store any byte values including '\\0's,    but most functions that take \c{char *} arguments assume that the    data ends at the first '\\0' they encounter.    \sa constData()*//*! \fn QByteArray::operator const void *() const    Returns a void pointer to the data.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -