diff options
author | Thiago Macieira <thiago.macieira@nokia.com> | 2010-01-07 09:23:03 (GMT) |
---|---|---|
committer | Thiago Macieira <thiago.macieira@nokia.com> | 2010-01-07 09:33:32 (GMT) |
commit | 51f92c1a4f66f658b3c3b7f034bb8dd661a0d57d (patch) | |
tree | 3f8dbd23cb75bd86857d240d29454b9f0f1aaa04 /src/corelib | |
parent | b63cfd1d90dfd8be70c49c409b4f6f1cca176735 (diff) | |
download | Qt-51f92c1a4f66f658b3c3b7f034bb8dd661a0d57d.zip Qt-51f92c1a4f66f658b3c3b7f034bb8dd661a0d57d.tar.gz Qt-51f92c1a4f66f658b3c3b7f034bb8dd661a0d57d.tar.bz2 |
Improve QUrl constructing performance by making empty URLs have d==0.
One major cost is that QUrl::d is never 0 (even for default
constructor). While this simplifies the code, it has a real
performance impact, since QUrlPrivate is quite expensive to create (14
QString/QByteArray members, plus others). In a simple test of the QML
flickr demo, 1 in 3 QUrlPrivate objects that were created were never
used before being deleted.
According to callgrind, this reduces cost of QUrl+QUrlPrivate by 25%
in the case tested.
Patch-by: Warwick Allison
Reviewed-by: Thiago Macieira
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/io/qurl.cpp | 134 |
1 files changed, 119 insertions, 15 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 59256b0..1882e92 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -4074,7 +4074,7 @@ QString QUrlPrivate::createErrorString() \sa setUrl(), setEncodedUrl(), fromEncoded(), TolerantMode */ -QUrl::QUrl(const QString &url) : d(new QUrlPrivate) +QUrl::QUrl(const QString &url) : d(0) { if (!url.isEmpty()) setUrl(url); @@ -4087,18 +4087,20 @@ QUrl::QUrl(const QString &url) : d(new QUrlPrivate) \sa setUrl() */ -QUrl::QUrl(const QString &url, ParsingMode parsingMode) : d(new QUrlPrivate) +QUrl::QUrl(const QString &url, ParsingMode parsingMode) : d(0) { if (!url.isEmpty()) setUrl(url, parsingMode); - else + else { + d = new QUrlPrivate; d->parsingMode = parsingMode; + } } /*! Constructs an empty QUrl object. */ -QUrl::QUrl() : d(new QUrlPrivate) +QUrl::QUrl() : d(0) { } @@ -4107,7 +4109,8 @@ QUrl::QUrl() : d(new QUrlPrivate) */ QUrl::QUrl(const QUrl &other) : d(other.d) { - d->ref.ref(); + if (d) + d->ref.ref(); } /*! @@ -4115,7 +4118,7 @@ QUrl::QUrl(const QUrl &other) : d(other.d) */ QUrl::~QUrl() { - if (!d->ref.deref()) + if (d && !d->ref.deref()) delete d; } @@ -4130,6 +4133,8 @@ QUrl::~QUrl() */ bool QUrl::isValid() const { + if (!d) return false; + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Validated)) d->validate(); @@ -4141,6 +4146,8 @@ bool QUrl::isValid() const */ bool QUrl::isEmpty() const { + if (!d) return true; + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) return d->encodedOriginal.isEmpty(); else @@ -4161,8 +4168,9 @@ bool QUrl::isEmpty() const */ void QUrl::clear() { - detach(); - d->clear(); + if (d && !d->ref.deref()) + delete d; + d = 0; } /*! @@ -4261,8 +4269,9 @@ static inline char toHex(quint8 c) */ void QUrl::setEncodedUrl(const QByteArray &encodedUrl, ParsingMode parsingMode) { - clear(); QByteArray tmp = encodedUrl; + if (!d) d = new QUrlPrivate; + else d->clear(); if ((d->parsingMode = parsingMode) == TolerantMode) { // Replace stray % with %25 QByteArray copy = tmp; @@ -4336,6 +4345,7 @@ void QUrl::setEncodedUrl(const QByteArray &encodedUrl, ParsingMode parsingMode) */ void QUrl::setScheme(const QString &scheme) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4351,6 +4361,7 @@ void QUrl::setScheme(const QString &scheme) */ QString QUrl::scheme() const { + if (!d) return QString(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->scheme; @@ -4374,6 +4385,8 @@ QString QUrl::scheme() const */ void QUrl::setAuthority(const QString &authority) { + if (!d) d = new QUrlPrivate; + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4389,6 +4402,8 @@ void QUrl::setAuthority(const QString &authority) */ QString QUrl::authority() const { + if (!d) return QString(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->authority(); @@ -4409,6 +4424,8 @@ QString QUrl::authority() const */ void QUrl::setUserInfo(const QString &userInfo) { + if (!d) d = new QUrlPrivate; + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4422,6 +4439,8 @@ void QUrl::setUserInfo(const QString &userInfo) */ QString QUrl::userInfo() const { + if (!d) return QString(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->userInfo(); @@ -4436,6 +4455,8 @@ QString QUrl::userInfo() const */ void QUrl::setUserName(const QString &userName) { + if (!d) d = new QUrlPrivate; + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4452,6 +4473,8 @@ void QUrl::setUserName(const QString &userName) */ QString QUrl::userName() const { + if (!d) return QString(); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->userInfo(); // causes the unencoded form to be set @@ -4473,6 +4496,7 @@ QString QUrl::userName() const */ void QUrl::setEncodedUserName(const QByteArray &userName) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4493,6 +4517,7 @@ void QUrl::setEncodedUserName(const QByteArray &userName) */ QByteArray QUrl::encodedUserName() const { + if (!d) return QByteArray(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->ensureEncodedParts(); @@ -4508,6 +4533,7 @@ QByteArray QUrl::encodedUserName() const */ void QUrl::setPassword(const QString &password) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4524,6 +4550,7 @@ void QUrl::setPassword(const QString &password) */ QString QUrl::password() const { + if (!d) return QString(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->userInfo(); // causes the unencoded form to be set @@ -4545,6 +4572,7 @@ QString QUrl::password() const */ void QUrl::setEncodedPassword(const QByteArray &password) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4565,6 +4593,7 @@ void QUrl::setEncodedPassword(const QByteArray &password) */ QByteArray QUrl::encodedPassword() const { + if (!d) return QByteArray(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->ensureEncodedParts(); @@ -4579,6 +4608,7 @@ QByteArray QUrl::encodedPassword() const */ void QUrl::setHost(const QString &host) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized | QUrlPrivate::HostCanonicalized); @@ -4592,6 +4622,7 @@ void QUrl::setHost(const QString &host) */ QString QUrl::host() const { + if (!d) return QString(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); if (d->host.isEmpty() || d->host.at(0) != QLatin1Char('[')) @@ -4645,6 +4676,7 @@ QByteArray QUrl::encodedHost() const */ void QUrl::setPort(int port) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4662,6 +4694,7 @@ void QUrl::setPort(int port) */ int QUrl::port() const { + if (!d) return -1; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Validated)) d->validate(); return d->port; @@ -4680,6 +4713,7 @@ int QUrl::port() const */ int QUrl::port(int defaultPort) const { + if (!d) return defaultPort; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->port == -1 ? defaultPort : d->port; } @@ -4699,6 +4733,7 @@ int QUrl::port(int defaultPort) const */ void QUrl::setPath(const QString &path) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4714,6 +4749,7 @@ void QUrl::setPath(const QString &path) */ QString QUrl::path() const { + if (!d) return QString(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); if (d->path.isNull()) { @@ -4745,6 +4781,7 @@ QString QUrl::path() const */ void QUrl::setEncodedPath(const QByteArray &path) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4765,6 +4802,7 @@ void QUrl::setEncodedPath(const QByteArray &path) */ QByteArray QUrl::encodedPath() const { + if (!d) return QByteArray(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->ensureEncodedParts(); @@ -4780,6 +4818,7 @@ QByteArray QUrl::encodedPath() const */ bool QUrl::hasQuery() const { + if (!d) return false; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->hasQuery; @@ -4809,6 +4848,7 @@ bool QUrl::hasQuery() const */ void QUrl::setQueryDelimiters(char valueDelimiter, char pairDelimiter) { + if (!d) d = new QUrlPrivate; detach(); d->valueDelimiter = valueDelimiter; @@ -4821,6 +4861,7 @@ void QUrl::setQueryDelimiters(char valueDelimiter, char pairDelimiter) */ char QUrl::queryPairDelimiter() const { + if (!d) return '&'; return d->pairDelimiter; } @@ -4830,6 +4871,7 @@ char QUrl::queryPairDelimiter() const */ char QUrl::queryValueDelimiter() const { + if (!d) return '='; return d->valueDelimiter; } @@ -4852,6 +4894,7 @@ char QUrl::queryValueDelimiter() const */ void QUrl::setEncodedQuery(const QByteArray &query) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -4871,6 +4914,7 @@ void QUrl::setEncodedQuery(const QByteArray &query) */ void QUrl::setQueryItems(const QList<QPair<QString, QString> > &query) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); @@ -4910,6 +4954,7 @@ void QUrl::setQueryItems(const QList<QPair<QString, QString> > &query) */ void QUrl::setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &query) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); @@ -4939,6 +4984,7 @@ void QUrl::setEncodedQueryItems(const QList<QPair<QByteArray, QByteArray> > &que */ void QUrl::addQueryItem(const QString &key, const QString &value) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); @@ -4973,6 +5019,7 @@ void QUrl::addQueryItem(const QString &key, const QString &value) */ void QUrl::addEncodedQueryItem(const QByteArray &key, const QByteArray &value) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); @@ -4993,6 +5040,7 @@ void QUrl::addEncodedQueryItem(const QByteArray &key, const QByteArray &value) */ QList<QPair<QString, QString> > QUrl::queryItems() const { + if (!d) return QList<QPair<QString, QString> >(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); QList<QPair<QString, QString> > itemMap; @@ -5025,6 +5073,7 @@ QList<QPair<QString, QString> > QUrl::queryItems() const */ QList<QPair<QByteArray, QByteArray> > QUrl::encodedQueryItems() const { + if (!d) return QList<QPair<QByteArray, QByteArray> >(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); QList<QPair<QByteArray, QByteArray> > itemMap; @@ -5053,6 +5102,7 @@ QList<QPair<QByteArray, QByteArray> > QUrl::encodedQueryItems() const */ bool QUrl::hasQueryItem(const QString &key) const { + if (!d) return false; return hasEncodedQueryItem(toPercentEncoding(key, queryExcludeChars)); } @@ -5071,6 +5121,7 @@ bool QUrl::hasQueryItem(const QString &key) const */ bool QUrl::hasEncodedQueryItem(const QByteArray &key) const { + if (!d) return false; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); int pos = 0; @@ -5093,6 +5144,7 @@ bool QUrl::hasEncodedQueryItem(const QByteArray &key) const */ QString QUrl::queryItemValue(const QString &key) const { + if (!d) return QString(); QByteArray tmp = encodedQueryItemValue(toPercentEncoding(key, queryExcludeChars)); return fromPercentEncodingMutable(&tmp); } @@ -5112,6 +5164,7 @@ QString QUrl::queryItemValue(const QString &key) const */ QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const { + if (!d) return QByteArray(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); int pos = 0; @@ -5135,6 +5188,7 @@ QByteArray QUrl::encodedQueryItemValue(const QByteArray &key) const */ QStringList QUrl::allQueryItemValues(const QString &key) const { + if (!d) return QStringList(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); QByteArray encodedKey = toPercentEncoding(key, queryExcludeChars); @@ -5172,6 +5226,7 @@ QStringList QUrl::allQueryItemValues(const QString &key) const */ QList<QByteArray> QUrl::allEncodedQueryItemValues(const QByteArray &key) const { + if (!d) return QList<QByteArray>(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); QList<QByteArray> values; @@ -5199,6 +5254,7 @@ QList<QByteArray> QUrl::allEncodedQueryItemValues(const QByteArray &key) const */ void QUrl::removeQueryItem(const QString &key) { + if (!d) return; removeEncodedQueryItem(toPercentEncoding(key, queryExcludeChars)); } @@ -5217,6 +5273,7 @@ void QUrl::removeQueryItem(const QString &key) */ void QUrl::removeEncodedQueryItem(const QByteArray &key) { + if (!d) return; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); @@ -5243,6 +5300,7 @@ void QUrl::removeEncodedQueryItem(const QByteArray &key) */ void QUrl::removeAllQueryItems(const QString &key) { + if (!d) return; removeAllEncodedQueryItems(toPercentEncoding(key, queryExcludeChars)); } @@ -5261,6 +5319,7 @@ void QUrl::removeAllQueryItems(const QString &key) */ void QUrl::removeAllEncodedQueryItems(const QByteArray &key) { + if (!d) return; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); @@ -5284,6 +5343,7 @@ void QUrl::removeAllEncodedQueryItems(const QByteArray &key) */ QByteArray QUrl::encodedQuery() const { + if (!d) return QByteArray(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->query; @@ -5308,6 +5368,7 @@ QByteArray QUrl::encodedQuery() const */ void QUrl::setFragment(const QString &fragment) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -5324,6 +5385,7 @@ void QUrl::setFragment(const QString &fragment) */ QString QUrl::fragment() const { + if (!d) return QString(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); if (d->fragment.isNull() && !d->encodedFragment.isNull()) { @@ -5354,6 +5416,7 @@ QString QUrl::fragment() const */ void QUrl::setEncodedFragment(const QByteArray &fragment) { + if (!d) d = new QUrlPrivate; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); detach(); QURL_UNSETFLAG(d->stateFlags, QUrlPrivate::Validated | QUrlPrivate::Normalized); @@ -5375,6 +5438,7 @@ void QUrl::setEncodedFragment(const QByteArray &fragment) */ QByteArray QUrl::encodedFragment() const { + if (!d) return QByteArray(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); d->ensureEncodedParts(); @@ -5390,6 +5454,7 @@ QByteArray QUrl::encodedFragment() const */ bool QUrl::hasFragment() const { + if (!d) return false; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->hasFragment; @@ -5416,6 +5481,8 @@ bool QUrl::hasFragment() const */ QUrl QUrl::resolved(const QUrl &relative) const { + if (!d) return relative; + if (!relative.d) return *this; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); if (!QURL_HASFLAG(relative.d->stateFlags, QUrlPrivate::Parsed)) @@ -5432,6 +5499,7 @@ QUrl QUrl::resolved(const QUrl &relative) const if (!relative.authority().isEmpty()) { t = relative; } else { + t.d = new QUrlPrivate; if (relative.d->encodedPath.isEmpty()) { t.d->encodedPath = d->encodedPath; t.setEncodedQuery(relative.d->hasQuery ? relative.d->query : d->query); @@ -5462,6 +5530,7 @@ QUrl QUrl::resolved(const QUrl &relative) const */ bool QUrl::isRelative() const { + if (!d) return true; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); return d->scheme.isEmpty(); @@ -5476,6 +5545,7 @@ bool QUrl::isRelative() const */ QString QUrl::toString(FormattingOptions options) const { + if (!d) return QString(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); QString url; @@ -5527,6 +5597,7 @@ QString QUrl::toString(FormattingOptions options) const */ QByteArray QUrl::toEncoded(FormattingOptions options) const { + if (!d) return QByteArray(); return d->toEncoded(options); } @@ -5777,7 +5848,9 @@ void QUrl::setIdnWhitelist(const QStringList &list) */ bool QUrl::operator <(const QUrl &url) const { + if (!d) return url.d ? QByteArray() < url.d->normalized() : false; if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); + if (!url.d) return d->normalized() < QByteArray(); if (!QURL_HASFLAG(url.d->stateFlags, QUrlPrivate::Parsed)) url.d->parse(); return d->normalized() < url.d->normalized(); } @@ -5788,6 +5861,8 @@ bool QUrl::operator <(const QUrl &url) const */ bool QUrl::operator ==(const QUrl &url) const { + if (!d) return url.isEmpty(); + if (!url.d) return isEmpty(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); if (!QURL_HASFLAG(url.d->stateFlags, QUrlPrivate::Parsed)) url.d->parse(); return d->normalized() == url.d->normalized(); @@ -5807,7 +5882,17 @@ bool QUrl::operator !=(const QUrl &url) const */ QUrl &QUrl::operator =(const QUrl &url) { - qAtomicAssign(d, url.d); + if (!d) { + if (url.d) { + url.d->ref.ref(); + d = url.d; + } + } else { + if (url.d) + qAtomicAssign(d, url.d); + else + clear(); + } return *this; } @@ -5816,8 +5901,13 @@ QUrl &QUrl::operator =(const QUrl &url) */ QUrl &QUrl::operator =(const QString &url) { - QUrl tmp(url); - qAtomicAssign(d, tmp.d); + if (url.isEmpty()) { + clear(); + } else { + QUrl tmp(url); + if (!d) d = new QUrlPrivate; + qAtomicAssign(d, tmp.d); + } return *this; } @@ -5826,14 +5916,19 @@ QUrl &QUrl::operator =(const QString &url) Forces a detach. */ void QUrl::detach() -{ qAtomicDetach(d); } +{ + if (!d) + d = new QUrlPrivate; + else + qAtomicDetach(d); +} /*! \internal */ bool QUrl::isDetached() const { - return d->ref == 1; + return !d || d->ref == 1; } @@ -5875,6 +5970,7 @@ QUrl QUrl::fromLocalFile(const QString &localFile) */ QString QUrl::toLocalFile() const { + if (!d) return QString(); if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); QString tmp; @@ -5903,9 +5999,15 @@ QString QUrl::toLocalFile() const */ bool QUrl::isParentOf(const QUrl &childUrl) const { + QString childPath = childUrl.path(); + + if (!d) + return ((childUrl.scheme().isEmpty()) + && (childUrl.authority().isEmpty()) + && childPath.length() > 0 && childPath.at(0) == QLatin1Char('/')); + if (!QURL_HASFLAG(d->stateFlags, QUrlPrivate::Parsed)) d->parse(); - QString childPath = childUrl.path(); QString ourPath = path(); return ((childUrl.scheme().isEmpty() || d->scheme == childUrl.scheme()) @@ -6145,6 +6247,8 @@ QDebug operator<<(QDebug d, const QUrl &url) */ QString QUrl::errorString() const { + if (!d) + return QLatin1String(QT_TRANSLATE_NOOP(QUrl, "Invalid URL \"\": ")); // XXX not a good message, but the one an empty URL produces return d->createErrorString(); } |