escape_xml now takes in a std::string*

It previously took in a std::string, which means the string is copied and the
string isn't modified in-place nor does the caller get the escaped string,
making the call essentially useless
This commit is contained in:
blank X 2021-12-02 22:19:25 +07:00
parent f81d977f46
commit 57e1498aec
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 14 additions and 14 deletions

View File

@ -26,33 +26,33 @@ size_t write_memory_cb(char* contents, size_t size, size_t nmemb, std::string* s
// behold: the why // behold: the why
// oh and this is stolen from https://stackoverflow.com/a/3418285 // oh and this is stolen from https://stackoverflow.com/a/3418285
void escape_xml(std::string str) { void escape_xml(std::string* str) {
if (str.empty()) { if (str->empty()) {
return; return;
} }
size_t start = 0; size_t start = 0;
while ((start = str.find("&", start)) != std::string::npos) { while ((start = str->find("&", start)) != std::string::npos) {
str.replace(start, 1, "&"); str->replace(start, 1, "&");
start += 5; start += 5;
} }
start = 0; start = 0;
while ((start = str.find("<", start)) != std::string::npos) { while ((start = str->find("<", start)) != std::string::npos) {
str.replace(start, 1, "&lt;"); str->replace(start, 1, "&lt;");
start += 4; start += 4;
} }
start = 0; start = 0;
while ((start = str.find(">", start)) != std::string::npos) { while ((start = str->find(">", start)) != std::string::npos) {
str.replace(start, 1, "&gt;"); str->replace(start, 1, "&gt;");
start += 4; start += 4;
} }
start = 0; start = 0;
while ((start = str.find("\"", start)) != std::string::npos) { while ((start = str->find("\"", start)) != std::string::npos) {
str.replace(start, 1, "&quot;"); str->replace(start, 1, "&quot;");
start += 6; start += 6;
} }
start = 0; start = 0;
while ((start = str.find("'", start)) != std::string::npos) { while ((start = str->find("'", start)) != std::string::npos) {
str.replace(start, 1, "&#x27;"); str->replace(start, 1, "&#x27;");
start += 6; start += 6;
} }
} }
@ -113,7 +113,7 @@ int main(int argc, char* argv[]) {
last_updated_timestamp = time(NULL); last_updated_timestamp = time(NULL);
} }
std::string ogp_title = j["body"]["meta"]["ogp"]["title"].get<std::string>(); std::string ogp_title = j["body"]["meta"]["ogp"]["title"].get<std::string>();
escape_xml(ogp_title); escape_xml(&ogp_title);
char tm_str[100] = ""; char tm_str[100] = "";
strftime(tm_str, 100, "%FT%TZ", gmtime(&last_updated_timestamp)); strftime(tm_str, 100, "%FT%TZ", gmtime(&last_updated_timestamp));
// wonder if pixiv is a urn namespace // wonder if pixiv is a urn namespace
@ -126,7 +126,7 @@ int main(int argc, char* argv[]) {
// istg if 0 isn't 1970 // istg if 0 isn't 1970
time_t upload_timestamp = illust["upload_timestamp"].get<time_t>(); time_t upload_timestamp = illust["upload_timestamp"].get<time_t>();
strftime(tm_str, 100, "%FT%TZ", gmtime(&upload_timestamp)); strftime(tm_str, 100, "%FT%TZ", gmtime(&upload_timestamp));
escape_xml(title); escape_xml(&title);
printf(" <entry>\n <id>urn:pixiv:illust:%s</id>\n <title type=\"text\">%s</title>\n <updated>%s</updated>\n <link rel=\"alternate\" type=\"text/html\" href=\"https://www.pixiv.net/en/artworks/%s\"></link>\n </entry>\n", illust_id.c_str(), title.c_str(), tm_str, illust_id.c_str()); printf(" <entry>\n <id>urn:pixiv:illust:%s</id>\n <title type=\"text\">%s</title>\n <updated>%s</updated>\n <link rel=\"alternate\" type=\"text/html\" href=\"https://www.pixiv.net/en/artworks/%s\"></link>\n </entry>\n", illust_id.c_str(), title.c_str(), tm_str, illust_id.c_str());
} }
printf("</feed>\n"); printf("</feed>\n");