Add QByteArray::percentDecoded() as an instance method
Percent-decoding was previously only present as a static method taking a QBA parameter; it might as well be an instance method of that parameter. Change most QBA tests to use it rather the static method. [ChangeLog][QtCore][QByteArray] percentDecoded() is now available as an instance method of the byte array to be decoded, equivalent to the static QByteArray::fromPercentEncoding(). Change-Id: I982101c44bdac5cc4041e85598d52ac101d38fa1 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
e0206fe9d4
commit
e10d613509
@ -490,17 +490,22 @@ macAddress.toHex(0); // returns "123456abcdef"
|
||||
|
||||
//! [51]
|
||||
QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33");
|
||||
text.data(); // returns "Qt is great!"
|
||||
qDebug("%s", text.data()); // reports "Qt is great!"
|
||||
//! [51]
|
||||
|
||||
//! [52]
|
||||
QByteArray text = "{a fishy string?}";
|
||||
QByteArray ba = text.toPercentEncoding("{}", "s");
|
||||
qDebug(ba.constData());
|
||||
qDebug("%s", ba.constData());
|
||||
// prints "{a fi%73hy %73tring%3F}"
|
||||
//! [52]
|
||||
|
||||
//! [53]
|
||||
QByteArray ba = QByteArrayLiteral("byte array contents");
|
||||
//! [53]
|
||||
|
||||
//! [54]
|
||||
QByteArray encoded("Qt%20is%20great%33");
|
||||
QByteArray decoded = encoded.percentDecoded(); // Set to "Qt is great!"
|
||||
//! [54]
|
||||
}
|
||||
|
@ -4494,20 +4494,17 @@ static void q_fromPercentEncoding(QByteArray *ba, char percent)
|
||||
ba->truncate(outlen);
|
||||
}
|
||||
|
||||
void q_fromPercentEncoding(QByteArray *ba)
|
||||
{
|
||||
q_fromPercentEncoding(ba, '%');
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 4.4
|
||||
\since 6.4
|
||||
|
||||
Returns a decoded copy of the URI/URL-style percent-encoded \a input.
|
||||
The \a percent parameter allows you to replace the '%' character for
|
||||
another (for instance, '_' or '=').
|
||||
Decodes URI/URL-style percent-encoding.
|
||||
|
||||
Returns a byte array containing the decoded text. The \a percent parameter
|
||||
allows use of a different character than '%' (for instance, '_' or '=') as
|
||||
the escape character.
|
||||
|
||||
For example:
|
||||
\snippet code/src_corelib_text_qbytearray.cpp 51
|
||||
\snippet code/src_corelib_text_qbytearray.cpp 54
|
||||
|
||||
\note Given invalid input (such as a string containing the sequence "%G5",
|
||||
which is not a valid hexadecimal number) the output will be invalid as
|
||||
@ -4515,18 +4512,35 @@ void q_fromPercentEncoding(QByteArray *ba)
|
||||
|
||||
\sa toPercentEncoding(), QUrl::fromPercentEncoding()
|
||||
*/
|
||||
QByteArray QByteArray::fromPercentEncoding(const QByteArray &input, char percent)
|
||||
QByteArray QByteArray::percentDecoded(char percent) const
|
||||
{
|
||||
if (input.isNull())
|
||||
return QByteArray(); // preserve null
|
||||
if (input.isEmpty())
|
||||
return QByteArray(input.data(), 0);
|
||||
if (isEmpty())
|
||||
return *this; // Preserves isNull().
|
||||
|
||||
QByteArray tmp = input;
|
||||
QByteArray tmp = *this;
|
||||
q_fromPercentEncoding(&tmp, percent);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/*!
|
||||
\since 4.4
|
||||
|
||||
Decodes \a input from URI/URL-style percent-encoding.
|
||||
|
||||
Returns a byte array containing the decoded text. The \a percent parameter
|
||||
allows use of a different character than '%' (for instance, '_' or '=') as
|
||||
the escape character. Equivalent to input.percentDecoded(percent).
|
||||
|
||||
For example:
|
||||
\snippet code/src_corelib_text_qbytearray.cpp 51
|
||||
|
||||
\sa percentDecoded()
|
||||
*/
|
||||
QByteArray QByteArray::fromPercentEncoding(const QByteArray &input, char percent)
|
||||
{
|
||||
return input.percentDecoded(percent);
|
||||
}
|
||||
|
||||
/*! \fn QByteArray QByteArray::fromStdString(const std::string &str)
|
||||
\since 5.4
|
||||
|
||||
|
@ -373,6 +373,7 @@ public:
|
||||
QByteArray toPercentEncoding(const QByteArray &exclude = QByteArray(),
|
||||
const QByteArray &include = QByteArray(),
|
||||
char percent = '%') const;
|
||||
[[nodiscard]] QByteArray percentDecoded(char percent = '%') const;
|
||||
|
||||
inline QByteArray &setNum(short, int base = 10);
|
||||
inline QByteArray &setNum(ushort, int base = 10);
|
||||
|
@ -1652,15 +1652,15 @@ void tst_QByteArray::toFromPercentEncoding()
|
||||
|
||||
QByteArray data = arr.toPercentEncoding();
|
||||
QCOMPARE(data, QByteArray("Qt%20is%20great%21"));
|
||||
QCOMPARE(QByteArray::fromPercentEncoding(data), arr);
|
||||
QCOMPARE(data.percentDecoded(), arr);
|
||||
|
||||
data = arr.toPercentEncoding("! ", "Qt");
|
||||
QCOMPARE(data, QByteArray("%51%74 is grea%74!"));
|
||||
QCOMPARE(QByteArray::fromPercentEncoding(data), arr);
|
||||
QCOMPARE(data.percentDecoded(), arr);
|
||||
|
||||
data = arr.toPercentEncoding(QByteArray(), "abcdefghijklmnopqrstuvwxyz", 'Q');
|
||||
QCOMPARE(data, QByteArray("Q51Q74Q20Q69Q73Q20Q67Q72Q65Q61Q74Q21"));
|
||||
QCOMPARE(QByteArray::fromPercentEncoding(data, 'Q'), arr);
|
||||
QCOMPARE(data.percentDecoded('Q'), arr);
|
||||
|
||||
// verify that to/from percent encoding preserves nullity
|
||||
arr = "";
|
||||
@ -1676,16 +1676,16 @@ void tst_QByteArray::toFromPercentEncoding()
|
||||
QVERIFY(arr.isNull());
|
||||
QVERIFY(arr.toPercentEncoding().isEmpty());
|
||||
QVERIFY(arr.toPercentEncoding().isNull());
|
||||
QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isEmpty());
|
||||
QVERIFY(QByteArray::fromPercentEncoding(QByteArray()).isNull());
|
||||
QVERIFY(QByteArray().percentDecoded().isEmpty());
|
||||
QVERIFY(QByteArray().percentDecoded().isNull());
|
||||
|
||||
// Verify that literal % in the string to be encoded does round-trip:
|
||||
arr = "Qt%20is%20great%21";
|
||||
data = arr.toPercentEncoding();
|
||||
QCOMPARE(QByteArray::fromPercentEncoding(data), arr);
|
||||
QCOMPARE(data.percentDecoded(), arr);
|
||||
arr = "87% of all statistics are made up!";
|
||||
data = arr.toPercentEncoding();
|
||||
QCOMPARE(QByteArray::fromPercentEncoding(data), arr);
|
||||
QCOMPARE(data.percentDecoded(), arr);
|
||||
}
|
||||
|
||||
void tst_QByteArray::fromPercentEncoding_data()
|
||||
@ -1707,7 +1707,7 @@ void tst_QByteArray::fromPercentEncoding()
|
||||
QFETCH(QByteArray, encodedString);
|
||||
QFETCH(QByteArray, decodedString);
|
||||
|
||||
QCOMPARE(QByteArray::fromPercentEncoding(encodedString), decodedString);
|
||||
QCOMPARE(encodedString.percentDecoded(), decodedString);
|
||||
}
|
||||
|
||||
void tst_QByteArray::toPercentEncoding_data()
|
||||
@ -1768,7 +1768,7 @@ void tst_QByteArray::pecentEncodingRoundTrip()
|
||||
|
||||
QByteArray encodedData = original.toPercentEncoding(excludeInEncoding, includeInEncoding);
|
||||
QCOMPARE(encodedData, encoded);
|
||||
QCOMPARE(QByteArray::fromPercentEncoding(encodedData), original);
|
||||
QCOMPARE(encodedData.percentDecoded(), original);
|
||||
}
|
||||
|
||||
struct StringComparisonData
|
||||
|
Loading…
x
Reference in New Issue
Block a user