Skip to content

Instantly share code, notes, and snippets.

@omegacoleman
Last active September 10, 2019 13:10
Show Gist options
  • Save omegacoleman/bf344cbc6d33893a72aa6f0c9e9ed15a to your computer and use it in GitHub Desktop.
Save omegacoleman/bf344cbc6d33893a72aa6f0c9e9ed15a to your computer and use it in GitHub Desktop.
#include <sstream>
#include <boost/utility/string_view.hpp>
#include "../../exception.hpp"
namespace s3::api::misc
{
inline char digit_to_upper_hex_letter(const char ch)
{
return (ch >= 10) ? ('A' - 10 + ch) : ('0' + ch);
}
template <bool EncodeSlash>
inline void uriencode_char(const char ch, std::ostringstream &oss);
template <>
inline void uriencode_char<true>(const char ch, std::ostringstream &oss)
{
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '~' || ch == '.') {
oss << ch;
} else {
oss << '%';
oss << digit_to_upper_hex_letter((ch >> 4) & 0x0f);
oss << digit_to_upper_hex_letter(ch & 0x0f);
}
}
template <>
inline void uriencode_char<false>(const char ch, std::ostringstream &oss)
{
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '~' || ch == '.'
|| ch == '/') {
oss << ch;
} else {
oss << '%';
oss << digit_to_upper_hex_letter((ch >> 4) & 0x0f);
oss << digit_to_upper_hex_letter(ch & 0x0f);
}
}
template <bool EncodeSlash=true>
inline std::string uriencode(boost::string_view input)
{
std::ostringstream oss{};
for(const char ch : input)
{
uriencode_char<EncodeSlash>(ch, oss);
}
return oss.str();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment