Make urldecode in-place

This commit is contained in:
Luis Díaz Más 2022-02-16 17:35:06 +01:00
parent 3d370cc2ae
commit f1ff3aaa4c
3 changed files with 14 additions and 41 deletions

View File

@ -65,17 +65,6 @@ namespace Exiv2
*/
EXIV2API std::string urlencode(const std::string_view& str);
/*!
@brief Decode the input url.
@param str The url needs decoding.
@return the url-decoded version of str.
@note Be sure to 'free' the returned string after use with 'delete []'.
Source: http://www.geekhideout.com/urlcode.shtml
@todo This function can probably be hidden into the implementation details
*/
EXIV2API char* urldecode(const char* str);
/*!
@brief Like urlencode(char* str) but accept the input url in the std::string and modify it.
@todo This function can probably be hidden into the implementation details

View File

@ -120,31 +120,24 @@ namespace Exiv2 {
return encoded;
}
char* urldecode(const char* str) {
const char* pstr = str;
auto buf = new char[(strlen(str) + 1)];
char* pbuf = buf;
while (*pstr) {
if (*pstr == '%') {
if (pstr[1] && pstr[2]) {
*pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
pstr += 2;
void urldecode(std::string& str)
{
size_t idxIn{0}, idxOut{0};
size_t sizeStr = str.size();
while (idxIn < sizeStr) {
if (str[idxIn] == '%') {
if (str[idxIn+1] && str[idxIn+2]) {
str[idxOut++] = from_hex(str[idxIn+1]) << 4 | from_hex(str[idxIn+2]);
idxIn += 2;
}
} else if (*pstr == '+') {
*pbuf++ = ' ';
} else if (str[idxIn] == '+') {
str[idxOut++] = ' ';
} else {
*pbuf++ = *pstr;
str[idxOut++] = str[idxIn];
}
pstr++;
idxIn++;
}
*pbuf = '\0';
return buf;
}
void urldecode(std::string& str) {
char* decodeStr = Exiv2::urldecode(str.c_str());
str = std::string(decodeStr);
delete [] decodeStr;
str.erase(idxOut);
}
// https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c

View File

@ -97,15 +97,6 @@ TEST(urlencode, encodesGivenUrlWithSpace)
ASSERT_STREQ("http%3a%2f%2fwww.geekhideout.com%2furl+code.shtml", url.c_str());
}
TEST(urldecode, decodesGivenUrl)
{
const std::string expectedDecodedUrl ("http://www.geekhideout.com/urlcode.shtml");
const std::string url ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml");
char * url3 = urldecode(url.c_str());
ASSERT_STREQ(expectedDecodedUrl.c_str(), url3);
delete [] url3;
}
TEST(urldecode, decodesGivenUrlInPlace)
{
const std::string expectedDecodedUrl ("http://www.geekhideout.com/urlcode.shtml");