1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-21 22:13:07 +01:00

Introduce String::URLEncode

This commit is contained in:
Hielke Morsink
2021-09-05 12:44:58 +02:00
parent c23775bf8c
commit b2dfeaf1b9
2 changed files with 30 additions and 0 deletions

View File

@@ -14,7 +14,10 @@
#endif // __MINGW32__
#include <algorithm>
#include <cctype>
#include <cwctype>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <vector>
#ifndef _WIN32
@@ -823,6 +826,31 @@ namespace String
return trunc;
}
std::string URLEncode(std::string_view value)
{
std::ostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (auto c : value)
{
// Keep alphanumeric and other accepted characters intact
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
{
escaped << c;
}
else
{
// Any other characters are percent-escaped
escaped << std::uppercase;
escaped << '%' << std::setw(2) << int32_t(static_cast<unsigned char>(c));
escaped << std::nouppercase;
}
}
return escaped.str();
}
} // namespace String
char32_t CodepointView::iterator::GetNextCodepoint(const char* ch, const char** next)