📄 qstring.cpp
字号:
\endcode \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->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. \a ba is converted to Unicode using fromAscii(). 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 exaple.*//*! \fn QString &QString::operator=(const char *str) \overload Assigns \a str to this string. \a str is converted to Unicode using fromAscii(). 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 fromAscii(). 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 i, const QString &str) Inserts the string \a str at index position \a i and returns a reference to this string. Example: \code QString str = "Meal"; str.insert(1, QString("ontr")); // str == "Montreal" \endcode If \a i is greater than size(), the array is first extended using resize(). \sa append(), prepend(), replace(), remove()*//*! \overload Inserts the Latin-1 string \a str at index position \a i.*/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;}/*! \overload Inserts the first \a size characters of the QChar array \a unicode at index position \a i 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;}/*! \overload Inserts \a ch at index position \a i 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: \code QString x = "free"; QString y = "dom"; x.append(y); // x == "freedom" \endcode This is the same as insert(size(), \a str). This operation 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. \a ba is converted to Unicode using fromAscii(). 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. \a str is converted to Unicode using fromAscii(). 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: \code QString x = "ship"; QString y = "air"; x.prepend(y); // x == "airship" \endcode \sa append(), insert()*//*! \fn QString &QString::prepend(const QLatin1String &str) \overload Prepends the Latin-1 string \a str to this string.*//*! \fn QString &QString::prepend(const QByteArray &ba) \overload Prepends the byte array \a ba to this string. \a ba is converted to Unicode using fromAscii(). 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::prepend(const char *str) \overload Prepends the string \a str to this string. \a str is converted to Unicode using fromAscii(). 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::prepend(QChar ch) \overload Prepends the character \a ch to this string.*//*! Removes \a len characters from the string, starting at index position \a pos, and returns a reference to the string. If \a pos is within the string, but \a pos + \a len is beyond the end of the string, the string is truncated at position \a pos. \code QString str = "Montreal"; str.remove(1, 4); // str == "Meal" \endcode \sa insert(), replace()*/QString &QString::remove(int pos, int len){ if (pos < 0) pos += d->size; if (pos < 0 || pos >= d->size) { // range problems } else if (pos + len >= d->size) { // pos ok resize(pos); } else if (len > 0) { detach(); memmove(d->data + pos, d->data + pos + len, (d->size - pos - len + 1) * sizeof(ushort)); d->size -= len; } return *this;}/*! \overload Removes every occurrence of \a str in this string. Returns a reference to this string. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive. This is the same as replace(\a str, "", \a cs).*/QString &QString::remove(const QString &str, Qt::CaseSensitivity cs){ if (str.d->size) { int i = 0; while ((i = indexOf(str, i, cs)) != -1) remove(i, str.d->size); } return *this;}/*! \overload Removes every occurrence of the character \a ch in this string, and returns a reference to this string. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive. Example: \code QString str = "Ali Baba"; str.remove(QChar('a'), Qt::CaseInsensitive); // str == "li Bb" \endcode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -