diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-03-28 19:31:45 -0300 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-30 01:19:59 +0200 |
commit | 66df11f4d109ca3d97fed8985d6bbc6dcf90733d (patch) | |
tree | 0c514e0c7b03beea92403baaa380c6158cead84f /src/corelib/io | |
parent | 64a10879cb1f3a48b4b44c2e3a46694efb3bec0a (diff) |
Fix QUrl operator== and operator<
Don't crash when either side is null but not both sides.
Also make sure operator< is working properly and satisfies the basic
conditions of a type (such as that if A < B, then !(B < A)).
Change-Id: Idd9e9fc593e1a7781d9f4f2b13a1024b643926fd
Reviewed-by: Giuseppe D'Angelo <dangelog@gmail.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
Diffstat (limited to 'src/corelib/io')
-rw-r--r-- | src/corelib/io/qurl.cpp | 64 | ||||
-rw-r--r-- | src/corelib/io/qurl_p.h | 2 |
2 files changed, 46 insertions, 20 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index a83d6ebea4..a716369594 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -2197,24 +2197,44 @@ QByteArray QUrl::toAce(const QString &domain) */ bool QUrl::operator <(const QUrl &url) const { - if (!d) return url.d; - if (d->scheme < url.d->scheme) - return true; - if (d->userName < url.d->userName) - return true; - if (d->password < url.d->password) - return true; - if (d->host < url.d->host) - return true; - if (d->port < url.d->port) - return true; - if (d->path < url.d->path) - return true; - if (d->query < url.d->query) - return true; - if (d->fragment < url.d->fragment) - return true; - return false; + if (!d || !url.d) { + bool thisIsEmpty = !d || d->isEmpty(); + bool thatIsEmpty = !url.d || url.d->isEmpty(); + + // sort an empty URL first + return thisIsEmpty && !thatIsEmpty; + } + + int cmp; + cmp = d->scheme.compare(url.d->scheme); + if (cmp != 0) + return cmp < 0; + + cmp = d->userName.compare(url.d->userName); + if (cmp != 0) + return cmp < 0; + + cmp = d->password.compare(url.d->password); + if (cmp != 0) + return cmp < 0; + + cmp = d->host.compare(url.d->host); + if (cmp != 0) + return cmp < 0; + + if (d->port != url.d->port) + return d->port < url.d->port; + + cmp = d->path.compare(url.d->path); + if (cmp != 0) + return cmp < 0; + + cmp = d->query.compare(url.d->query); + if (cmp != 0) + return cmp < 0; + + cmp = d->fragment.compare(url.d->fragment); + return cmp < 0; } /*! @@ -2223,8 +2243,12 @@ bool QUrl::operator <(const QUrl &url) const */ bool QUrl::operator ==(const QUrl &url) const { - if (!d || !url.d) - return d == url.d; + if (!d && !url.d) + return true; + if (!d) + return url.d->isEmpty(); + if (!url.d) + return d->isEmpty(); return d->scheme == url.d->scheme && d->userName == url.d->userName && d->password == url.d->password && diff --git a/src/corelib/io/qurl_p.h b/src/corelib/io/qurl_p.h index fb54d74260..2333809c12 100644 --- a/src/corelib/io/qurl_p.h +++ b/src/corelib/io/qurl_p.h @@ -107,6 +107,8 @@ public: void parse(const QString &url, QUrl::ParsingMode parsingMode); void clear(); + bool isEmpty() const + { return sectionIsPresent == 0 && port == -1 && path.isEmpty(); } // no QString scheme() const; void appendAuthority(QString &appendTo, QUrl::FormattingOptions options) const; |