From 8103d29e94f90131ee0ad69fc312c1e62e74a6a9 Mon Sep 17 00:00:00 2001 From: Rym Bouabid Date: Tue, 20 Feb 2024 16:54:53 +0100 Subject: QUrl: Use new comparison helper macros The class had operator==(), operator!=() and operator <() defined as public member functions, so use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of these methods and replace them with hidden friends. Use QT_TEST_ALL_EQUALITY_OPS macro in unit-tests. Use new \compares command in the documentation to describe the comparison operators provided by QUrl. Task-number: QTBUG-120303 Change-Id: Ic4fa2335292cc4b75ad2373832c0b89d768f529c Reviewed-by: Thiago Macieira --- src/corelib/io/qurl.cpp | 109 +++++++++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 48 deletions(-) (limited to 'src/corelib/io/qurl.cpp') diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index fdafd070df..4360b5b076 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -14,6 +14,8 @@ \ingroup network \ingroup shared + \compares weak + It can parse and construct URLs in both encoded and unencoded form. QUrl also has support for internationalized domain names (IDNs). @@ -3067,88 +3069,101 @@ QByteArray QUrl::toAce(const QString &domain, AceProcessingOptions options) /*! \internal - Returns \c true if this URL is "less than" the given \a url. This + \fn bool QUrl::operator<(const QUrl &lhs, const QUrl &rhs) + + Returns \c true if URL \a lhs is "less than" URL \a rhs. This provides a means of ordering URLs. */ -bool QUrl::operator <(const QUrl &url) const + +Qt::weak_ordering compareThreeWay(const QUrl &lhs, const QUrl &rhs) { - if (!d || !url.d) { - bool thisIsEmpty = !d || d->isEmpty(); - bool thatIsEmpty = !url.d || url.d->isEmpty(); + if (!lhs.d || !rhs.d) { + bool thisIsEmpty = !lhs.d || lhs.d->isEmpty(); + bool thatIsEmpty = !rhs.d || rhs.d->isEmpty(); // sort an empty URL first - return thisIsEmpty && !thatIsEmpty; + if (thisIsEmpty) { + if (!thatIsEmpty) + return Qt::weak_ordering::less; + else + return Qt::weak_ordering::equivalent; + } else { + return Qt::weak_ordering::greater; + } } int cmp; - cmp = d->scheme.compare(url.d->scheme); + cmp = lhs.d->scheme.compare(rhs.d->scheme); if (cmp != 0) - return cmp < 0; + return Qt::compareThreeWay(cmp, 0); - cmp = d->userName.compare(url.d->userName); + cmp = lhs.d->userName.compare(rhs.d->userName); if (cmp != 0) - return cmp < 0; + return Qt::compareThreeWay(cmp, 0); - cmp = d->password.compare(url.d->password); + cmp = lhs.d->password.compare(rhs.d->password); if (cmp != 0) - return cmp < 0; + return Qt::compareThreeWay(cmp, 0); - cmp = d->host.compare(url.d->host); + cmp = lhs.d->host.compare(rhs.d->host); if (cmp != 0) - return cmp < 0; + return Qt::compareThreeWay(cmp, 0); - if (d->port != url.d->port) - return d->port < url.d->port; + if (lhs.d->port != rhs.d->port) + return Qt::compareThreeWay(lhs.d->port, rhs.d->port); - cmp = d->path.compare(url.d->path); + cmp = lhs.d->path.compare(rhs.d->path); if (cmp != 0) - return cmp < 0; + return Qt::compareThreeWay(cmp, 0); - if (d->hasQuery() != url.d->hasQuery()) - return url.d->hasQuery(); + if (lhs.d->hasQuery() != rhs.d->hasQuery()) + return rhs.d->hasQuery() ? Qt::weak_ordering::less : Qt::weak_ordering::greater; - cmp = d->query.compare(url.d->query); + cmp = lhs.d->query.compare(rhs.d->query); if (cmp != 0) - return cmp < 0; + return Qt::compareThreeWay(cmp, 0); - if (d->hasFragment() != url.d->hasFragment()) - return url.d->hasFragment(); + if (lhs.d->hasFragment() != rhs.d->hasFragment()) + return rhs.d->hasFragment() ? Qt::weak_ordering::less : Qt::weak_ordering::greater; - cmp = d->fragment.compare(url.d->fragment); - return cmp < 0; + cmp = lhs.d->fragment.compare(rhs.d->fragment); + return Qt::compareThreeWay(cmp, 0); } /*! - Returns \c true if this URL and the given \a url are equal; + \fn bool QUrl::operator==(const QUrl &lhs, const QUrl &rhs) + + Returns \c true if \a lhs and \a rhs URLs are equivalent; otherwise returns \c false. \sa matches() */ -bool QUrl::operator ==(const QUrl &url) const + +bool comparesEqual(const QUrl &lhs, const QUrl &rhs) { - if (!d && !url.d) + if (!lhs.d && !rhs.d) return true; - if (!d) - return url.d->isEmpty(); - if (!url.d) - return d->isEmpty(); + if (!lhs.d) + return rhs.d->isEmpty(); + if (!rhs.d) + return lhs.d->isEmpty(); // First, compare which sections are present, since it speeds up the // processing considerably. We just have to ignore the host-is-present flag // for local files (the "file" protocol), due to the requirements of the // XDG file URI specification. int mask = QUrlPrivate::FullUrl; - if (isLocalFile()) + if (lhs.isLocalFile()) mask &= ~QUrlPrivate::Host; - return (d->sectionIsPresent & mask) == (url.d->sectionIsPresent & mask) && - d->scheme == url.d->scheme && - d->userName == url.d->userName && - d->password == url.d->password && - d->host == url.d->host && - d->port == url.d->port && - d->path == url.d->path && - d->query == url.d->query && - d->fragment == url.d->fragment; + return (lhs.d->sectionIsPresent & mask) == (rhs.d->sectionIsPresent & mask) && + lhs.d->scheme == rhs.d->scheme && + lhs.d->userName == rhs.d->userName && + lhs.d->password == rhs.d->password && + lhs.d->host == rhs.d->host && + lhs.d->port == rhs.d->port && + lhs.d->path == rhs.d->path && + lhs.d->query == rhs.d->query && + lhs.d->fragment == rhs.d->fragment; } /*! @@ -3228,15 +3243,13 @@ bool QUrl::matches(const QUrl &url, FormattingOptions options) const } /*! - Returns \c true if this URL and the given \a url are not equal; + \fn bool QUrl::operator !=(const QUrl &lhs, const QUrl &rhs) + + Returns \c true if \a lhs and \a rhs URLs are not equal; otherwise returns \c false. \sa matches() */ -bool QUrl::operator !=(const QUrl &url) const -{ - return !(*this == url); -} /*! Assigns the specified \a url to this object. -- cgit v1.2.3