diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-04-07 22:31:59 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-04-08 05:40:52 +0000 |
commit | de3785b8bc4eeec403835840b0328b5256854ea4 (patch) | |
tree | ba680f1110a03f9319137dbc0035556b4b711407 /src/corelib/tools | |
parent | f5d8ad61a4c85a656a7332c43d0c42f5eaf43593 (diff) |
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>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qversionnumber.cpp | 53 | ||||
-rw-r--r-- | src/corelib/tools/qversionnumber.h | 4 |
2 files changed, 49 insertions, 8 deletions
diff --git a/src/corelib/tools/qversionnumber.cpp b/src/corelib/tools/qversionnumber.cpp index 97e5da8b3c..0f237bce87 100644 --- a/src/corelib/tools/qversionnumber.cpp +++ b/src/corelib/tools/qversionnumber.cpp @@ -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 '.'. @@ -423,14 +421,53 @@ QString QVersionNumber::toString() const */ QVersionNumber QVersionNumber::fromString(const QString &string, int *suffixIndex) { - QVector<int> seg; + 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 - const QByteArray cString(string.toLatin1()); + 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 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)); } diff --git a/src/corelib/tools/qversionnumber.h b/src/corelib/tools/qversionnumber.h index 2aaa6201f7..a7f91e220c 100644 --- a/src/corelib/tools/qversionnumber.h +++ b/src/corelib/tools/qversionnumber.h @@ -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 |