📄 qstring.cpp
字号:
\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.*//*! \fn bool QString::operator>(const QString &other) const Returns true if this string is lexically greater 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 with localeAwareCompare().*//*! \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 || *c == '\0') return !isEmpty(); while (*c) { if (uc == e || *uc != *c) break; ++uc; ++c; } return (uc == e ? false : *uc > *c);}/*! \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.*//*! \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 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.*//*! \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 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.*/#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: \quotefromfile snippets/qstring/main.cpp \skipto Widget::indexOfFunction() \skipto QString x \printuntil x.indexOf(y, 11) 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) + QUnicodeTables::lower(needle[idx].unicode())); hashHaystack = ((hashHaystack<<1) + QUnicodeTables::lower(haystack[idx].unicode())); } hashHaystack -= QUnicodeTables::lower(*(haystack+sl_minus_1)).unicode(); while (haystack <= end) { hashHaystack += QUnicodeTables::lower(*(haystack+sl_minus_1)).unicode(); if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) return haystack-unicode(); REHASH(QUnicodeTables::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 = QUnicodeTables::lower(ch); while (++n != e) if (QUnicodeTables::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: \quotefromfile snippets/qstring/main.cpp \skipto Widget::lastIndexOfFunction() \skipto QString x \printuntil x.lastIndexOf(y, 1) \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) + QUnicodeTables::lower((n-idx)->unicode())); hashHaystack = ((hashHaystack<<1) + QUnicodeTables::lower((h-idx)->unicode())); } hashHaystack -= QUnicodeTables::lower(*haystack).unicode(); while (haystack >= end) { hashHaystack += QUnicodeTables::lower(*haystack).unicode(); if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) return haystack-unicode(); --haystack; REHASH(QUnicodeTables::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 = QUnicodeTables::lower(ch); for (; n >= b; --n) if (QUnicodeTables::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: \quotefromfile snippets/qstring/main.cpp \skipto Widget::replaceFunction() \skipto QString s = \printuntil // s == "Boxoxa" 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), ... \quotefromfile snippets/qstring/main.cpp \skipto Widget::replaceFunction() \skipto QString t \printuntil // t == "A \\emph{bon mot}." \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 numCaptur
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -