diff src/core/strings.cc @ 260:dd211ff68b36

pages/seasons: add initial functionality the menu doesn't work yet, but it's a good start
author Paper <paper@paper.us.eu.org>
date Wed, 03 Apr 2024 19:48:38 -0400
parents 862d0d8619f6
children 9a04802848c0
line wrap: on
line diff
--- a/src/core/strings.cc	Mon Apr 01 18:11:15 2024 -0400
+++ b/src/core/strings.cc	Wed Apr 03 19:48:38 2024 -0400
@@ -70,26 +70,38 @@
 /* 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) {
+void ReplaceAll(std::string& string, std::string_view find, std::string_view replace) {
 	size_t pos = 0;
 	while ((pos = string.find(find, pos)) != std::string::npos) {
 		string.replace(pos, find.length(), replace);
 		pos += replace.length();
 	}
-	return string;
 }
 
-std::string SanitizeLineEndings(const std::string& string) {
+void SanitizeLineEndings(std::string& string) {
 	/* LOL */
-	return ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(string, "\r\n", "\n"), "</p>", "\n"), "<br>", "\n"),
-	                             "<br />", "\n"),
-	                  "\n\n\n", "\n\n");
+	ReplaceAll(string, "\r\n",   "\n");
+	ReplaceAll(string, "</p>",   "\n");
+	ReplaceAll(string, "<br>",   "\n");
+	ReplaceAll(string, "<br />", "\n");
+	ReplaceAll(string, "\n\n\n", "\n\n");
+}
+
+void ConvertRomanNumerals(std::string& string) {
+	static const std::vector<std::pair<std::string_view, std::string_view>> vec = {
+		{"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"},
+		{"7", "VII"}, {"8", "VIII"}, {"9", "IX"}, {"11", "XI"}, {"12", "XII"},
+		{"13", "XIII"}
+	};
+
+	for (const auto& item : vec)
+		ReplaceAll(string, item.second, item.first);
 }
 
 /* removes dumb HTML tags because anilist is aids and
  * gives us HTML for synopses :/
  */
-std::string RemoveHtmlTags(std::string string) {
+void RemoveHtmlTags(std::string& string) {
 	while (string.find("<") != std::string::npos) {
 		auto startpos = string.find("<");
 		auto endpos = string.find(">") + 1;
@@ -97,17 +109,16 @@
 		if (endpos != std::string::npos)
 			string.erase(startpos, endpos - startpos);
 	}
-	return string;
 }
 
 /* e.g. "&lt;" for "<" */
-std::string ParseHtmlEntities(std::string string) {
+void ParseHtmlEntities(std::string& string) {
+	/* The only one of these I can understand using are the first
+	 * three. why do the rest of these exist?
+	 *
+	 * probably mojibake.
+	 */
 	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?
-  *
-  * probably mojibake.
-  */
 	    {"&lt;",    "<"   },
         {"&rt;",    ">"   },
         {"&nbsp;",  "\xA0"},
@@ -124,13 +135,14 @@
 	};
 
 	for (const auto& item : map)
-		string = ReplaceAll(string, item.first, item.second);
-	return string;
+		ReplaceAll(string, item.first, item.second);
 }
 
 /* removes stupid HTML stuff */
-std::string TextifySynopsis(const std::string& string) {
-	return ParseHtmlEntities(RemoveHtmlTags(SanitizeLineEndings(string)));
+void TextifySynopsis(std::string& string) {
+	SanitizeLineEndings(string);
+	RemoveHtmlTags(string);
+	ParseHtmlEntities(string);
 }
 
 /* let Qt handle the heavy lifting of locale shit