Added Percent::encode
This commit is contained in:
parent
48d74a974a
commit
1d9e900f44
2 changed files with 24 additions and 0 deletions
19
utility.hpp
19
utility.hpp
|
|
@ -50,8 +50,27 @@ typedef std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, C
|
|||
/// Percent encoding and decoding
|
||||
class Percent {
|
||||
public:
|
||||
static std::string encode(const std::string &value) {
|
||||
static auto hex_chars = "0123456789ABCDEF";
|
||||
|
||||
std::string result;
|
||||
result.reserve(value.size()); // minimum size of result
|
||||
|
||||
for(auto &chr : value) {
|
||||
if(chr == ' ')
|
||||
result += '+';
|
||||
else if(chr == '!' || chr == '#' || chr == '$' || (chr >= '&' && chr <= ',') || (chr >= '/' && chr <= ';') || chr == '=' || chr=='?' || chr == '@' || chr == '[' || chr == ']')
|
||||
result += std::string("%") + hex_chars[chr >> 4] + hex_chars[chr & 15];
|
||||
else
|
||||
result += chr;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::string decode(const std::string &value) {
|
||||
std::string result;
|
||||
result.reserve((value.size() + 2) / 3); // minimum size of result
|
||||
|
||||
for(size_t i = 0; i < value.size(); ++i) {
|
||||
auto &chr = value[i];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue