Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From b0e7cc60369b9952e56549e921b69237f7377a65 Mon Sep 17 00:00:00 2001
- From: Denis Dzyubenko <denis.dzyubenko@nokia.com>
- Date: Fri, 15 Oct 2010 13:37:10 +0200
- Subject: [PATCH] Added functions to QString to use QStringRef more.
- Also modified a few places in Qt where using QStringRef should be preferred.
- Reviewed-by: pending
- ---
- src/corelib/io/qresource.cpp | 14 +-
- src/corelib/io/qurl.cpp | 31 +-
- src/corelib/kernel/qcoreapplication.cpp | 5 +-
- src/corelib/kernel/qmetaobject.cpp | 5 +-
- src/corelib/tools/qdatetime.cpp | 28 +-
- src/corelib/tools/qlocale.cpp | 63 +++-
- src/corelib/tools/qlocale.h | 1 +
- src/corelib/tools/qlocale_p.h | 5 +-
- src/corelib/tools/qstring.cpp | 698 ++++++++++++++++++++++++++++++-
- src/corelib/tools/qstring.h | 52 +++-
- src/testlib/qtest.h | 16 +
- tests/auto/qstring/tst_qstring.cpp | 80 ++++-
- 12 files changed, 937 insertions(+), 61 deletions(-)
- diff --git a/src/corelib/io/qresource.cpp b/src/corelib/io/qresource.cpp
- index 929885c..b39a75e 100644
- --- a/src/corelib/io/qresource.cpp
- +++ b/src/corelib/io/qresource.cpp
- @@ -316,7 +316,7 @@ QResourcePrivate::ensureInitialized() const
- QString path = fileName;
- if(path.startsWith(QLatin1Char(':')))
- - path = path.mid(1);
- + path.remove(0, 1);
- if(path.startsWith(QLatin1Char('/'))) {
- that->load(path);
- @@ -343,7 +343,7 @@ QResourcePrivate::ensureChildren() const
- QString path = absoluteFilePath, k;
- if(path.startsWith(QLatin1Char(':')))
- - path = path.mid(1);
- + path.remove(0, 1);
- QSet<QString> kids;
- QString cleaned = cleanPath(path);
- for(int i = 0; i < related.size(); ++i) {
- @@ -641,7 +641,7 @@ int QResourceRoot::findNode(const QString &_path, const QLocale &locale) const
- if(!root.endsWith(QLatin1Char('/')))
- root += QLatin1Char('/');
- if(path.size() >= root.size() && path.startsWith(root))
- - path = path.mid(root.length()-1);
- + path.remove(0, root.length()-1);
- if(path.isEmpty())
- path = QLatin1Char('/');
- }
- @@ -816,8 +816,8 @@ bool QResourceRoot::mappingRootSubdir(const QString &path, QString *match) const
- {
- const QString root = mappingRoot();
- if(!root.isEmpty()) {
- - const QStringList root_segments = root.split(QLatin1Char('/'), QString::SkipEmptyParts),
- - path_segments = path.split(QLatin1Char('/'), QString::SkipEmptyParts);
- + const QList<QStringRef> root_segments = root.splitRef(QLatin1Char('/'), QString::SkipEmptyParts),
- + path_segments = path.splitRef(QLatin1Char('/'), QString::SkipEmptyParts);
- if(path_segments.size() <= root_segments.size()) {
- int matched = 0;
- for(int i = 0; i < path_segments.size(); ++i) {
- @@ -827,7 +827,7 @@ bool QResourceRoot::mappingRootSubdir(const QString &path, QString *match) const
- }
- if(matched == path_segments.size()) {
- if(match && root_segments.size() > matched)
- - *match = root_segments.at(matched);
- + *match = root_segments.at(matched).toString();
- return true;
- }
- }
- @@ -1040,7 +1040,7 @@ public:
- static QString qt_resource_fixResourceRoot(QString r) {
- if(!r.isEmpty()) {
- if(r.startsWith(QLatin1Char(':')))
- - r = r.mid(1);
- + r = r.remove(0, 1);
- if(!r.isEmpty())
- r = QDir::cleanPath(r);
- }
- diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
- index 6452c0f..951fa40 100644
- --- a/src/corelib/io/qurl.cpp
- +++ b/src/corelib/io/qurl.cpp
- @@ -203,6 +203,15 @@ extern void q_normalizePercentEncoding(QByteArray *ba, const char *exclude);
- extern void q_toPercentEncoding(QByteArray *ba, const char *exclude, const char *include = 0);
- extern void q_fromPercentEncoding(QByteArray *ba);
- +static QByteArray toPercentEncodingHelper(const QStringRef &s, const char *exclude, const char *include = 0)
- +{
- + if (s.isNull())
- + return QByteArray(); // null
- + QByteArray ba = s.toUtf8();
- + q_toPercentEncoding(&ba, exclude, include);
- + return ba;
- +}
- +
- static QByteArray toPercentEncodingHelper(const QString &s, const char *exclude, const char *include = 0)
- {
- if (s.isNull())
- @@ -302,7 +311,7 @@ public:
- void ensureEncodedParts() const;
- QString authority(QUrl::FormattingOptions options = QUrl::None) const;
- void setAuthority(const QString &auth);
- - void setUserInfo(const QString &userInfo);
- + void setUserInfo(const QStringRef &userInfo);
- QString userInfo(QUrl::FormattingOptions options = QUrl::None) const;
- void setEncodedAuthority(const QByteArray &authority);
- void setEncodedUserInfo(const QUrlParseData *parseData);
- @@ -3542,7 +3551,7 @@ void QUrlPrivate::setAuthority(const QString &auth)
- {
- isHostValid = true;
- if (auth.isEmpty()) {
- - setUserInfo(QString());
- + setUserInfo(QStringRef());
- host.clear();
- port = -1;
- return;
- @@ -3579,7 +3588,7 @@ void QUrlPrivate::setAuthority(const QString &auth)
- int userInfoIndex = auth.indexOf(QLatin1Char('@'));
- if (userInfoIndex != -1 && (portIndex == -1 || userInfoIndex < portIndex))
- - setUserInfo(auth.left(userInfoIndex));
- + setUserInfo(auth.leftRef(userInfoIndex));
- int hostIndex = 0;
- if (userInfoIndex != -1)
- @@ -3588,22 +3597,22 @@ void QUrlPrivate::setAuthority(const QString &auth)
- if (portIndex != -1)
- hostLength -= (auth.length() - portIndex);
- - host = auth.mid(hostIndex, hostLength).trimmed();
- + host = auth.midRef(hostIndex, hostLength).trimmedRef().toString();
- }
- -void QUrlPrivate::setUserInfo(const QString &userInfo)
- +void QUrlPrivate::setUserInfo(const QStringRef &userInfo)
- {
- encodedUserName.clear();
- encodedPassword.clear();
- int delimIndex = userInfo.indexOf(QLatin1Char(':'));
- if (delimIndex == -1) {
- - userName = userInfo;
- + userName = userInfo.toString();
- password.clear();
- return;
- }
- - userName = userInfo.left(delimIndex);
- - password = userInfo.right(userInfo.length() - delimIndex - 1);
- + userName = userInfo.leftRef(delimIndex).toString();
- + password = userInfo.rightRef(userInfo.length() - delimIndex - 1).toString();
- }
- void QUrlPrivate::setEncodedUserInfo(const QUrlParseData *parseData)
- @@ -4338,8 +4347,8 @@ void QUrl::setUrl(const QString &url, ParsingMode parsingMode)
- }
- QByteArray encodedUrl;
- if (start != -1) {
- - QString hostPart = tmp.left(start);
- - QString otherPart = tmp.mid(start);
- + QStringRef hostPart = tmp.leftRef(start);
- + QStringRef otherPart = tmp.midRef(start);
- encodedUrl = toPercentEncodingHelper(hostPart, ":/?#[]@!$&'()*+,;=")
- + toPercentEncodingHelper(otherPart, ":/?#@!$&'()*+,;=");
- } else {
- @@ -4540,7 +4549,7 @@ void QUrl::setUserInfo(const QString &userInfo)
- detach();
- QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized);
- - d->setUserInfo(userInfo.trimmed());
- + d->setUserInfo(QStringRef(&userInfo).trimmedRef());
- }
- /*!
- diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
- index e967884..751d9da 100644
- --- a/src/corelib/kernel/qcoreapplication.cpp
- +++ b/src/corelib/kernel/qcoreapplication.cpp
- @@ -1988,8 +1988,9 @@ QString QCoreApplication::applicationFilePath()
- */
- QByteArray pEnv = qgetenv("PATH");
- QDir currentDir = QDir::current();
- - QStringList paths = QString::fromLocal8Bit(pEnv.constData()).split(QLatin1Char(':'));
- - for (QStringList::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) {
- + QString path = QString::fromLocal8Bit(pEnv.constData());
- + QList<QStringRef> paths = path.splitRef(QLatin1Char(':'));
- + for (QList<QStringRef>::const_iterator p = paths.constBegin(); p != paths.constEnd(); ++p) {
- if ((*p).isEmpty())
- continue;
- QString candidate = currentDir.absoluteFilePath(*p + QLatin1Char('/') + argv0);
- diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp
- index ceb2a9c..7d92e19 100644
- --- a/src/corelib/kernel/qmetaobject.cpp
- +++ b/src/corelib/kernel/qmetaobject.cpp
- @@ -1898,13 +1898,14 @@ int QMetaEnum::keysToValue(const char *keys) const
- {
- if (!mobj)
- return -1;
- - QStringList l = QString::fromLatin1(keys).split(QLatin1Char('|'));
- + QString keysStr = QString::fromLatin1(keys);
- + QList<QStringRef> l = keysStr.splitRef(QLatin1Char('|'));
- //#### TODO write proper code, do not use QStringList
- int value = 0;
- int count = mobj->d.data[handle + 2];
- int data = mobj->d.data[handle + 3];
- for (int li = 0; li < l.size(); ++li) {
- - QString trimmed = l.at(li).trimmed();
- + QStringRef trimmed = l.at(li).trimmedRef();
- QByteArray qualified_key = trimmed.toLatin1();
- const char *key = qualified_key.constData();
- uint scope = 0;
- diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp
- index ab7530d..2dbac9b 100644
- --- a/src/corelib/tools/qdatetime.cpp
- +++ b/src/corelib/tools/qdatetime.cpp
- @@ -1170,13 +1170,13 @@ QDate QDate::fromString(const QString& s, Qt::DateFormat f)
- default:
- #ifndef QT_NO_TEXTDATE
- case Qt::TextDate: {
- - QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts);
- + QList<QStringRef> parts = s.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
- if (parts.count() != 4) {
- return QDate();
- }
- - QString monthName = parts.at(1);
- + QStringRef monthName = parts.at(1);
- int month = -1;
- // Assume that English monthnames are the default
- for (int i = 0; i < 12; ++i) {
- @@ -1831,13 +1831,13 @@ QTime QTime::fromString(const QString& s, Qt::DateFormat f)
- default:
- {
- bool ok = true;
- - const int hour(s.mid(0, 2).toInt(&ok));
- + const int hour(s.midRef(0, 2).toInt(&ok));
- if (!ok)
- return QTime();
- - const int minute(s.mid(3, 2).toInt(&ok));
- + const int minute(s.midRef(3, 2).toInt(&ok));
- if (!ok)
- return QTime();
- - const int second(s.mid(6, 2).toInt(&ok));
- + const int second(s.midRef(6, 2).toInt(&ok));
- if (!ok)
- return QTime();
- const QString msec_s(QLatin1String("0.") + s.mid(9, 4));
- @@ -3265,7 +3265,7 @@ int QDateTime::utcOffset() const
- #ifndef QT_NO_DATESTRING
- -static int fromShortMonthName(const QString &monthName)
- +static int fromShortMonthName(const QStringRef &monthName)
- {
- // Assume that English monthnames are the default
- for (int i = 0; i < 12; ++i) {
- @@ -3348,7 +3348,7 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f)
- : QLocale::ShortFormat));
- #if !defined(QT_NO_TEXTDATE)
- case Qt::TextDate: {
- - QStringList parts = s.split(QLatin1Char(' '), QString::SkipEmptyParts);
- + QList<QStringRef> parts = s.splitRef(QLatin1Char(' '), QString::SkipEmptyParts);
- if ((parts.count() < 5) || (parts.count() > 6)) {
- return QDateTime();
- @@ -3369,7 +3369,7 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f)
- // first variant failed, lets try the other
- month = fromShortMonthName(parts.at(2));
- if (month != -1) {
- - QString dayStr = parts.at(1);
- + QStringRef dayStr = parts.at(1);
- if (dayStr.endsWith(QLatin1Char('.'))) {
- dayStr.chop(1);
- day = dayStr.toInt(&ok);
- @@ -3387,13 +3387,13 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f)
- }
- int year;
- - QStringList timeParts = parts.at(3).split(QLatin1Char(':'));
- + QList<QStringRef> timeParts = parts.at(3).splitRef(QLatin1Char(':'));
- if ((timeParts.count() == 3) || (timeParts.count() == 2)) {
- year = parts.at(4).toInt(&ok);
- if (!ok)
- return QDateTime();
- } else {
- - timeParts = parts.at(4).split(QLatin1Char(':'));
- + timeParts = parts.at(4).splitRef(QLatin1Char(':'));
- if ((timeParts.count() != 3) && (timeParts.count() != 2))
- return QDateTime();
- year = parts.at(3).toInt(&ok);
- @@ -3422,7 +3422,7 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f)
- if (parts.count() == 5)
- return QDateTime(date, time, Qt::LocalTime);
- - QString tz = parts.at(5);
- + QStringRef tz = parts.at(5);
- if (!tz.startsWith(QLatin1String("GMT"), Qt::CaseInsensitive))
- return QDateTime();
- int tzoffset = 0;
- @@ -3432,10 +3432,10 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f)
- && (sign != QLatin1Char('-'))) {
- return QDateTime();
- }
- - int tzhour = tz.mid(4, 2).toInt(&ok);
- + int tzhour = tz.midRef(4, 2).toInt(&ok);
- if (!ok)
- return QDateTime();
- - int tzminute = tz.mid(6).toInt(&ok);
- + int tzminute = tz.midRef(6).toInt(&ok);
- if (!ok)
- return QDateTime();
- tzoffset = (tzhour*60 + tzminute) * 60;
- @@ -5472,7 +5472,7 @@ int QDateTimeParser::findAmPm(QString &str, int index, int *used) const
- }
- if (used)
- *used = str.size();
- - if (str.trimmed().isEmpty()) {
- + if (str.trimmedRef().isEmpty()) {
- return PossibleBoth;
- }
- const QLatin1Char space(' ');
- diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp
- index 6b1de5e..f33dd39 100644
- --- a/src/corelib/tools/qlocale.cpp
- +++ b/src/corelib/tools/qlocale.cpp
- @@ -4322,23 +4322,23 @@ static bool removeGroupSeparators(QLocalePrivate::CharBuff *num)
- number. We can't detect junk here, since we don't even know the base
- of the number.
- */
- -bool QLocalePrivate::numberToCLocale(const QString &num,
- - GroupSeparatorMode group_sep_mode,
- - CharBuff *result) const
- +bool QLocalePrivate::numberToCLocale(const QStringRef &num,
- + GroupSeparatorMode group_sep_mode,
- + CharBuff *result) const
- {
- const QChar *uc = num.unicode();
- - int l = num.length();
- + int len = num.length();
- int idx = 0;
- // Skip whitespace
- - while (idx < l && uc[idx].isSpace())
- + while (idx < len && uc[idx].isSpace())
- ++idx;
- - if (idx == l)
- + if (idx == len)
- return false;
- const QChar _group = group();
- - while (idx < l) {
- + while (idx < len) {
- const QChar &in = uc[idx];
- char out = digitToCLocale(in);
- @@ -4362,7 +4362,7 @@ bool QLocalePrivate::numberToCLocale(const QString &num,
- }
- // Check trailing whitespace
- - for (; idx < l; ++idx) {
- + for (; idx < len; ++idx) {
- if (!uc[idx].isSpace())
- return false;
- }
- @@ -4475,7 +4475,7 @@ double QLocalePrivate::stringToDouble(const QString &number, bool *ok,
- GroupSeparatorMode group_sep_mode) const
- {
- CharBuff buff;
- - if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
- + if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmedRef() : QStringRef(&number),
- group_sep_mode, &buff)) {
- if (ok != 0)
- *ok = false;
- @@ -4488,7 +4488,7 @@ qlonglong QLocalePrivate::stringToLongLong(const QString &number, int base,
- bool *ok, GroupSeparatorMode group_sep_mode) const
- {
- CharBuff buff;
- - if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
- + if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmedRef() : QStringRef(&number),
- group_sep_mode, &buff)) {
- if (ok != 0)
- *ok = false;
- @@ -4502,7 +4502,7 @@ qulonglong QLocalePrivate::stringToUnsLongLong(const QString &number, int base,
- bool *ok, GroupSeparatorMode group_sep_mode) const
- {
- CharBuff buff;
- - if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number,
- + if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmedRef() : QStringRef(&number),
- group_sep_mode, &buff)) {
- if (ok != 0)
- *ok = false;
- @@ -4513,6 +4513,47 @@ qulonglong QLocalePrivate::stringToUnsLongLong(const QString &number, int base,
- }
- +double QLocalePrivate::stringToDouble(const QStringRef &number, bool *ok,
- + GroupSeparatorMode group_sep_mode) const
- +{
- + CharBuff buff;
- + if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmedRef() : number,
- + group_sep_mode, &buff)) {
- + if (ok != 0)
- + *ok = false;
- + return 0.0;
- + }
- + return bytearrayToDouble(buff.constData(), ok);
- +}
- +
- +qlonglong QLocalePrivate::stringToLongLong(const QStringRef &number, int base,
- + bool *ok, GroupSeparatorMode group_sep_mode) const
- +{
- + CharBuff buff;
- + if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmedRef() : number,
- + group_sep_mode, &buff)) {
- + if (ok != 0)
- + *ok = false;
- + return 0;
- + }
- +
- + return bytearrayToLongLong(buff.constData(), base, ok);
- +}
- +
- +qulonglong QLocalePrivate::stringToUnsLongLong(const QStringRef &number, int base,
- + bool *ok, GroupSeparatorMode group_sep_mode) const
- +{
- + CharBuff buff;
- + if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmedRef() : number,
- + group_sep_mode, &buff)) {
- + if (ok != 0)
- + *ok = false;
- + return 0;
- + }
- +
- + return bytearrayToUnsLongLong(buff.constData(), base, ok);
- +}
- +
- double QLocalePrivate::bytearrayToDouble(const char *num, bool *ok, bool *overflow)
- {
- if (ok != 0)
- diff --git a/src/corelib/tools/qlocale.h b/src/corelib/tools/qlocale.h
- index 8b424bb..795168b 100644
- --- a/src/corelib/tools/qlocale.h
- +++ b/src/corelib/tools/qlocale.h
- @@ -112,6 +112,7 @@ class Q_CORE_EXPORT QLocale
- Q_ENUMS(Language)
- Q_ENUMS(Country)
- friend class QString;
- + friend class QStringRef;
- friend class QByteArray;
- friend class QIntValidator;
- friend class QDoubleValidator;
- diff --git a/src/corelib/tools/qlocale_p.h b/src/corelib/tools/qlocale_p.h
- index 6205745..669c184 100644
- --- a/src/corelib/tools/qlocale_p.h
- +++ b/src/corelib/tools/qlocale_p.h
- @@ -125,13 +125,16 @@ public:
- qint64 stringToLongLong(const QString &num, int base, bool *ok, GroupSeparatorMode group_sep_mode) const;
- quint64 stringToUnsLongLong(const QString &num, int base, bool *ok, GroupSeparatorMode group_sep_mode) const;
- + double stringToDouble(const QStringRef &num, bool *ok, GroupSeparatorMode group_sep_mode) const;
- + qint64 stringToLongLong(const QStringRef &num, int base, bool *ok, GroupSeparatorMode group_sep_mode) const;
- + quint64 stringToUnsLongLong(const QStringRef &num, int base, bool *ok, GroupSeparatorMode group_sep_mode) const;
- static double bytearrayToDouble(const char *num, bool *ok, bool *overflow = 0);
- static qint64 bytearrayToLongLong(const char *num, int base, bool *ok, bool *overflow = 0);
- static quint64 bytearrayToUnsLongLong(const char *num, int base, bool *ok);
- typedef QVarLengthArray<char, 256> CharBuff;
- - bool numberToCLocale(const QString &num,
- + bool numberToCLocale(const QStringRef &num,
- GroupSeparatorMode group_sep_mode,
- CharBuff *result) const;
- inline char digitToCLocale(const QChar &c) const;
- diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
- index cfffc4a..04ebb82 100644
- --- a/src/corelib/tools/qstring.cpp
- +++ b/src/corelib/tools/qstring.cpp
- @@ -1723,6 +1723,20 @@ QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
- }
- /*!
- + \since 4.8
- + \overload
- +*/
- +QString &QString::remove(const QStringRef &str, Qt::CaseSensitivity cs)
- +{
- + if (str.string()) {
- + int i = 0;
- + while ((i = indexOf(str, i, cs)) != -1)
- + remove(i, str.size());
- + }
- + return *this;
- +}
- +
- +/*!
- Removes every occurrence of the character \a ch in this string, and
- returns a reference to this string.
- @@ -3216,7 +3230,7 @@ int QString::count(const QRegExp& rx) const
- QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const
- {
- - QStringList sections = split(sep, KeepEmptyParts,
- + QList<QStringRef> sections = splitRef(sep, KeepEmptyParts,
- (flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
- if (sections.isEmpty())
- return QString();
- @@ -3240,7 +3254,7 @@ QString QString::section(const QString &sep, int start, int end, SectionFlags fl
- QString ret;
- int first_i = start, last_i = end;
- for (int i = 0; x <= end && i < sections.size(); ++i) {
- - QString section = sections.at(i);
- + QStringRef section = sections.at(i);
- const bool empty = section.isEmpty();
- if (x >= start) {
- if(x == start)
- @@ -3264,9 +3278,9 @@ QString QString::section(const QString &sep, int start, int end, SectionFlags fl
- #ifndef QT_NO_REGEXP
- class qt_section_chunk {
- public:
- - qt_section_chunk(int l, QString s) { length = l; string = s; }
- + qt_section_chunk(int l, QStringRef s) { length = l; string = s; }
- int length;
- - QString string;
- + QStringRef string;
- };
- /*!
- @@ -3295,12 +3309,12 @@ QString QString::section(const QRegExp ®, int start, int end, SectionFlags fl
- QList<qt_section_chunk> sections;
- int n = length(), m = 0, last_m = 0, last_len = 0;
- while ((m = sep.indexIn(*this, m)) != -1) {
- - sections.append(qt_section_chunk(last_len, QString(uc + last_m, m - last_m)));
- + sections.append(qt_section_chunk(last_len, midRef(last_m, m - last_m)));
- last_m = m;
- last_len = sep.matchedLength();
- m += qMax(sep.matchedLength(), 1);
- }
- - sections.append(qt_section_chunk(last_len, QString(uc + last_m, n - last_m)));
- + sections.append(qt_section_chunk(last_len, midRef(last_m, n - last_m)));
- if(start < 0)
- start += sections.count();
- @@ -3321,18 +3335,18 @@ QString QString::section(const QRegExp ®, int start, int end, SectionFlags fl
- if(x != start)
- ret += section.string;
- else
- - ret += section.string.mid(section.length);
- + ret += section.string.midRef(section.length);
- }
- if (!empty || !(flags & SectionSkipEmpty))
- x++;
- }
- if((flags & SectionIncludeLeadingSep) && first_i < sections.size()) {
- const qt_section_chunk §ion = sections.at(first_i);
- - ret.prepend(section.string.left(section.length));
- + ret.prepend(section.string.leftRef(section.length));
- }
- if((flags & SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
- const qt_section_chunk §ion = sections.at(last_i+1);
- - ret += section.string.left(section.length);
- + ret += section.string.leftRef(section.length);
- }
- return ret;
- }
- @@ -4142,9 +4156,10 @@ QString QString::simplified() const
- \snippet doc/src/snippets/qstring/main.cpp 82
- - Unlike simplified(), trimmed() leaves internal whitespace alone.
- + Unlike simplified(), trimmed() and trimmedRef() leave internal whitespace
- + alone.
- - \sa simplified()
- + \sa simplified(), trimmedRef()
- */
- QString QString::trimmed() const
- {
- @@ -4169,6 +4184,23 @@ QString QString::trimmed() const
- return QString(s + start, l);
- }
- +/*!
- + \since 4.8
- +
- + Returns a reference to a substring that has whitespace removed from the
- + start and the end.
- +
- + Whitespace means any character for which QChar::isSpace() returns
- + true. This includes the ASCII characters '\\t', '\\n', '\\v',
- + '\\f', '\\r', and ' '.
- +
- + Unlike simplified(), trimmed() and trimmedRef() leave internal whitespace alone.
- +*/
- +QStringRef QString::trimmedRef() const
- +{
- + return QStringRef(this).trimmedRef();
- +}
- +
- /*! \fn const QChar QString::at(int position) const
- Returns the character at the given index \a position in the
- @@ -6086,6 +6118,36 @@ QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseS
- }
- /*!
- + \since 4.8
- + \overload
- +*/
- +QStringList QString::split(const QLatin1String &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + return split(QString(sep), behavior, cs);
- +}
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QStringList QString::split(const QStringRef &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + QStringList list;
- + int start = 0;
- + int extra = 0;
- + int end;
- + while ((end = indexOf(sep, start + extra, cs)) != -1) {
- + if (start != end || behavior == KeepEmptyParts)
- + list.append(mid(start, end - start));
- + start = end + sep.size();
- + extra = (sep.size() == 0 ? 1 : 0);
- + }
- + if (start != size() || behavior == KeepEmptyParts)
- + list.append(mid(start));
- + return list;
- +}
- +
- +/*!
- \overload
- */
- QStringList QString::split(const QChar &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
- @@ -6103,6 +6165,77 @@ QStringList QString::split(const QChar &sep, SplitBehavior behavior, Qt::CaseSen
- return list;
- }
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QString::splitRef(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + QList<QStringRef> list;
- + int start = 0;
- + int extra = 0;
- + int end;
- + while ((end = indexOf(sep, start + extra, cs)) != -1) {
- + if (start != end || behavior == KeepEmptyParts)
- + list.append(midRef(start, end - start));
- + start = end + sep.size();
- + extra = (sep.size() == 0 ? 1 : 0);
- + }
- + if (start != size() || behavior == KeepEmptyParts)
- + list.append(midRef(start));
- + return list;
- +}
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QString::splitRef(const QStringRef &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + QList<QStringRef> list;
- + int start = 0;
- + int extra = 0;
- + int end;
- + while ((end = indexOf(sep, start + extra, cs)) != -1) {
- + if (start != end || behavior == KeepEmptyParts)
- + list.append(midRef(start, end - start));
- + start = end + sep.size();
- + extra = (sep.size() == 0 ? 1 : 0);
- + }
- + if (start != size() || behavior == KeepEmptyParts)
- + list.append(midRef(start));
- + return list;
- +}
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QString::splitRef(const QLatin1String &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + return splitRef(QString(sep), behavior, cs);
- +}
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QString::splitRef(const QChar &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + QList<QStringRef> list;
- + int start = 0;
- + int end;
- + while ((end = indexOf(sep, start, cs)) != -1) {
- + if (start != end || behavior == KeepEmptyParts)
- + list.append(midRef(start, end - start));
- + start = end + 1;
- + }
- + if (start != size() || behavior == KeepEmptyParts)
- + list.append(midRef(start));
- + return list;
- +}
- +
- +
- #ifndef QT_NO_REGEXP
- /*!
- \overload
- @@ -6148,6 +6281,29 @@ QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const
- list.append(mid(start));
- return list;
- }
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QString::splitRef(const QRegExp &rx, SplitBehavior behavior) const
- +{
- + QRegExp rx2(rx);
- + QList<QStringRef> list;
- + int start = 0;
- + int extra = 0;
- + int end;
- + while ((end = rx2.indexIn(*this, start + extra)) != -1) {
- + int matchedLen = rx2.matchedLength();
- + if (start != end || behavior == KeepEmptyParts)
- + list.append(midRef(start, end - start));
- + start = end + matchedLen;
- + extra = (matchedLen == 0) ? 1 : 0;
- + }
- + if (start != size() || behavior == KeepEmptyParts)
- + list.append(midRef(start));
- + return list;
- +}
- #endif
- /*!
- @@ -9096,4 +9252,524 @@ QVector<uint> QStringRef::toUcs4() const
- return v;
- }
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QStringRef::splitRef(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + QList<QStringRef> list;
- + int start = 0;
- + int extra = 0;
- + int end;
- + while ((end = indexOf(sep, start + extra, cs)) != -1) {
- + if (start != end || behavior == QString::KeepEmptyParts)
- + list.append(midRef(start, end - start));
- + start = end + sep.size();
- + extra = (sep.size() == 0 ? 1 : 0);
- + }
- + if (start != size() || behavior == QString::KeepEmptyParts)
- + list.append(midRef(start));
- + return list;
- +}
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QStringRef::splitRef(const QStringRef &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + QList<QStringRef> list;
- + int start = 0;
- + int extra = 0;
- + int end;
- + while ((end = indexOf(sep, start + extra, cs)) != -1) {
- + if (start != end || behavior == QString::KeepEmptyParts)
- + list.append(midRef(start, end - start));
- + start = end + sep.size();
- + extra = (sep.size() == 0 ? 1 : 0);
- + }
- + if (start != size() || behavior == QString::KeepEmptyParts)
- + list.append(midRef(start));
- + return list;
- +}
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QStringRef::splitRef(const QLatin1String &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + return splitRef(QString(sep), behavior, cs);
- +}
- +
- +/*!
- + \since 4.8
- + \overload
- +*/
- +QList<QStringRef> QStringRef::splitRef(const QChar &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const
- +{
- + QList<QStringRef> list;
- + int start = 0;
- + int end;
- + while ((end = indexOf(sep, start, cs)) != -1) {
- + if (start != end || behavior == QString::KeepEmptyParts)
- + list.append(midRef(start, end - start));
- + start = end + 1;
- + }
- + if (start != size() || behavior == QString::KeepEmptyParts)
- + list.append(midRef(start));
- + return list;
- +}
- +
- +/*!
- + \since 4.8
- + Returns the string converted to a \c{long long} using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +
- + \sa toULongLong(), toInt()
- +*/
- +qint64 QStringRef::toLongLong(bool *ok, int base) const
- +{
- + if (!m_string || m_size == 0)
- + return 0;
- +
- +#if defined(QT_CHECK_RANGE)
- + if (base != 0 && (base < 2 || base > 36)) {
- + qWarning("QString::toLongLong: Invalid base (%d)", base);
- + base = 10;
- + }
- +#endif
- +
- + bool my_ok;
- + QLocale def_locale;
- + qint64 result = def_locale.d()->stringToLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
- + if (my_ok) {
- + if (ok != 0)
- + *ok = true;
- + return result;
- + }
- +
- + QLocale c_locale(QLocale::C);
- + return c_locale.d()->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
- +}
- +
- +/*!
- + \since 4.8
- + Returns the string converted to an \c{unsigned long long} using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +
- + \sa toLongLong()
- +*/
- +quint64 QStringRef::toULongLong(bool *ok, int base) const
- +{
- +#if defined(QT_CHECK_RANGE)
- + if (base != 0 && (base < 2 || base > 36)) {
- + qWarning("QString::toULongLong: Invalid base (%d)", base);
- + base = 10;
- + }
- +#endif
- +
- + bool my_ok;
- + QLocale def_locale;
- + quint64 result = def_locale.d()->stringToUnsLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
- + if (my_ok) {
- + if (ok != 0)
- + *ok = true;
- + return result;
- + }
- +
- + QLocale c_locale(QLocale::C);
- + return c_locale.d()->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
- +}
- +
- +/*!
- + \since 4.8
- +
- + Returns the string converted to a \c long using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +
- +
- + \sa toULong(), toInt()
- +*/
- +long QStringRef::toLong(bool *ok, int base) const
- +{
- + qint64 v = toLongLong(ok, base);
- + if (v < LONG_MIN || v > LONG_MAX) {
- + if (ok)
- + *ok = false;
- + v = 0;
- + }
- + return (long)v;
- +}
- +
- +/*!
- + \since 4.8
- +
- + Returns the string converted to an \c{unsigned long} using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +*/
- +ulong QStringRef::toULong(bool *ok, int base) const
- +{
- + quint64 v = toULongLong(ok, base);
- + if (v > ULONG_MAX) {
- + if (ok)
- + *ok = false;
- + v = 0;
- + }
- + return (ulong)v;
- +}
- +
- +
- +/*!
- + \since 4.8
- + Returns the string converted to an \c int using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +
- + \sa toUInt(), toDouble()
- +*/
- +int QStringRef::toInt(bool *ok, int base) const
- +{
- + qint64 v = toLongLong(ok, base);
- + if (v < INT_MIN || v > INT_MAX) {
- + if (ok)
- + *ok = false;
- + v = 0;
- + }
- + return v;
- +}
- +
- +/*!
- + \since 4.8
- + Returns the string converted to an \c{unsigned int} using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +
- + \sa toInt()
- +*/
- +uint QStringRef::toUInt(bool *ok, int base) const
- +{
- + quint64 v = toULongLong(ok, base);
- + if (v > UINT_MAX) {
- + if (ok)
- + *ok = false;
- + v = 0;
- + }
- + return (uint)v;
- +}
- +
- +/*!
- + \since 4.8
- +
- + Returns the string converted to a \c short using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +
- + \sa toUShort(), toInt()
- +*/
- +short QStringRef::toShort(bool *ok, int base) const
- +{
- + long v = toLongLong(ok, base);
- + if (v < SHRT_MIN || v > SHRT_MAX) {
- + if (ok)
- + *ok = false;
- + v = 0;
- + }
- + return (short)v;
- +}
- +
- +/*!
- + \since 4.8
- + Returns the string converted to an \c{unsigned short} using base \a
- + base, which is 10 by default and must be between 2 and 36, or 0.
- + Returns 0 if the conversion fails.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true.
- +
- + If \a base is 0, the C language convention is used: If the string
- + begins with "0x", base 16 is used; if the string begins with "0",
- + base 8 is used; otherwise, base 10 is used.
- +
- + \sa toShort()
- +*/
- +ushort QStringRef::toUShort(bool *ok, int base) const
- +{
- + ulong v = toULongLong(ok, base);
- + if (v > USHRT_MAX) {
- + if (ok)
- + *ok = false;
- + v = 0;
- + }
- + return (ushort)v;
- +}
- +
- +
- +/*!
- + \since 4.8
- + Returns the string converted to a \c double value.
- +
- + Returns 0.0 if the conversion fails.
- +
- + If a conversion error occurs, \c{*}\a{ok} is set to false;
- + otherwise \c{*}\a{ok} is set to true.
- +
- + \snippet doc/src/snippets/qstring/main.cpp 66
- +
- + Various string formats for floating point numbers can be converted
- + to double values:
- +
- + \snippet doc/src/snippets/qstring/main.cpp 67
- +
- + This function tries to interpret the string according to the
- + current locale. The current locale is determined from the
- + system at application startup and can be changed by calling
- + QLocale::setDefault(). If the string cannot be interpreted
- + according to the current locale, this function falls back
- + on the "C" locale.
- +
- + \snippet doc/src/snippets/qstring/main.cpp 69
- + \snippet doc/src/snippets/qstring/main.cpp 70
- +
- + Due to the ambiguity between the decimal point and thousands group
- + separator in various locales, this function does not handle
- + thousands group separators. If you need to convert such numbers,
- + see QLocale::toDouble().
- +
- + \snippet doc/src/snippets/qstring/main.cpp 68
- +
- + \sa QLocale::setDefault() QLocale::toDouble() trimmedRef()
- +*/
- +
- +double QStringRef::toDouble(bool *ok) const
- +{
- + bool my_ok;
- + QLocale def_locale;
- + double result = def_locale.d()->stringToDouble(*this, &my_ok, QLocalePrivate::FailOnGroupSeparators);
- + if (my_ok) {
- + if (ok != 0)
- + *ok = true;
- + return result;
- + }
- +
- + QLocale c_locale(QLocale::C);
- + return c_locale.d()->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators);
- +}
- +
- +/*!
- + \since 4.8
- + Returns the string converted to a \c float value.
- +
- + If a conversion error occurs, *\a{ok} is set to false; otherwise
- + *\a{ok} is set to true. Returns 0.0 if the conversion fails.
- +
- + \sa toDouble(), toInt()
- +*/
- +float QStringRef::toFloat(bool *ok) const
- +{
- + bool myOk;
- + double d = toDouble(&myOk);
- + if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
- + if (ok != 0)
- + *ok = false;
- + return 0.0;
- + }
- + if (ok != 0)
- + *ok = true;
- + return (float) d;
- +}
- +
- +/*!
- + \since 4.8
- +
- + Returns a reference to a substring that contains the \a n leftmost characters
- + of the string.
- +
- + The entire string is returned if \a n is greater than size() or
- + less than zero.
- +
- + \sa rightRef(), midRef(), startsWith()
- +*/
- +QStringRef QStringRef::leftRef(int n) const
- +{
- + if (n >= m_size || n < 0 || !m_string)
- + return *this;
- + return QStringRef(m_string, m_position, n);
- +}
- +
- +/*!
- + \since 4.8
- +
- + Returns a reference to a substring that contains the \a n rightmost characters
- + of the string.
- +
- + The entire string is returned if \a n is greater than size() or
- + less than zero.
- +
- + \sa leftRef(), midRef(), endsWith()
- +*/
- +QStringRef QStringRef::rightRef(int n) const
- +{
- + if (n >= m_size || n < 0 || !m_string)
- + return *this;
- + return QStringRef(m_string, m_position + m_size - n, n);
- +}
- +
- +/*!
- + \since 4.8
- +
- + Returns a reference to a substring that contains \a n characters of this
- + string, starting at the specified \a position index.
- +
- + Returns a null string if the \a position index exceeds the
- + length of the string. If there are less than \a n characters
- + available in the string starting at the given \a position, or if
- + \a n is -1 (default), the function returns all characters that
- + are available from the specified \a position.
- +
- + \sa leftRef(), rightRef()
- +*/
- +QStringRef QStringRef::midRef(int position, int n) const
- +{
- + if (position >= m_size || !m_size || !m_string)
- + return QStringRef();
- + if (n < 0)
- + n = m_size - position;
- + if (position < 0) {
- + n += position;
- + position = 0;
- + }
- + if (n + position > m_size)
- + n = m_size - position;
- + return QStringRef(m_string, m_position + position, n);
- +}
- +
- +/*!
- + \since 4.8
- +
- + Returns a reference to a substring that has whitespace removed from the
- + start and the end.
- +
- + Whitespace means any character for which QChar::isSpace() returns
- + true. This includes the ASCII characters '\\t', '\\n', '\\v',
- + '\\f', '\\r', and ' '.
- +
- + Unlike simplified(), trimmed() and trimmedRef() leave internal whitespace
- + alone.
- +
- + \sa simplified(), trimmed()
- +*/
- +QStringRef QStringRef::trimmedRef() const
- +{
- + if (!m_string || m_size == 0)
- + return QStringRef();
- + // skip whitespace chars from the start
- + int start = 0;
- + int end = m_size - 1;
- + while (start < m_size && at(start).isSpace())
- + ++start;
- + if (start <= m_size) {
- + while (end && at(end).isSpace())
- + --end;
- + }
- + return QStringRef(m_string, m_position + start, end - start + 1);
- +}
- +
- +
- +/*!
- + \since 4.8
- +
- + Truncates the string at the given \a position index.
- +
- + Essentially for QStringRef this just sets the size of the string.
- +
- + If the specified \a position index is beyond the end of the
- + string, nothing happens.
- +
- + If \a position is negative, it is equivalent to passing zero.
- +
- + \sa chop(), leftRef()
- +*/
- +void QStringRef::truncate(int pos)
- +{
- + if (!m_string || pos >= m_size || pos < 0)
- + return;
- + m_size = pos;
- +}
- +
- +/*!
- + \since 4.8
- +
- + Removes \a n characters from the end of the string.
- +
- + Essentially for QStringRef this just sets the size of the string.
- +
- + If \a n is greater than size(), the result is an empty string.
- +
- + If you want to remove characters from the \e beginning of the
- + string, just create a new QStringRef.
- +
- + \sa truncate()
- +*/
- +void QStringRef::chop(int n)
- +{
- + if (!m_string || n > m_size || n < 0)
- + return;
- + m_size -= n;
- +}
- +
- QT_END_NAMESPACE
- diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
- index 589fdc2..bebff92 100644
- --- a/src/corelib/tools/qstring.h
- +++ b/src/corelib/tools/qstring.h
- @@ -257,11 +257,13 @@ public:
- QString toCaseFolded() const Q_REQUIRED_RESULT;
- QString trimmed() const Q_REQUIRED_RESULT;
- + QStringRef trimmedRef() const Q_REQUIRED_RESULT;
- QString simplified() const Q_REQUIRED_RESULT;
- QString &insert(int i, QChar c);
- QString &insert(int i, const QChar *uc, int len);
- inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); }
- + inline QString &insert(int i, const QStringRef &s);
- QString &insert(int i, const QLatin1String &s);
- QString &append(QChar c);
- QString &append(const QString &s);
- @@ -269,6 +271,7 @@ public:
- QString &append(const QLatin1String &s);
- inline QString &prepend(QChar c) { return insert(0, c); }
- inline QString &prepend(const QString &s) { return insert(0, s); }
- + inline QString &prepend(const QStringRef &s) { return insert(0, s); }
- inline QString &prepend(const QLatin1String &s) { return insert(0, s); }
- inline QString &operator+=(QChar c) {
- @@ -287,6 +290,7 @@ public:
- QString &remove(int i, int len);
- QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- + QString &remove(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &replace(int i, int len, QChar after);
- QString &replace(int i, int len, const QChar *s, int slen);
- QString &replace(int i, int len, const QString &after);
- @@ -309,10 +313,24 @@ public:
- QStringList split(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
- Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QStringList split(const QStringRef &sep, SplitBehavior behavior = KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QStringList split(const QLatin1String &sep, SplitBehavior behavior = KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- QStringList split(const QChar &sep, SplitBehavior behavior = KeepEmptyParts,
- Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- +
- + QList<QStringRef> splitRef(const QString &sep, SplitBehavior behavior = KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QList<QStringRef> splitRef(const QStringRef &sep, SplitBehavior behavior = KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QList<QStringRef> splitRef(const QLatin1String &sep, SplitBehavior behavior = KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QList<QStringRef> splitRef(const QChar &sep, SplitBehavior behavior = KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- #ifndef QT_NO_REGEXP
- QStringList split(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const Q_REQUIRED_RESULT;
- + QList<QStringRef> splitRef(const QRegExp &sep, SplitBehavior behavior = KeepEmptyParts) const Q_REQUIRED_RESULT;
- #endif
- enum NormalizationForm {
- @@ -1136,6 +1154,15 @@ public:
- int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- + QList<QStringRef> splitRef(const QString &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QList<QStringRef> splitRef(const QStringRef &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QList<QStringRef> splitRef(const QLatin1String &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- + QList<QStringRef> splitRef(const QChar &sep, QString::SplitBehavior behavior = QString::KeepEmptyParts,
- + Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_REQUIRED_RESULT;
- +
- inline QBool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- inline QBool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- inline QBool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- @@ -1195,13 +1222,34 @@ public:
- int localeAwareCompare(const QStringRef &s) const;
- static int localeAwareCompare(const QStringRef &s1, const QString &s2);
- static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2);
- +
- + short toShort(bool *ok=0, int base=10) const;
- + ushort toUShort(bool *ok=0, int base=10) const;
- + int toInt(bool *ok=0, int base=10) const;
- + uint toUInt(bool *ok=0, int base=10) const;
- + long toLong(bool *ok=0, int base=10) const;
- + ulong toULong(bool *ok=0, int base=10) const;
- + qlonglong toLongLong(bool *ok=0, int base=10) const;
- + qulonglong toULongLong(bool *ok=0, int base=10) const;
- + float toFloat(bool *ok=0) const;
- + double toDouble(bool *ok=0) const;
- +
- + QStringRef leftRef(int n) const Q_REQUIRED_RESULT;
- + QStringRef rightRef(int n) const Q_REQUIRED_RESULT;
- + QStringRef midRef(int position, int n = -1) const Q_REQUIRED_RESULT;
- + QStringRef trimmedRef() const;
- + void truncate(int pos);
- + void chop(int n);
- };
- inline QStringRef &QStringRef::operator=(const QString *aString)
- { m_string = aString; m_position = 0; m_size = aString?aString->size():0; return *this; }
- inline QStringRef::QStringRef(const QString *aString, int aPosition, int aSize)
- - :m_string(aString), m_position(aPosition), m_size(aSize){}
- + :m_string(aString), m_position(aPosition), m_size(aSize)
- +{
- + Q_ASSERT((!aString && aPosition == 0 && aSize == 0) || (aString && aPosition >= 0 && aSize >= 0));
- +}
- inline QStringRef::QStringRef(const QString *aString)
- :m_string(aString), m_position(0), m_size(aString?aString->size() : 0){}
- @@ -1290,6 +1338,8 @@ inline QBool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) c
- +inline QString &QString::insert(int i, const QStringRef &s) { return insert(i, s.constData(), s.length()); }
- +
- QT_END_NAMESPACE
- QT_END_HEADER
- diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h
- index aa9b2bd..c13c503 100644
- --- a/src/testlib/qtest.h
- +++ b/src/testlib/qtest.h
- @@ -174,6 +174,14 @@ template<> inline char *toString(const QVariant &v)
- #ifndef QTEST_NO_SPECIALIZATIONS
- template<>
- #endif
- +inline bool qCompare(QStringRef const &t1, QLatin1String const &t2, const char *actual,
- + const char *expected, const char *file, int line)
- +{
- + return qCompare<QString>(t1.toString(), QString(t2), actual, expected, file, line);
- +}
- +#ifndef QTEST_NO_SPECIALIZATIONS
- +template<>
- +#endif
- inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
- const char *expected, const char *file, int line)
- {
- @@ -182,6 +190,14 @@ inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *act
- #ifndef QTEST_NO_SPECIALIZATIONS
- template<>
- #endif
- +inline bool qCompare(QLatin1String const &t1, QStringRef const &t2, const char *actual,
- + const char *expected, const char *file, int line)
- +{
- + return qCompare<QString>(QString(t1), t2.toString(), actual, expected, file, line);
- +}
- +#ifndef QTEST_NO_SPECIALIZATIONS
- +template<>
- +#endif
- inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *actual,
- const char *expected, const char *file, int line)
- {
- diff --git a/tests/auto/qstring/tst_qstring.cpp b/tests/auto/qstring/tst_qstring.cpp
- index af6b371..8050afa 100644
- --- a/tests/auto/qstring/tst_qstring.cpp
- +++ b/tests/auto/qstring/tst_qstring.cpp
- @@ -138,6 +138,10 @@ private slots:
- void rightRef();
- void leftRef();
- void stringRef();
- + void stringRef_leftRef();
- + void stringRef_rightRef();
- + void stringRef_midRef();
- + void stringRef_trimmed();
- void contains();
- void count();
- void lastIndexOf_data();
- @@ -1130,6 +1134,10 @@ void tst_QString::indexOf()
- QCOMPARE(haystack.indexOf(ref.at(0), startpos, cs), resultpos);
- }
- + // QStringRef overload
- + QCOMPARE( haystack.indexOf(QStringRef(&needle), startpos, cs), resultpos );
- + if (resultpos > 0 && needle.size() > 1)
- + QCOMPARE( haystack.indexOf(needle.midRef(1), startpos, cs), resultpos+1 );
- }
- void tst_QString::indexOf2_data()
- @@ -1527,6 +1535,77 @@ void tst_QString::stringRef()
- QVERIFY(alpha == hash.value(QStringRef(&s_alpha2)));
- }
- +void tst_QString::stringRef_leftRef()
- +{
- + QString str;
- + str = QLatin1String("Foo123Bar");
- +
- + QStringRef Foo123 = str.leftRef(6);
- + QCOMPARE(Foo123, QLatin1String("Foo123"));
- + QStringRef Foo = Foo123.leftRef(3);
- + QCOMPARE(Foo, QLatin1String("Foo"));
- +
- + QStringRef full = Foo123.leftRef(42);
- + QCOMPARE(full, Foo123);
- + QCOMPARE(full, QLatin1String("Foo123"));
- +}
- +
- +void tst_QString::stringRef_rightRef()
- +{
- + QString str;
- + str = QLatin1String("Foo123Bar");
- +
- + QStringRef s123Bar = str.rightRef(6);
- + QCOMPARE(s123Bar, QLatin1String("123Bar"));
- + QStringRef Bar = s123Bar.rightRef(3);
- + QCOMPARE(Bar, QLatin1String("Bar"));
- +
- + QStringRef full = s123Bar.rightRef(42);
- + QCOMPARE(full, s123Bar);
- + QCOMPARE(full, QLatin1String("123Bar"));
- +}
- +
- +void tst_QString::stringRef_midRef()
- +{
- + QString str;
- + str = QLatin1String("xFoo123Barx");
- +
- + QStringRef Foo123Bar = str.midRef(1, 9);
- + QCOMPARE(Foo123Bar, QLatin1String("Foo123Bar"));
- + QStringRef s123 = Foo123Bar.midRef(3, 3);
- + QCOMPARE(s123, QLatin1String("123"));
- + QStringRef s2 = s123.midRef(1, 1);
- + QCOMPARE(s2, QLatin1String("2"));
- + QStringRef invalid = s2.midRef(10, 1);
- + QVERIFY(invalid.isNull());
- +
- + QCOMPARE(Foo123Bar.midRef(3), QLatin1String("123Bar"));
- +}
- +
- +void tst_QString::stringRef_trimmed()
- +{
- + QString str;
- +
- + str = QLatin1String("Text");
- + QCOMPARE(QStringRef(&str, 0, 4).toString(), QLatin1String("Text"));
- + QCOMPARE(QStringRef(&str, 0, 4).trimmed().toString(), QLatin1String("Text"));
- + QCOMPARE(QStringRef(&str, 1, 2).trimmed().toString(), QLatin1String("ex"));
- +
- + str = QLatin1String(" ");
- + QCOMPARE(QStringRef(&str, 0, 1).trimmed().toString(), QLatin1String(""));
- + QCOMPARE(str, QLatin1String(" "));
- +
- + str = " a ";
- + QCOMPARE(QStringRef(&str, 0, 5).trimmed().toString(), QLatin1String("a"));
- +
- + str = " abc ";
- + QCOMPARE(QStringRef(&str, 0, 4).trimmed().toString(), QLatin1String("abc"));
- + QCOMPARE(QStringRef(&str, 1, 4).trimmed().toString(), QLatin1String("abc"));
- + QCOMPARE(QStringRef(&str, 0, 5).trimmed().toString(), QLatin1String("abc"));
- +}
- +
- +
- +
- void tst_QString::leftJustified()
- {
- QString a;
- @@ -3813,7 +3892,6 @@ void tst_QString::section()
- }
- }
- -
- void tst_QString::operator_eqeq_nullstring()
- {
- /* Some of these might not be all that logical but it's the behaviour we've had since 3.0.0
- --
- 1.7.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement