mirror of
https://github.com/spiffcode/hostile-takeover.git
synced 2025-12-16 12:08:36 +00:00
62 lines
1.7 KiB
C++
62 lines
1.7 KiB
C++
#include "base/misc.h"
|
|
#include <ctype.h>
|
|
#include <string>
|
|
#include <cstring>
|
|
|
|
namespace base {
|
|
|
|
const char *StringEncoder::Replace(const char *s, const char *findstr,
|
|
const char *replacewith) {
|
|
int index = 0;
|
|
std::string out(s);
|
|
while (true) {
|
|
size_t found = out.find(findstr, index);
|
|
if (found == std::string::npos) {
|
|
break;
|
|
}
|
|
out.replace(found, strlen(findstr), replacewith);
|
|
index += found + strlen(replacewith);
|
|
}
|
|
return base::Format::ToString("%s", out.c_str());
|
|
}
|
|
|
|
// Algorithmically identical to python cgi.escape
|
|
const char *StringEncoder::Escape(const char *s, bool quote) {
|
|
// html content escaping
|
|
std::string out = Replace(s, "&", "&");
|
|
out = Replace(out.c_str(), "<", "<");
|
|
out = Replace(out.c_str(), ">", ">");
|
|
if (quote) {
|
|
out = Replace(out.c_str(), "\"", """);
|
|
}
|
|
return base::Format::ToString("%s", out.c_str());
|
|
}
|
|
|
|
// For encoding query keys and values
|
|
const char *StringEncoder::QueryEncode(const char *s) {
|
|
char *buf = new char[strlen(s) * 3 + 1];
|
|
char *out = buf;
|
|
const char *in = s;
|
|
while (*in) {
|
|
if (isalnum(*in) || *in == '-' || *in == '_' || *in == '.' ||
|
|
*in == '~') {
|
|
*out++ = *in++;
|
|
} else if (*in == ' ') {
|
|
*out++ = '+';
|
|
in++;
|
|
} else {
|
|
char *hex = "0123456789abcdef";
|
|
*out++ = '%';
|
|
*out++ = hex[(*in >> 4) & 0xf];
|
|
*out++ = hex[(*in) & 0xf];
|
|
in++;
|
|
}
|
|
}
|
|
*out = 0;
|
|
const char *result = base::Format::ToString("%s", buf);
|
|
delete[] buf;
|
|
return result;
|
|
}
|
|
|
|
} // namespace base
|