QVersionNumber: add fromString(QStringView/QLatin1String) overloads

The parsing code anyway operated on a QByteArray created from
toLatin1(), so expose this to the user by providing a QLatin1String
overload.

Also provide a QStringView overload, since we can. Port one user (in
qmake) to the new overload.

[ChangeLog][QtCore][QVersionNumber] Added QStringView and
QLatin1String overloads of fromString().

Change-Id: Idbff44c3997f5cfa86ea1bce8b3da4b700a3d9cc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2017-04-07 22:31:59 +02:00
parent f5d8ad61a4
commit de3785b8bc
5 changed files with 66 additions and 9 deletions

View File

@ -1569,7 +1569,7 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinConditional(
return ReturnFalse;
}
const QVersionNumber lvn = QVersionNumber::fromString(values(args.at(0).toKey()).join('.'));
const QVersionNumber rvn = QVersionNumber::fromString(args.at(1).toQString());
const QVersionNumber rvn = QVersionNumber::fromString(args.at(1).toQStringView());
if (func_t == T_VERSION_AT_LEAST)
return returnBool(lvn >= rvn);
return returnBool(lvn <= rvn);

View File

@ -95,6 +95,14 @@ void QObject::parse()
// version is 5.4.0
// suffixIndex is 5
//! [3]
//! [3-latin-1]
QLatin1String string("5.4.0-alpha");
int suffixIndex;
auto version = QVersionNumber::fromString(string, &suffixIndex);
// version is 5.4.0
// suffixIndex is 5
//! [3-latin-1]
}
void Object::equivalent()

View File

@ -406,10 +406,8 @@ QString QVersionNumber::toString() const
return version;
}
#if QT_STRINGVIEW_LEVEL < 2
/*!
\fn QVersionNumber QVersionNumber::fromString(const QString &string,
int *suffixIndex)
Constructs a QVersionNumber from a specially formatted \a string of
non-negative decimal numbers delimited by '.'.
@ -422,15 +420,54 @@ QString QVersionNumber::toString() const
\sa isNull()
*/
QVersionNumber QVersionNumber::fromString(const QString &string, int *suffixIndex)
{
return fromString(QLatin1String(string.toLatin1()), suffixIndex);
}
#endif
/*!
\since 5.10
\overload
Constructs a QVersionNumber from a specially formatted \a string of
non-negative decimal numbers delimited by '.'.
Once the numerical segments have been parsed, the remainder of the string
is considered to be the suffix string. The start index of that string will be
stored in \a suffixIndex if it is not null.
\snippet qversionnumber/main.cpp 3
\sa isNull()
*/
QVersionNumber QVersionNumber::fromString(QStringView string, int *suffixIndex)
{
return fromString(QLatin1String(string.toLatin1()), suffixIndex);
}
/*!
\since 5.10
\overload
Constructs a QVersionNumber from a specially formatted \a string of
non-negative decimal numbers delimited by '.'.
Once the numerical segments have been parsed, the remainder of the string
is considered to be the suffix string. The start index of that string will be
stored in \a suffixIndex if it is not null.
\snippet qversionnumber/main.cpp 3-latin1-1
\sa isNull()
*/
QVersionNumber QVersionNumber::fromString(QLatin1String string, int *suffixIndex)
{
QVector<int> seg;
const QByteArray cString(string.toLatin1());
const char *start = cString.constData();
const char *start = string.begin();
const char *end = start;
const char *lastGoodEnd = start;
const char *endOfString = cString.constData() + cString.size();
const char *endOfString = string.end();
do {
bool ok = false;
@ -443,7 +480,7 @@ QVersionNumber QVersionNumber::fromString(const QString &string, int *suffixInde
} while (start < endOfString && (end < endOfString && *end == '.'));
if (suffixIndex)
*suffixIndex = int(lastGoodEnd - cString.constData());
*suffixIndex = int(lastGoodEnd - string.begin());
return QVersionNumber(qMove(seg));
}

View File

@ -280,7 +280,11 @@ public:
Q_CORE_EXPORT static Q_DECL_PURE_FUNCTION QVersionNumber commonPrefix(const QVersionNumber &v1, const QVersionNumber &v2) Q_REQUIRED_RESULT;
Q_CORE_EXPORT QString toString() const Q_REQUIRED_RESULT;
#if QT_STRINGVIEW_LEVEL < 2
Q_CORE_EXPORT static Q_DECL_PURE_FUNCTION QVersionNumber fromString(const QString &string, int *suffixIndex = Q_NULLPTR) Q_REQUIRED_RESULT;
#endif
Q_CORE_EXPORT static Q_DECL_PURE_FUNCTION QVersionNumber fromString(QLatin1String string, int *suffixIndex = nullptr) Q_REQUIRED_RESULT;
Q_CORE_EXPORT static Q_DECL_PURE_FUNCTION QVersionNumber fromString(QStringView string, int *suffixIndex = nullptr) Q_REQUIRED_RESULT;
private:
#ifndef QT_NO_DATASTREAM

View File

@ -513,6 +513,14 @@ void tst_QVersionNumber::fromString()
QCOMPARE(QVersionNumber::fromString(constructionString), expectedVersion);
QCOMPARE(QVersionNumber::fromString(constructionString, &index), expectedVersion);
QCOMPARE(index, suffixIndex);
QCOMPARE(QVersionNumber::fromString(QStringView(constructionString)), expectedVersion);
QCOMPARE(QVersionNumber::fromString(QStringView(constructionString), &index), expectedVersion);
QCOMPARE(index, suffixIndex);
QCOMPARE(QVersionNumber::fromString(QLatin1String(constructionString.toLatin1())), expectedVersion);
QCOMPARE(QVersionNumber::fromString(QLatin1String(constructionString.toLatin1()), &index), expectedVersion);
QCOMPARE(index, suffixIndex);
}
void tst_QVersionNumber::toString_data()