From 1b7e9dba75f18342911bc6954be3e754322f091f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 30 Mar 2012 17:48:42 -0300 Subject: 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 --- src/corelib/io/qurlrecode.cpp | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'src/corelib/io/qurlrecode.cpp') 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 } -- cgit v1.2.3