summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qurlrecode.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-05-21 20:21:16 +0200
committerQt by Nokia <qt-info@nokia.com>2012-05-22 20:56:38 +0200
commit1ca791faf5b89c35b4b39d443d2118a882c3946b (patch)
treee11be9b68338786b61d12323f58e998211f89121 /src/corelib/io/qurlrecode.cpp
parent53d0624403f7f2ac8fe8364a7c5cd136717d40ed (diff)
Add the QUrl::FullyDecoded flag to the component formatting
This allows the QUrl component getters to return fully decoded data, like they did in Qt 4. This is necessary for some use-cases where the component like the user name, password or path are used outside the context of a URL. In those contexts, the percent-encoded data makes no sense, and the loss of data of what could be represented in a URL is acceptable. Also take the opportunity to expand the documentation of those getter methods, explaining what the options argument does. Discussed-on: http://lists.qt-project.org/pipermail/development/2012-May/003811.html Change-Id: I89f743cde78c02f169c88314bff0768714341419 Reviewed-by: Lars Knoll <lars.knoll@nokia.com> Reviewed-by: David Faure <faure@kde.org> Reviewed-by: Shane Kearns <shane.kearns@accenture.com>
Diffstat (limited to 'src/corelib/io/qurlrecode.cpp')
-rw-r--r--src/corelib/io/qurlrecode.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/corelib/io/qurlrecode.cpp b/src/corelib/io/qurlrecode.cpp
index 3d985dbdc0..12d23e9450 100644
--- a/src/corelib/io/qurlrecode.cpp
+++ b/src/corelib/io/qurlrecode.cpp
@@ -560,6 +560,43 @@ non_trivial:
return 0;
}
+static int decode(QString &appendTo, const ushort *begin, const ushort *end)
+{
+ const int origSize = appendTo.size();
+ const ushort *input = begin;
+ ushort *output = 0;
+ while (input != end) {
+ if (*input != '%') {
+ if (output)
+ *output++ = *input;
+ ++input;
+ continue;
+ }
+
+ if (Q_UNLIKELY(!output)) {
+ // detach
+ appendTo.resize(origSize + (end - begin));
+ output = reinterpret_cast<ushort *>(appendTo.begin()) + origSize;
+ memcpy(output, begin, (input - begin) * sizeof(ushort));
+ output += input - begin;
+ }
+
+ ++input;
+ Q_ASSERT(input <= end - 2); // we need two characters
+ Q_ASSERT(isHex(input[0]));
+ Q_ASSERT(isHex(input[1]));
+ *output++ = decodeNibble(input[0]) << 4 | decodeNibble(input[1]);
+ input += 2;
+ }
+
+ if (output) {
+ int len = output - reinterpret_cast<ushort *>(appendTo.begin());
+ appendTo.truncate(len);
+ return len - origSize;
+ }
+ return 0;
+}
+
template <size_t N>
static void maskTable(uchar (&table)[N], const uchar (&mask)[N])
{
@@ -583,6 +620,12 @@ static void maskTable(uchar (&table)[N], const uchar (&mask)[N])
\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
+ \li QUrl::FullyDecoded: if set, this function will decode all percent-encoded sequences,
+ including that of the percent character. The resulting string
+ will not be percent-encoded anymore. Use with caution!
+ In this mode, the behaviour is undefined if the input string
+ contains any percent-encoding sequences above %80.
+ Also, the function will not correct bad % sequences.
\endlist
Other flags are ignored (including QUrl::EncodeReserved).
@@ -599,6 +642,10 @@ qt_urlRecode(QString &appendTo, const QChar *begin, const QChar *end,
QUrl::ComponentFormattingOptions encoding, const ushort *tableModifications)
{
uchar actionTable[sizeof defaultActionTable];
+ if (encoding == QUrl::FullyDecoded) {
+ return decode(appendTo, reinterpret_cast<const ushort *>(begin), reinterpret_cast<const ushort *>(end));
+ }
+
if (!(encoding & QUrl::EncodeDelimiters) && encoding & QUrl::DecodeReserved) {
// reset the table
memset(actionTable, DecodeCharacter, sizeof actionTable);