📄 qstring.cpp
字号:
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 QString &other) const Returns true if this string is lexically greater than or equal to 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 with localeAwareCompare().*//*! \fn bool QString::operator>=(const QLatin1String &other) const \overload*//*! \fn bool QString::operator>=(const QByteArray &other) const \overload \a other is converted to a QString 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 bool QString::operator>=(const char *other) const \overload \a other is converted to a QString 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 bool QString::operator!=(const QString &other) const Returns true if this string is not equal to 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 with localeAwareCompare().*//*! \fn bool QString::operator!=(const QLatin1String &other) const \overload*//*! \fn bool QString::operator!=(const QByteArray &other) const \overload \a other is converted to a QString 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 bool QString::operator!=(const char *other) const \overload \a other is converted to a QString 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.*/#define REHASH(a) \ if (sl_minus_1 < (int)sizeof(int) * CHAR_BIT) \ hashHaystack -= (a) << sl_minus_1; \ hashHaystack <<= 1/*! Returns the index position of the first occurrence of the string \a str in this string, searching forward from index position \a from. Returns -1 if \a str is not found. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive. Example: \code QString x = "sticky question"; QString y = "sti"; x.indexOf(y); // returns 0 x.indexOf(y, 1); // returns 10 x.indexOf(y, 10); // returns 10 x.indexOf(y, 11); // returns -1 \endcode If \a from is -1, the search starts at the last character; if it is -2, at the next to last character and so on. \sa lastIndexOf(), contains(), count()*/int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const{ const int l = d->size; const int sl = str.d->size; if (from < 0) from += l; if (uint(sl + from) > (uint)l) return -1; if (!sl) return from; if (!l) return -1; if (sl == 1) return indexOf(*(const QChar *)str.d->data, from, cs); /* We use the Boyer-Moore algorithm in cases where the overhead for the skip table should pay off, otherwise we use a simple hash function. */ if (l > 500 && sl > 5) return QStringMatcher(str, cs).indexIn(*this, from); /* We use some hashing for efficiency's sake. Instead of comparing strings, we compare the hash value of str with that of a part of this QString. Only if that matches, we call ucstrncmp() or ucstrnicmp(). */ const QChar *needle = (const QChar*) str.d->data; const QChar *haystack = (const QChar*) d->data + from; const QChar *end = (const QChar*) d->data + (l-sl); const int sl_minus_1 = sl-1; int hashNeedle = 0, hashHaystack = 0, idx; if (cs == Qt::CaseSensitive) { for (idx = 0; idx < sl; ++idx) { hashNeedle = ((hashNeedle<<1) + needle[idx].unicode()); hashHaystack = ((hashHaystack<<1) + haystack[idx].unicode()); } hashHaystack -= (haystack+sl_minus_1)->unicode(); while (haystack <= end) { hashHaystack += (haystack+sl_minus_1)->unicode(); if (hashHaystack == hashNeedle && ucstrncmp(needle, haystack, sl) == 0) return haystack-unicode(); REHASH(haystack->unicode()); ++haystack; } } else { for (idx = 0; idx < sl; ++idx) { hashNeedle = ((hashNeedle<<1) + ::lower(needle[idx].unicode()).unicode()); hashHaystack = ((hashHaystack<<1) + ::lower(haystack[idx].unicode()).unicode()); } hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode(); while (haystack <= end) { hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode(); if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) return haystack-unicode(); REHASH(::lower(*haystack).unicode()); ++haystack; } } return -1;}/*! \overload Returns the index position of the first occurrence of the character \a ch in the string, searching forward from index position \a from. Returns -1 if \a ch could not be found.*/int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const{ if (from < 0) from = qMax(from + d->size, 0); if (from < d->size) { const QChar *n = (const QChar*)d->data + from - 1; const QChar *e = (const QChar*)d->data + d->size; if (cs == Qt::CaseSensitive) { while (++n != e) if (*n == ch) return n - (const QChar*)d->data; } else { ch = ::lower(ch); while (++n != e) if (::lower(*n) == ch) return n - (const QChar*)d->data; } } return -1;}/*! Returns the index position of the last occurrence of the string \a str in this string, searching backward from index position \a from. If \a from is -1 (the default), the search starts at the last character; if \a from is -2, at the next to last character and so on. Returns -1 if \a str is not found. If \a cs is Qt::CaseSensitive (the default), the search is case sensitive; otherwise the search is case insensitive. Example: \code QString x = "crazy azimuths"; QString y = "az"; x.lastIndexOf(y); // returns 6 x.lastIndexOf(y, 6); // returns 6 x.lastIndexOf(y, 5); // returns 2 x.lastIndexOf(y, 1); // returns -1 \endcode \sa indexOf(), contains(), count()*/int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const{ /* See indexOf() for explanations. */ const int l = d->size; if (from < 0) from += l; const int sl = str.d->size; int delta = l-sl; if (from == l && sl == 0) return from; if (from < 0 || from >= l || delta < 0) return -1; if (from > delta) from = delta; if (sl == 1) return lastIndexOf(*(const QChar*) str.d->data, from, cs); const QChar *needle = (const QChar*) str.d->data; const QChar *haystack = (const QChar*) d->data + from; const QChar *end = (const QChar*) d->data; const int sl_minus_1 = sl-1; const QChar *n = needle+sl_minus_1; const QChar *h = haystack+sl_minus_1; int hashNeedle = 0, hashHaystack = 0, idx; if (cs == Qt::CaseSensitive) { for (idx = 0; idx < sl; ++idx) { hashNeedle = ((hashNeedle<<1) + (n-idx)->unicode()); hashHaystack = ((hashHaystack<<1) + (h-idx)->unicode()); } hashHaystack -= haystack->unicode(); while (haystack >= end) { hashHaystack += haystack->unicode(); if (hashHaystack == hashNeedle && ucstrncmp(needle, haystack, sl) == 0) return haystack-unicode(); --haystack; REHASH((haystack+sl)->unicode()); } } else { for (idx = 0; idx < sl; ++idx) { hashNeedle = ((hashNeedle<<1) + ::lower((n-idx)->unicode()).unicode()); hashHaystack = ((hashHaystack<<1) + ::lower((h-idx)->unicode()).unicode()); } hashHaystack -= ::lower(*haystack).unicode(); while (haystack >= end) { hashHaystack += ::lower(*haystack).unicode(); if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) return haystack-unicode(); --haystack; REHASH(::lower(*(haystack+sl)).unicode()); } } return -1;}/*! \overload Returns the index position of the last occurrence of the character \a ch, searching backward from position \a from.*/int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const{ if (from < 0) from += d->size; if (from < 0 || from >= d->size) return -1; if (from >= 0) { const QChar *n = (const QChar*)d->data + from; const QChar *b = (const QChar*)d->data; if (cs == Qt::CaseSensitive) { for (; n >= b; --n) if (*n == ch) return n - b; } else { ch = ::lower(ch); for (; n >= b; --n) if (::lower(*n) == ch) return n - b; } } return -1;}#ifndef QT_NO_REGEXP/*! \overload Replaces every occurrence of the regular expression \a rx in the string with \a after. Returns a reference to the string. For example: \code QString str = "Banana"; str.replace(QRegExp("a[mn]"), "ox"); // str == "Boxoxa" \endcode For regular expressions containing \l{capturing parentheses}, occurrences of \bold{\\1}, \bold{\\2}, ..., in \a after are replaced with \a{rx}.cap(1), cap(2), ... \code QString str = "A <i>bon mot</i>."; str.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}"); // str == "A \\emph{bon mot}." \endcode \sa indexOf(), lastIndexOf(), remove(), QRegExp::cap()*/QString& QString::replace(const QRegExp &rx, const QString &after){ QRegExp rx2(rx); if (isEmpty() && rx2.indexIn(*this) == -1) return *this; realloc(); int index = 0; int numCaptures = rx2.numCaptures(); int al = after.length(); QRegExp::CaretMode caretMode = QRegExp::CaretAtZero; if (numCaptures > 0) { if (numCaptures > 9) numCaptures = 9; const QChar *uc = after.unicode(); int numBackRefs = 0; for (int i = 0; i < al - 1; i++) { if (uc[i] == QLatin1Char('\\')) { int no = uc[i + 1].digitValue(); if (no > 0 && no <= numCaptures) numBackRefs++; } } /* This is the harder case where we have back-references. We don't try to optimize it. */ if (numBackRefs > 0) { int *capturePositions = new int[numBackRefs]; int *captureNumbers = new int[numBackRefs]; int j = 0; for (int i = 0; i < al - 1; i++) { if (uc[i] == QLatin1Char('\\')) { int no = uc[i + 1].digitValue(); if (no > 0 && no <= numCaptures) { capturePositions[j] = i; captureNumbers[j] = no; j++; } } } while (index <= length()) { index = rx2.indexIn(*this, index, caretMode); if (index == -1) break; QString after2 = after; for (j = numBackRefs - 1; j >= 0; j--) after2.replace(capturePositions[j], 2, rx2.cap(captureNumbers[j])); replace(index, rx2.matchedLength(), after2); index += after2.length(); if (rx2.matchedLength() == 0) { // avoid infinite loop on 0-length matches (e.g., [a-z]*) index++; } caretMode = QRegExp::CaretWontMatch; } delete[] capturePositions; delete[] captureNumbers; return *this; } } /* This is the simple and optimized case where we don't have back-references. */ while (index != -1) { struct { int pos; int length; } replacements[2048]; int pos = 0; int adjust = 0; while (pos < 2047) { index = rx2.indexIn(*this, index, caretMode); if (index == -1) break; int ml = rx2.matchedLength(); replacements[pos].pos = index; replacements[pos++].length = ml; index +=
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -