📄 qstring.cpp
字号:
#endif qFree(d);}/*! Sets the size of the string to \a size characters. If \a size is greater than the current size, the string is extended to make it \a size characters long with the extra characters added to the end. The new characters are uninitialized. If \a size is less than the current size, characters are removed from the end. Example: \quotefromfile snippets/qstring/main.cpp \skipto Widget::resizeFunction() \skipto QString s \printuntil // s == "Hello???" If you want to append a certain number of identical characters to the string, use \l operator+=() as follows rather than resize(): \quotefromfile snippets/qstring/main.cpp \skipto Widget::resizeFunction() \skipto QString t \printuntil // t == "HelloXXXXXXXXXX" If you want to expand the string so that it reaches a certain width and fill the new positions with a particular character, use the leftJustified() function: \quotefromfile snippets/qstring/main.cpp \skipto Widget::resizeFunction() \skipto QString r \printuntil // r == "Hello " \sa truncate(), reserve()*/void QString::resize(int size){ if (size <= 0 && !d->capacity) { Data *x = &shared_empty; x->ref.ref(); x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) free(x); } else { if (d->ref != 1 || size > d->alloc || (!d->capacity && size < d->size && size < d->alloc >> 1)) realloc(grow(size)); if (d->alloc >= size) { d->size = size; if (d->data == d->array) { d->array[size] = '\0'; } } }}/*! \fn int QString::capacity() const Returns the maximum number of characters that can be stored in the string without forcing a reallocation. The sole purpose of this function is to provide a means of fine tuning QString's memory usage. In general, you will rarely ever need to call this function. If you want to know how many characters are in the string, call size(). \sa reserve(), squeeze()*//*! \fn void QString::reserve(int size) Attempts to allocate memory for at least \a size characters. If you know in advance how large the string will be, you can call this function, and if you resize the string often you are likely to get better performance. If \a size is an underestimate, the worst that will happen is that the QString will be a bit slower. The sole purpose of this function is to provide a means of fine tuning QString's memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the string, call resize(). This function is useful for code that needs to build up a long string and wants to avoid repeated reallocation. In this example, we want to add to the string until some condition is true, and we're fairly sure that size is large enough to make a call to reserve() worthwhile: \quotefromfile snippets/qstring/main.cpp \skipto Widget::reserveFunction() \skipto QString result \printuntil result.squeeze() \sa squeeze(), capacity()*//*! \fn void QString::squeeze() Releases any memory not required to store the character data. The sole purpose of this function is to provide a means of fine tuning QString's memory usage. In general, you will rarely ever need to call this function. \sa reserve(), capacity()*/void QString::realloc(int alloc){ if (d->ref != 1 || d->data != d->array) { Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc * sizeof(QChar))); if (!x) return; x->size = qMin(alloc, d->size); ::memcpy(x->array, d->data, x->size * sizeof(QChar)); x->array[x->size] = 0; x->asciiCache = 0; x->ref.init(1); x->alloc = alloc; x->clean = d->clean; x->simpletext = d->simpletext; x->righttoleft = d->righttoleft; x->capacity = d->capacity; x->data = x->array; x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) free(x); } else {#ifdef QT3_SUPPORT if (d->asciiCache) { Q_ASSERT(asciiCache); asciiCache->remove(d); }#endif Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc * sizeof(QChar))); if (!x) return; x->alloc = alloc; x->data = x->array; d = x; }}void QString::realloc(){ realloc(d->size);}void QString::expand(int i){ int sz = d->size; resize(qMax(i + 1, sz)); if (d->size - 1 > sz) { ushort *n = d->data + d->size - 1; ushort *e = d->data + sz; while (n != e) * --n = ' '; }}/*! \fn void QString::clear() Clears the contents of the string and makes it empty. \sa resize(), isEmpty()*//*! \fn QString &QString::operator=(const QString &other) Assigns \a other to this string and returns a reference to this string.*/QString &QString::operator=(const QString &other){ Data *x = other.d; x->ref.ref(); x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) free(x); return *this;}/*! \fn QString &QString::operator=(const QLatin1String &str) \overload Assigns the Latin-1 string \a str to this string.*//*! \fn QString &QString::operator=(const QByteArray &ba) \overload Assigns \a ba to this string. The byte array is converted to Unicode using the fromAscii() function. You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn QString &QString::operator=(const char *str) \overload Assigns \a str to this string. The const char pointer is converted to Unicode using the fromAscii() function. You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn QString &QString::operator=(char ch) \overload Assigns character \a ch to this string. The character is converted to Unicode using the fromAscii() function. You can disable this operator by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \overload Sets the string to contain the single character \a ch.*/QString &QString::operator=(QChar ch){ return operator=(QString(ch));}/*! \fn QString& QString::insert(int position, const QString &str) Inserts the string \a str at the given index \a position and returns a reference to this string. Example: \quotefromfile snippets/qstring/main.cpp \skipto Widget::insertFunction() \skipto QString str \printuntil // str == "Montreal" If the given \a position is greater than size(), the array is first extended using resize(). \sa append(), prepend(), replace(), remove()*//*! \fn QString &QString::insert(int position, const QLatin1String &str) \overload Inserts the Latin-1 string \a str at the given index \a position.*/QString &QString::insert(int i, const QLatin1String &str){ const uchar *s = (const uchar *)str.latin1(); if (i < 0 || !s || !(*s)) return *this; int len = qstrlen(str.latin1()); expand(qMax(d->size, i) + len - 1); ::memmove(d->data + i + len, d->data + i, (d->size - i - len) * sizeof(QChar)); for (int j = 0; j < len; ++j) d->data[i + j] = s[j]; return *this;}/*! \fn QString& QString::insert(int position, const QChar *unicode, int size) \overload Inserts the first \a size characters of the QChar array \a unicode at the given index \a position in the string.*/QString& QString::insert(int i, const QChar *unicode, int size){ if (i < 0 || size <= 0) return *this; const ushort *s = (const ushort *)unicode; if (s >= d->data && s < d->data + d->alloc) { // Part of me - take a copy ushort *tmp = static_cast<ushort *>(malloc(size * sizeof(QChar))); memcpy(tmp, s, size * sizeof(QChar)); insert(i, reinterpret_cast<const QChar *>(tmp), size); ::free(tmp); return *this; } expand(qMax(d->size, i) + size - 1); ::memmove(d->data + i + size, d->data + i, (d->size - i - size) * sizeof(QChar)); memcpy(d->data + i, s, size * sizeof(QChar)); return *this;}/*! \fn QString& QString::insert(int position, QChar ch) \overload Inserts \a ch at the given index \a position in the string.*/QString& QString::insert(int i, QChar ch){ if (i < 0) i += d->size; if (i < 0) return *this; expand(qMax(i, d->size)); ::memmove(d->data + i + 1, d->data + i, (d->size - i) * sizeof(QChar)); d->data[i] = ch.unicode(); return *this;}/*! Appends the string \a str onto the end of this string. Example: \quotefromfile snippets/qstring/main.cpp \skipto Widget::appendFunction() \skipto QString \printuntil // This is the same as using the insert() function: \skipto insert \printline insert The append() function is typically very fast (\l{constant time}), because QString preallocates extra space at the end of the string data so it can grow without reallocating the entire string each time. \sa operator+=(), prepend(), insert()*/QString &QString::append(const QString &str){ if (str.d != &shared_null) { if (d == &shared_null) { operator=(str); } else { if (d->ref != 1 || d->size + str.d->size > d->alloc) realloc(grow(d->size + str.d->size)); memcpy(d->data + d->size, str.d->data, str.d->size * sizeof(QChar)); d->size += str.d->size; d->data[d->size] = '\0'; } } return *this;}/*! \overload Appends the Latin-1 string \a str to this string.*/QString &QString::append(const QLatin1String &str){ const uchar *s = (const uchar *)str.latin1(); if (s) { int len = qstrlen((char *)s); if (d->ref != 1 || d->size + len > d->alloc) realloc(grow(d->size + len)); ushort *i = d->data + d->size; while ((*i++ = *s++)) ; d->size += len; } return *this;}/*! \fn QString &QString::append(const QByteArray &ba) \overload Appends the byte array \a ba to this string. The given byte array is converted to Unicode using the fromAscii() function. You can disable this function by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \fn QString &QString::append(const char *str) \overload Appends the string \a str to this string. The given const char pointer is converted to Unicode using the fromAscii() function. You can disable this function by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.*//*! \overload Appends the character \a ch to this string.*/QString &QString::append(QChar ch){ if (d->ref != 1 || d->size + 1 > d->alloc) realloc(grow(d->size + 1)); d->data[d->size++] = ch.unicode(); d->data[d->size] = '\0'; return *this;}/*! \fn QString &QString::prepend(const QString &str) Prepends the string \a str to the beginning of this string and returns a reference to this string. Example: \quotefromfile snippets/qstring/main.cpp \skipto Widget::prependFunction() \skipto QString x \printuntil // x == "airship" \sa append(), insert()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -