Mercurial > minori
diff src/core/strings.cc @ 99:503bc1547d49
strings: clarify on some functions and make some of them miniscule
amounts faster
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 03 Nov 2023 09:00:46 -0400 |
parents | 582b2fca1561 |
children | f5940a575d83 |
line wrap: on
line diff
--- a/src/core/strings.cc Thu Nov 02 15:22:02 2023 -0400 +++ b/src/core/strings.cc Fri Nov 03 09:00:46 2023 -0400 @@ -14,6 +14,7 @@ namespace Strings { +/* ew */ std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { if (vector.size() < 1) return "-"; @@ -26,6 +27,8 @@ return out; } +/* This function is really only used for cleaning up the synopsis of + horrible HTML debris from AniList :) */ std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace) { size_t pos = 0; while ((pos = string.find(find, pos)) != std::string::npos) { @@ -35,29 +38,28 @@ return string; } -/* :) */ std::string SanitizeLineEndings(const std::string& string) { return ReplaceAll(ReplaceAll(ReplaceAll(string, "\r\n", "\n"), "<br>", "\n"), "\n\n\n", "\n\n"); } /* removes dumb HTML tags because anilist is aids and gives us HTML for synopses :/ */ -std::string RemoveHtmlTags(const std::string& string) { - std::string html(string); - while (html.find("<") != std::string::npos) { - auto startpos = html.find("<"); - auto endpos = html.find(">") + 1; +std::string RemoveHtmlTags(std::string string) { + while (string.find("<") != std::string::npos) { + auto startpos = string.find("<"); + auto endpos = string.find(">") + 1; - if (endpos != std::string::npos) { - html.erase(startpos, endpos - startpos); - } + if (endpos != std::string::npos) + string.erase(startpos, endpos - startpos); } - return html; + return string; } /* e.g. "<" for "<" */ -std::string ParseHtmlEntities(const std::string& string) { +std::string ParseHtmlEntities(std::string string) { const std::unordered_map<std::string, std::string> map = { + /* The only one of these I can understand using are the first + three. why do the rest of these exist? */ {"<", "<"}, {"&rt;", ">"}, {" ", "\xA0"}, @@ -73,13 +75,13 @@ {"’", "’"} // Haibane Renmei, AniList }; - std::string ret = string; for (const auto& item : map) - ret = ReplaceAll(ret, item.first, item.second); - return ret; + if (string.find(item.first) != std::string::npos) + string = ReplaceAll(string, item.first, item.second); + return string; } -/* */ +/* removes stupid HTML stuff */ std::string TextifySynopsis(const std::string& string) { return ParseHtmlEntities(RemoveHtmlTags(SanitizeLineEndings(string))); } @@ -87,7 +89,9 @@ /* let Qt handle the heavy lifting of locale shit I don't want to deal with */ std::string ToUpper(const std::string& string) { - /* todo: this "locale" will have to be moved to session.h */ + /* todo: this "locale" will have to be moved to session.h + it also defaults to en-US, which sucks very much for + anyone who doesn't speak american english... */ QLocale locale; return ToUtf8String(locale.toUpper(ToQString(string))); }