From 54a50a7c8a7e9d15c03bdb64babe54e5c123e5c7 Mon Sep 17 00:00:00 2001 From: kukin-konstantin Date: Wed, 11 Jan 2017 20:33:18 +0300 Subject: fix encodeUrl function --- src/tools/StringTools.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'src/tools') diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index c682b35..d5270db 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -84,20 +84,22 @@ string generateRandomString(size_t length) { return result; } + string urlEncode(const string& value, const std::string& additionalLegitChars) { - static const string legitPunctuation = "-_.~"; - ostringstream result; - result.fill('0'); - result << hex; - for (const char& c : value) { - if (isalnum(c) || legitPunctuation.find(c) != legitPunctuation.npos || additionalLegitChars.find(c) != additionalLegitChars.npos) { - result << c; + static const string legitPunctuation = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~:"; + std::stringstream ss; + std::string t; + for (auto const &c : value) { + if ((legitPunctuation.find(c) == std::string::npos) + && (additionalLegitChars.find(c)==std::string::npos)) { + ss << '%' << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)(unsigned char)c; + t = ss.str(); } else { - result << '%' << setw(2) << int((unsigned char) c); + ss << c; } } - return result.str(); + return ss.str(); } string urlDecode(const string& value) { -- cgit v1.2.3