comparison src/core/strings.cc @ 258:862d0d8619f6

*: HUUUGE changes animia has been renamed to animone, so instead of thinking of a health condition, you think of a beautiful flower :) I've also edited some of the code for animone, but I have no idea if it even works or not because I don't have a mac or windows machine lying around. whoops! ... anyway, all of the changes divergent from Anisthesia are now licensed under BSD. it's possible that I could even rewrite most of the code to where I don't even have to keep the MIT license, but that's thinking too far into the future I've been slacking off on implementing the anime seasons page, mostly out of laziness. I think I'd have to create another db file specifically for the seasons anyway, this code is being pushed *primarily* because the hard drive it's on is failing! yay :)
author Paper <paper@paper.us.eu.org>
date Mon, 01 Apr 2024 02:43:44 -0400
parents c130f47f6f48
children dd211ff68b36
comparison
equal deleted inserted replaced
257:699a20c57dc8 258:862d0d8619f6
4 #include "core/strings.h" 4 #include "core/strings.h"
5 #include "core/session.h" // locale 5 #include "core/session.h" // locale
6 6
7 #include <QByteArray> 7 #include <QByteArray>
8 #include <QDebug> 8 #include <QDebug>
9 #include <QLocale>
9 #include <QString> 10 #include <QString>
10 #include <QLocale>
11 11
12 #include <algorithm> 12 #include <algorithm>
13 #include <cctype> 13 #include <cctype>
14 #include <codecvt> 14 #include <codecvt>
15 #include <iostream>
15 #include <locale> 16 #include <locale>
16 #include <string> 17 #include <string>
18 #include <unordered_map>
17 #include <vector> 19 #include <vector>
18 #include <unordered_map>
19 #include <iostream>
20 20
21 namespace Strings { 21 namespace Strings {
22 22
23 /* ew */ 23 /* ew */
24 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { 24 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) {
49 } 49 }
50 50
51 return out; 51 return out;
52 } 52 }
53 53
54 std::vector<std::string> Split(const std::string &text, const std::string& delimiter) { 54 std::vector<std::string> Split(const std::string& text, const std::string& delimiter) {
55 if (text.length() < 1) 55 if (text.length() < 1)
56 return {}; 56 return {};
57 57
58 std::vector<std::string> tokens; 58 std::vector<std::string> tokens;
59 59
67 return tokens; 67 return tokens;
68 } 68 }
69 69
70 /* This function is really only used for cleaning up the synopsis of 70 /* This function is really only used for cleaning up the synopsis of
71 * horrible HTML debris from AniList :) 71 * horrible HTML debris from AniList :)
72 */ 72 */
73 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace) { 73 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace) {
74 size_t pos = 0; 74 size_t pos = 0;
75 while ((pos = string.find(find, pos)) != std::string::npos) { 75 while ((pos = string.find(find, pos)) != std::string::npos) {
76 string.replace(pos, find.length(), replace); 76 string.replace(pos, find.length(), replace);
77 pos += replace.length(); 77 pos += replace.length();
79 return string; 79 return string;
80 } 80 }
81 81
82 std::string SanitizeLineEndings(const std::string& string) { 82 std::string SanitizeLineEndings(const std::string& string) {
83 /* LOL */ 83 /* LOL */
84 return 84 return ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(ReplaceAll(string, "\r\n", "\n"), "</p>", "\n"), "<br>", "\n"),
85 ReplaceAll( 85 "<br />", "\n"),
86 ReplaceAll( 86 "\n\n\n", "\n\n");
87 ReplaceAll(
88 ReplaceAll(
89 ReplaceAll(string, "\r\n", "\n"),
90 "</p>", "\n"),
91 "<br>", "\n"),
92 "<br />", "\n"),
93 "\n\n\n", "\n\n");
94 } 87 }
95 88
96 /* removes dumb HTML tags because anilist is aids and 89 /* removes dumb HTML tags because anilist is aids and
97 * gives us HTML for synopses :/ 90 * gives us HTML for synopses :/
98 */ 91 */
99 std::string RemoveHtmlTags(std::string string) { 92 std::string RemoveHtmlTags(std::string string) {
100 while (string.find("<") != std::string::npos) { 93 while (string.find("<") != std::string::npos) {
101 auto startpos = string.find("<"); 94 auto startpos = string.find("<");
102 auto endpos = string.find(">") + 1; 95 auto endpos = string.find(">") + 1;
103 96
108 } 101 }
109 102
110 /* e.g. "&lt;" for "<" */ 103 /* e.g. "&lt;" for "<" */
111 std::string ParseHtmlEntities(std::string string) { 104 std::string ParseHtmlEntities(std::string string) {
112 const std::unordered_map<std::string, std::string> map = { 105 const std::unordered_map<std::string, std::string> map = {
113 /* The only one of these I can understand using are the first 106 /* The only one of these I can understand using are the first
114 * three. why do the rest of these exist? 107 * three. why do the rest of these exist?
115 * 108 *
116 * probably mojibake. 109 * probably mojibake.
117 */ 110 */
118 {"&lt;", "<"}, 111 {"&lt;", "<" },
119 {"&rt;", ">"}, 112 {"&rt;", ">" },
120 {"&nbsp;", "\xA0"}, 113 {"&nbsp;", "\xA0"},
121 {"&amp;", "&"}, 114 {"&amp;", "&" },
122 {"&quot;", "\""}, 115 {"&quot;", "\"" },
123 {"&apos;", "'"}, 116 {"&apos;", "'" },
124 {"&cent;", "¢"}, 117 {"&cent;", "¢" },
125 {"&pound;", "£"}, 118 {"&pound;", "£" },
126 {"&euro;", "€"}, 119 {"&euro;", "€" },
127 {"&yen;", "¥"}, 120 {"&yen;", "¥" },
128 {"&copy;", "©"}, 121 {"&copy;", "©" },
129 {"&reg;", "®"}, 122 {"&reg;", "®" },
130 {"&rsquo;", "’"} // Haibane Renmei, AniList 123 {"&rsquo;", "’" } // Haibane Renmei, AniList
131 }; 124 };
132 125
133 for (const auto& item : map) 126 for (const auto& item : map)
134 string = ReplaceAll(string, item.first, item.second); 127 string = ReplaceAll(string, item.first, item.second);
135 return string; 128 return string;
140 return ParseHtmlEntities(RemoveHtmlTags(SanitizeLineEndings(string))); 133 return ParseHtmlEntities(RemoveHtmlTags(SanitizeLineEndings(string)));
141 } 134 }
142 135
143 /* let Qt handle the heavy lifting of locale shit 136 /* let Qt handle the heavy lifting of locale shit
144 * I don't want to deal with 137 * I don't want to deal with
145 */ 138 */
146 std::string ToUpper(const std::string& string) { 139 std::string ToUpper(const std::string& string) {
147 return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string))); 140 return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string)));
148 } 141 }
149 142
150 std::string ToLower(const std::string& string) { 143 std::string ToLower(const std::string& string) {
202 } 195 }
203 196
204 /* util funcs */ 197 /* util funcs */
205 uint64_t HumanReadableSizeToBytes(const std::string& str) { 198 uint64_t HumanReadableSizeToBytes(const std::string& str) {
206 static const std::unordered_map<std::string, uint64_t> bytes_map = { 199 static const std::unordered_map<std::string, uint64_t> bytes_map = {
207 {"KB", 1ull << 10}, 200 {"KB", 1ull << 10},
208 {"MB", 1ull << 20}, 201 {"MB", 1ull << 20},
209 {"GB", 1ull << 30}, 202 {"GB", 1ull << 30},
210 {"TB", 1ull << 40}, 203 {"TB", 1ull << 40},
211 {"PB", 1ull << 50} /* surely we won't need more than this */ 204 {"PB", 1ull << 50} /* surely we won't need more than this */
212 }; 205 };
213 206
214 for (const auto& suffix : bytes_map) { 207 for (const auto& suffix : bytes_map) {
215 if (str.find(suffix.first) != std::string::npos) { 208 if (str.find(suffix.first) != std::string::npos) {
216 try { 209 try {