📄 qstring.cpp
字号:
*//*! \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. The 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::prepend(const char *str) \overload Prepends the string \a str to this string. The 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.*//*! \fn QString &QString::prepend(QChar ch) \overload Prepends the character \a ch to this string.*//*! \fn QString &QString::remove(int position, int n) Removes \a n characters from the string, starting at the given \a position index, and returns a reference to the string. If the specified \a position index is within the string, but \a position + \a n is beyond the end of the string, the string is truncated at the specified \a position. \quotefromfile snippets/qstring/main.cpp \skipto Widget::removeFunction() \skipto QString s \printuntil // s == "Meal" \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 the given \a str string 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. This is the same as \c replace(str, "", cs). \sa replace()*/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: \quotefromfile snippets/qstring/main.cpp \skipto Widget::removeFunction() \skipto QString t \printuntil // t == "li Bb" This is the same as \c replace(ch, "", cs). \sa replace()*/QString &QString::remove(QChar ch, Qt::CaseSensitivity cs){ int i = 0; ushort c = ch.unicode(); if (cs == Qt::CaseSensitive) { while (i < d->size) if (d->data[i] == ch) remove(i, 1); else i++; } else { c = foldCase(c); while (i < d->size) if (foldCase(d->data[i]) == c) remove(i, 1); else i++; } return *this;}/*! \fn QString &QString::remove(const QRegExp &rx) \overload Removes every occurrence of the regular expression \a rx in the string, and returns a reference to the string. For example: \quotefromfile snippets/qstring/main.cpp \skipto Widget::removeFunction() \skipto QString r \printuntil // r == "The" \sa indexOf(), lastIndexOf(), replace()*//*! \fn QString &QString::replace(int position, int n, const QString &after) Replaces \a n characters from the specified index \a position with the string \a after, and returns a reference to this string. Example: \quotefromfile snippets/qstring/main.cpp \skipto Widget::replaceFunction() \skipto QString x \printuntil // x == "Say no!" \sa insert(), remove()*/QString &QString::replace(int pos, int len, const QString &after){ QString copy = after; remove(pos, len); return insert(pos, copy.constData(), copy.d->size);}/*! \fn QString &QString::replace(int position, int n, const QChar *unicode, int size) \overload Replaces \a n characters from the specified index \a position with the first \a size characters of the QChar array \a unicode.*/QString &QString::replace(int pos, int len, const QChar *unicode, int size){ remove(pos, len); return insert(pos, unicode, size);}/*! \fn QString &QString::replace(int position, int n, QChar after) \overload Replaces \a n characters from the specified index \a position with the character \a after.*/QString &QString::replace(int pos, int len, QChar after){ remove(pos, len); return insert(pos, after);}/*! \overload Replaces every occurrence of the string \a before with the string \a after. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive. Example: \quotefromfile snippets/qstring/main.cpp \skipto Widget::replaceFunction() \skipto QString str \printuntil // str == "color behavior flavor neighbor"*/QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs){ if (d->size == 0) { if (before.d->size) return *this; } else { if (cs == Qt::CaseSensitive && before == after) return *this; } if (d->ref != 1) realloc(d->size); QStringMatcher matcher(before, cs); int index = 0; const int bl = before.d->size; const int al = after.d->size; if (bl == al) { if (bl) { const QChar *auc = (const QChar*) after.d->data; while ((index = matcher.indexIn(*this, index)) != -1) { // we need memmove(), not memcpy(), in the rare case where // this == &after, before == after, and cs is Qt::CaseInsensitive memmove(d->data + index, auc, al * sizeof(QChar)); index += bl; } } } else if (al < bl) { const QChar *auc = after.unicode(); uint to = 0; uint movestart = 0; uint num = 0; while ((index = matcher.indexIn(*this, index)) != -1) { if (num) { int msize = index - movestart; if (msize > 0) { memmove(d->data + to, d->data + movestart, msize * sizeof(QChar)); to += msize; } } else { to = index; } if (al) { memcpy(d->data+to, auc, al*sizeof(QChar)); to += al; } index += bl; movestart = index; num++; } if (num) { int msize = d->size - movestart; if (msize > 0) memmove(d->data + to, d->data + movestart, msize * sizeof(QChar)); resize(d->size - num*(bl-al)); } } else { const QString copy = after; // the most complex case. We don't want to lose performance by doing repeated // copies and reallocs of the string. while (index != -1) { uint indices[4096]; uint pos = 0; while (pos < 4095) { index = matcher.indexIn(*this, index); if (index == -1) break; indices[pos++] = index; index += bl; // avoid infinite loop if (!bl) index++; } if (!pos) break; // we have a table of replacement positions, use them for fast replacing int adjust = pos*(al-bl); // index has to be adjusted in case we get back into the loop above. if (index != -1) index += adjust; int newLen = d->size + adjust; int moveend = d->size; if (newLen > d->size) resize(newLen); while (pos) { pos--; int movestart = indices[pos] + bl; int insertstart = indices[pos] + pos*(al-bl); int moveto = insertstart + al; memmove(d->data + moveto, d->data + movestart, (moveend - movestart)*sizeof(QChar)); memcpy(d->data + insertstart, copy.unicode(), al*sizeof(QChar)); moveend = movestart-bl; } } } return *this;}/*! \overload Replaces every occurrence of the character \a ch in the string with \a after. Returns a reference to the string. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive.*/QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs){ return replace(QString(ch), after, cs);}/*! \overload Replaces every occurrence of the character \a before with the character \a after. Returns a reference to the string. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive.*/QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs){ ushort a = after.unicode(); ushort b = before.unicode(); if (d->size) { detach(); ushort *i = d->data; const ushort *e = i + d->size; if (cs == Qt::CaseSensitive) { for (; i != e; ++i) if (*i == b) *i = a; } else { b = foldCase(b); for (; i != e; ++i) if (foldCase(*i) == b) *i = a; } } return *this;}/*! Returns true if string \a other is equal to this string; otherwise returns false. The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with localeAwareCompare().*/bool QString::operator==(const QString &other) const{ return (size() == other.size()) && (memcmp((char*)unicode(),(char*)other.unicode(), size()*sizeof(QChar))==0);}/*! \overload*/bool QString::operator==(const QLatin1String &other) const{ const ushort *uc = d->data; const ushort *e = uc + d->size; const uchar *c = (uchar *)other.latin1(); if (!c) return isEmpty(); while (*c) { if (uc == e || *uc != *c) return false; ++uc; ++c; } return (uc == e);}/*! \fn bool QString::operator==(const QByteArray &other) const \overload The \a other byte array is converted to a QString 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 bool QString::operator==(const char *other) const \overload The \a other const char pointer is converted to a QString 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.*//*! Returns true if this string is lexically less than string \a other; otherwise returns false. The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings using the QString::localeAwareCompare() function.*/bool QString::operator<(const QString &other) const{ return ucstrcmp(*this, other) < 0;}/*! \overload*/bool QString::operator<(const QLatin1String &other) const{ const ushort *uc = d->data; const ushort *e = uc + d->size;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -