summaryrefslogtreecommitdiffstats
path: root/src/corelib/io
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-03-30 17:48:42 -0300
committerQt by Nokia <qt-info@nokia.com>2012-04-11 23:32:26 +0200
commit1b7e9dba75f18342911bc6954be3e754322f091f (patch)
tree1dfb775910294ff8d02e60cfa9c2bf178baa6221 /src/corelib/io
parent997ac954abe8e4f7d9323f13a79989f277de8301 (diff)
Change the component formatting enum values so the default is zero
By having the default value equal to zero, we follow the principle of least surprise. For example, if we had url.path() and we refactored to url.path(QUrl::DecodeSpaces) Then instead of ensuring spaces are decoded, we make spaces the only thing encoded (unicode, delimiters and reserved characters are encoded). Besides, modifying the default can only be used to encode something that wasn't encoded previously, so having the enums as Encode makes more sense. As a side-effect, toEncoded() does not support any extra encoding options. Change-Id: I2624ec446e65c2d979e9ca2f81bd3db22b00bb13 Reviewed-by: Shane Kearns <shane.kearns@accenture.com>
Diffstat (limited to 'src/corelib/io')
-rw-r--r--src/corelib/io/qurl.cpp58
-rw-r--r--src/corelib/io/qurl.h20
-rw-r--r--src/corelib/io/qurlquery.cpp7
-rw-r--r--src/corelib/io/qurlrecode.cpp39
4 files changed, 77 insertions, 47 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp
index cbe39e9fdf..44d5533364 100644
--- a/src/corelib/io/qurl.cpp
+++ b/src/corelib/io/qurl.cpp
@@ -467,16 +467,16 @@ static inline void appendToUser(QString &appendTo, const QString &value, QUrl::F
}
const ushort *actions = 0;
- if (options & QUrl::DecodeDelimiters)
- actions = decodedActions;
- else
+ if (options & QUrl::EncodeDelimiters)
actions = encodedActions;
+ else
+ actions = decodedActions;
if (!qt_urlRecode(appendTo, value.constData(), value.constEnd(), options, actions))
appendTo += value;
}
-void QUrlPrivate::appendAuthority(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
+inline void QUrlPrivate::appendAuthority(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
{
if ((options & QUrl::RemoveUserInfo) != QUrl::RemoveUserInfo) {
appendUserInfo(appendTo, options, appendingTo);
@@ -488,14 +488,17 @@ void QUrlPrivate::appendAuthority(QString &appendTo, QUrl::FormattingOptions opt
appendTo += QLatin1Char(':') + QString::number(port);
}
-void QUrlPrivate::appendUserInfo(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
+inline void QUrlPrivate::appendUserInfo(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
{
if (Q_LIKELY(userName.isEmpty() && password.isEmpty()))
return;
const ushort *userNameActions;
const ushort *passwordActions;
- if (options & QUrl::DecodeDelimiters) {
+ if (options & QUrl::EncodeDelimiters) {
+ userNameActions = encodedUserNameActions;
+ passwordActions = encodedPasswordActions;
+ } else {
switch (appendingTo) {
case UserInfo:
userNameActions = decodedUserNameInUserInfoActions;
@@ -513,11 +516,11 @@ void QUrlPrivate::appendUserInfo(QString &appendTo, QUrl::FormattingOptions opti
passwordActions = decodedPasswordInUrlActions;
break;
}
- } else {
- userNameActions = encodedUserNameActions;
- passwordActions = encodedPasswordActions;
}
+ if ((options & QUrl::EncodeReserved) == 0)
+ options |= QUrl::DecodeReserved;
+
if (!qt_urlRecode(appendTo, userName.constData(), userName.constEnd(), options, userNameActions))
appendTo += userName;
if (options & QUrl::RemovePassword || !hasPassword()) {
@@ -541,7 +544,7 @@ inline void QUrlPrivate::appendPassword(QString &appendTo, QUrl::FormattingOptio
inline void QUrlPrivate::appendPath(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
{
- if (appendingTo != Path && options & QUrl::DecodeDelimiters) {
+ if (appendingTo != Path && !(options & QUrl::EncodeDelimiters)) {
if (!qt_urlRecode(appendTo, path.constData(), path.constEnd(), options, decodedPathInUrlActions))
appendTo += path;
@@ -565,12 +568,12 @@ inline void QUrlPrivate::appendQuery(QString &appendTo, QUrl::FormattingOptions
}
const ushort *actions = 0;
- if (options & QUrl::DecodeDelimiters) {
+ if (options & QUrl::EncodeDelimiters) {
+ actions = encodedQueryActions;
+ } else {
// reset to default qt_urlRecode behaviour (leave delimiters alone)
- options &= ~QUrl::DecodeDelimiters;
+ options |= QUrl::EncodeDelimiters;
actions = appendingTo == Query ? decodedQueryInIsolationActions : decodedQueryInUrlActions;
- } else if ((options & QUrl::DecodeDelimiters) == 0) {
- actions = encodedQueryActions;
}
if (!qt_urlRecode(appendTo, query.constData(), query.constData() + query.length(),
@@ -764,7 +767,9 @@ inline void QUrlPrivate::setQuery(const QString &value, int from, int iend)
QString output;
const QChar *begin = value.constData() + from;
const QChar *end = value.constData() + iend;
- if (qt_urlRecode(output, begin, end, QUrl::DecodeUnicode | QUrl::DecodeSpaces,
+
+ // leave delimiters alone but decode the rest
+ if (qt_urlRecode(output, begin, end, QUrl::EncodeDelimiters,
decodedQueryInIsolationActions))
query = output;
else
@@ -804,7 +809,7 @@ inline void QUrlPrivate::setQuery(const QString &value, int from, int iend)
inline void QUrlPrivate::appendHost(QString &appendTo, QUrl::FormattingOptions options) const
{
// this is the only flag that matters
- options &= QUrl::DecodeUnicode;
+ options &= QUrl::EncodeUnicode;
if (host.isEmpty())
return;
if (host.at(0).unicode() == '[') {
@@ -813,10 +818,10 @@ inline void QUrlPrivate::appendHost(QString &appendTo, QUrl::FormattingOptions o
} else {
// this is either an IPv4Address or a reg-name
// if it is a reg-name, it is already stored in Unicode form
- if (options == QUrl::DecodeUnicode)
- appendTo += host;
- else
+ if (options == QUrl::EncodeUnicode)
appendTo += qt_ACE_do(host, ToAceOnly);
+ else
+ appendTo += host;
}
}
@@ -1915,7 +1920,7 @@ bool QUrl::hasFragment() const
QString QUrl::topLevelDomain(ComponentFormattingOptions options) const
{
QString tld = qTopLevelDomain(host());
- if ((options & DecodeUnicode) == 0) {
+ if (options & EncodeUnicode) {
return qt_ACE_do(tld, ToAceOnly);
}
return tld;
@@ -2055,8 +2060,12 @@ QString QUrl::toString(FormattingOptions options) const
}
QString url;
- if (!options.testFlag(DecodeReserved))
- options &= ~DecodeReserved;
+
+ // for the full URL, we consider that the reserved characters are prettier if encoded
+ if (options & DecodeReserved)
+ options &= ~EncodeReserved;
+ else
+ options |= EncodeReserved;
if (!(options & QUrl::RemoveScheme) && d->hasScheme())
url += d->scheme + QLatin1Char(':');
@@ -2123,9 +2132,8 @@ QString QUrl::toDisplayString(FormattingOptions options) const
*/
QByteArray QUrl::toEncoded(FormattingOptions options) const
{
- QString stringForm = toString(options);
- if (options & DecodeUnicode)
- return stringForm.toUtf8();
+ options &= ~DecodeReserved;
+ QString stringForm = toString(options | FullyEncoded);
return stringForm.toLatin1();
}
diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h
index ff46a8ac1a..dbfc327caf 100644
--- a/src/corelib/io/qurl.h
+++ b/src/corelib/io/qurl.h
@@ -138,15 +138,15 @@ public:
};
enum ComponentFormattingOption {
- FullyEncoded = 0x000000,
- DecodeSpaces = 0x100000,
- DecodeUnicode = 0x200000,
- DecodeDelimiters = 0x400000 | 0x800000,
- PrettyDecodeReserved = 0x1000000,
- DecodeReserved = PrettyDecodeReserved | 0x2000000,
-
- PrettyDecoded = DecodeSpaces | DecodeDelimiters | PrettyDecodeReserved | DecodeUnicode,
- MostDecoded = PrettyDecoded
+ PrettyDecoded = 0x000000,
+ EncodeSpaces = 0x100000,
+ EncodeUnicode = 0x200000,
+ EncodeDelimiters = 0x400000 | 0x800000,
+ EncodeReserved = 0x1000000,
+ DecodeReserved = 0x2000000,
+
+ FullyEncoded = EncodeSpaces | EncodeUnicode | EncodeDelimiters | EncodeReserved,
+ MostDecoded = PrettyDecoded | DecodeReserved
};
Q_DECLARE_FLAGS(ComponentFormattingOptions, ComponentFormattingOption)
#ifdef qdoc
@@ -331,8 +331,6 @@ inline QIncompatibleFlag operator|(QUrl::UrlFormattingOption f1, int f2)
{ return QIncompatibleFlag(int(f1) | f2); }
// add operators for OR'ing the two types of flags
-inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOption f)
-{ i |= QUrl::UrlFormattingOption(int(f)); return i; }
inline QUrl::FormattingOptions &operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
{ i |= QUrl::UrlFormattingOption(int(f)); return i; }
Q_DECL_CONSTEXPR inline QUrl::FormattingOptions operator|(QUrl::UrlFormattingOption i, QUrl::ComponentFormattingOption f)
diff --git a/src/corelib/io/qurlquery.cpp b/src/corelib/io/qurlquery.cpp
index c0b90dd587..ccb03611f5 100644
--- a/src/corelib/io/qurlquery.cpp
+++ b/src/corelib/io/qurlquery.cpp
@@ -226,7 +226,7 @@ inline QString QUrlQueryPrivate::recodeToUser(const QString &input, QUrl::Compon
if (idempotentRecodeToUser(encoding))
return input;
- if (encoding & QUrl::DecodeDelimiters) {
+ if (!(encoding & QUrl::EncodeDelimiters)) {
QString output;
if (qt_urlRecode(output, input.constData(), input.constData() + input.length(),
encoding, prettyDecodedActions))
@@ -466,10 +466,7 @@ QString QUrlQuery::query(QUrl::ComponentFormattingOptions encoding) const
decode('#'), // 3
0
};
- if (encoding & QUrl::DecodeDelimiters) {
- // full decoding: we only encode the characters above
- tableActions[3] = 0;
- } else {
+ if (encoding & QUrl::EncodeDelimiters) {
tableActions[3] = encode('#');
}
diff --git a/src/corelib/io/qurlrecode.cpp b/src/corelib/io/qurlrecode.cpp
index 02fced5b7b..b586bd7a1e 100644
--- a/src/corelib/io/qurlrecode.cpp
+++ b/src/corelib/io/qurlrecode.cpp
@@ -512,7 +512,7 @@ non_trivial:
if (decoded >= 0x80) {
// decode the UTF-8 sequence
- if (encoding & QUrl::DecodeUnicode &&
+ if (!(encoding & QUrl::EncodeUnicode) &&
encodedUtf8ToUtf16(result, output, begin, input, end, decoded))
continue;
@@ -523,7 +523,7 @@ non_trivial:
}
} else {
decoded = c;
- if (decoded >= 0x80 && (encoding & QUrl::DecodeUnicode) == 0) {
+ if (decoded >= 0x80 && encoding & QUrl::EncodeUnicode) {
// encode the UTF-8 sequence
unicodeToEncodedUtf8(result, output, begin, input, end, decoded);
continue;
@@ -581,15 +581,42 @@ static void maskTable(uchar (&table)[N], const uchar (&mask)[N])
table[i] &= mask[i];
}
+/*!
+ \internal
+
+ Recodes the string from \a begin to \a end. If any transformations are
+ done, append them to \a appendTo and return the number of characters added.
+ If no transformations were required, return 0.
+
+ The \a encoding option modifies the default behaviour:
+ \list
+ \li QUrl::EncodeDelimiters: if set, delimiters will be left untransformed (note: not encoded!);
+ if unset, delimiters will be decoded
+ \li QUrl::DecodeReserved: if set, reserved characters will be decoded;
+ if unset, reserved characters will be encoded
+ \li QUrl::EncodeSpaces: if set, spaces will be encoded to "%20"; if unset, they will be " "
+ \li QUrl::EncodeUnicode: if set, characters above U+0080 will be encoded to their UTF-8
+ percent-encoded form; if unset, they will be decoded to UTF-16
+ \endlist
+
+ Other flags are ignored (including QUrl::EncodeReserved).
+
+ The \a tableModifications argument can be used to supply extra
+ modifications to the tables, to be applied after the flags above are
+ handled. It consists of a sequence of 16-bit values, where the low 8 bits
+ indicate the character in question and the high 8 bits are either \c
+ EncodeCharacter, \c LeaveCharacter or \c DecodeCharacter.
+ */
+
Q_AUTOTEST_EXPORT int
qt_urlRecode(QString &appendTo, const QChar *begin, const QChar *end,
QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications)
{
uchar actionTable[sizeof defaultActionTable];
- if (encoding & QUrl::DecodeDelimiters && encoding & QUrl::DecodeReserved) {
+ if (!(encoding & QUrl::EncodeDelimiters) && encoding & QUrl::DecodeReserved) {
// reset the table
memset(actionTable, DecodeCharacter, sizeof actionTable);
- if (!(encoding & QUrl::DecodeSpaces))
+ if (encoding & QUrl::EncodeSpaces)
actionTable[0] = EncodeCharacter;
// these are always encoded
@@ -597,11 +624,11 @@ qt_urlRecode(QString &appendTo, const QChar *begin, const QChar *end,
actionTable[0x7F - ' '] = EncodeCharacter;
} else {
memcpy(actionTable, defaultActionTable, sizeof actionTable);
- if (encoding & QUrl::DecodeDelimiters)
+ if (!(encoding & QUrl::EncodeDelimiters))
maskTable(actionTable, delimsMask);
if (encoding & QUrl::DecodeReserved)
maskTable(actionTable, reservedMask);
- if (encoding & QUrl::DecodeSpaces)
+ if (!(encoding & QUrl::EncodeSpaces))
actionTable[0] = DecodeCharacter; // decode
}