diff options
author | kukin-konstantin <kukin.konstantin@gmail.com> | 2017-01-11 20:33:18 +0300 |
---|---|---|
committer | kukin-konstantin <kukin.konstantin@gmail.com> | 2017-01-11 20:33:18 +0300 |
commit | 54a50a7c8a7e9d15c03bdb64babe54e5c123e5c7 (patch) | |
tree | d375e690871e51c221f88420808677af7f11a361 /src/tools | |
parent | f3aba5cb3cca0f99f2a39d61acd960a836295593 (diff) |
fix encodeUrl function
Diffstat (limited to 'src/tools')
-rw-r--r-- | src/tools/StringTools.cpp | 20 |
1 files changed, 11 insertions, 9 deletions
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) { |