📄 qstring.cpp
字号:
const uchar *c = (uchar *) other.latin1(); if (!c || *c == 0) return false; while (*c) { if (uc == e || *uc != *c) break; ++uc; ++c; } return (uc == e ? *c : *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 less 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 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(QChar(str.d->data[0]), 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 ushort *needle = str.d->data; const ushort *haystack = d->data + from; const ushort *end = 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]); hashHaystack = ((hashHaystack<<1) + haystack[idx]); } hashHaystack -= haystack[sl_minus_1]; while (haystack <= end) { hashHaystack += haystack[sl_minus_1]; if (hashHaystack == hashNeedle && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0) return haystack - d->data; REHASH(*haystack); ++haystack; } } else { const ushort *haystack_start = d->data; for (idx = 0; idx < sl; ++idx) { hashNeedle = (hashNeedle<<1) + foldCase(needle + idx, needle); hashHaystack = (hashHaystack<<1) + foldCase(haystack + idx, haystack_start); } hashHaystack -= foldCase(haystack + sl_minus_1, haystack_start); while (haystack <= end) { hashHaystack += foldCase(haystack + sl_minus_1, haystack_start); if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) return haystack - d->data; REHASH(foldCase(haystack, haystack_start)); ++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{ ushort c = ch.unicode(); if (from < 0) from = qMax(from + d->size, 0); if (from < d->size) { const ushort *n = d->data + from - 1; const ushort *e = d->data + d->size; if (cs == Qt::CaseSensitive) { while (++n != e) if (*n == c) return n - d->data; } else { c = foldCase(c); while (++n != e) if (foldCase(*n) == c) return n - 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(QChar(str.d->data[0]), from, cs); const ushort *needle = str.d->data; const ushort *haystack = d->data + from; const ushort *end = d->data; const int sl_minus_1 = sl-1; const ushort *n = needle+sl_minus_1; const ushort *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)); hashHaystack = ((hashHaystack<<1) + *(h-idx)); } hashHaystack -= *haystack; while (haystack >= end) { hashHaystack += *haystack; if (hashHaystack == hashNeedle && ucstrncmp((const QChar *)needle, (const QChar *)haystack, sl) == 0) return haystack - d->data; --haystack; REHASH(haystack[sl]); } } else { for (idx = 0; idx < sl; ++idx) { hashNeedle = ((hashNeedle<<1) + foldCase(n-idx, str.d->data)); hashHaystack = ((hashHaystack<<1) + foldCase(h-idx, end)); } hashHaystack -= foldCase(haystack, end); while (haystack >= end) { hashHaystack += foldCase(haystack, end); if (hashHaystack == hashNeedle && ucstrnicmp(needle, haystack, sl) == 0) return haystack - d->data; --haystack; REHASH(foldCase(haystack + sl, end)); } } 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{ ushort c = ch.unicode(); if (from < 0) from += d->size; if (from < 0 || from >= d->size) return -1; if (from >= 0) { const ushort *n = d->data + from; const ushort *b = d->data; if
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -