comparison src/core/strings.cc @ 274:f6a756c19bfb

anime_list.cc: use mutexes so we don't sex the stack strings.cc: use Qt to convert from HTML to plain text.
author Paper <paper@paper.us.eu.org>
date Thu, 18 Apr 2024 17:24:42 -0400
parents f31305b9f60a
children 1b5c04268d6a
comparison
equal deleted inserted replaced
273:f31305b9f60a 274:f6a756c19bfb
6 6
7 #include <QByteArray> 7 #include <QByteArray>
8 #include <QDebug> 8 #include <QDebug>
9 #include <QLocale> 9 #include <QLocale>
10 #include <QString> 10 #include <QString>
11 #include <QTextDocument>
11 12
12 #include <algorithm> 13 #include <algorithm>
13 #include <cctype> 14 #include <cctype>
14 #include <codecvt> 15 #include <codecvt>
15 #include <iostream> 16 #include <iostream>
146 } 147 }
147 } 148 }
148 149
149 /* e.g. "&lt;" for "<" */ 150 /* e.g. "&lt;" for "<" */
150 void ParseHtmlEntities(std::string& string) { 151 void ParseHtmlEntities(std::string& string) {
151 /* The only one of these I can understand using are the first
152 * three. why do the rest of these exist?
153 *
154 * probably mojibake.
155 */
156 const std::unordered_map<std::string, std::string> map = { 152 const std::unordered_map<std::string, std::string> map = {
157 {"&lt;", "<" }, 153 {"&lt;", "<"},
158 {"&rt;", ">" }, 154 {"&rt;", ">"},
159 {"&nbsp;", "\xA0"}, 155 {"&nbsp;", "\xA0"},
160 {"&amp;", "&" }, 156 {"&amp;", "&"},
161 {"&quot;", "\"" }, 157 {"&quot;", "\""},
162 {"&apos;", "'" }, 158 {"&apos;", "'"},
163 {"&cent;", "¢" }, 159 {"&cent;", "¢"},
164 {"&pound;", "£" }, 160 {"&pound;", "£"},
165 {"&euro;", "€" }, 161 {"&euro;", "€"},
166 {"&yen;", "¥" }, 162 {"&yen;", "¥"},
167 {"&copy;", "©" }, 163 {"&copy;", "©"},
168 {"&reg;", "®" }, 164 {"&reg;", "®"},
165 {"&times;", "×"},
169 {"&rsquo;", "’" } // Haibane Renmei, AniList 166 {"&rsquo;", "’" } // Haibane Renmei, AniList
170 }; 167 };
171 168
172 for (const auto& item : map) 169 for (const auto& item : map)
173 ReplaceAll(string, item.first, item.second); 170 ReplaceAll(string, item.first, item.second);
174 } 171 }
175 172
176 /* removes stupid HTML stuff */ 173 /* removes stupid HTML stuff */
177 void TextifySynopsis(std::string& string) { 174 void TextifySynopsis(std::string& string) {
175 #if 1
176 /* Just let Qt deal with it. */
177 QTextDocument text;
178 text.setHtml(Strings::ToQString(string));
179 string = Strings::ToUtf8String(text.toPlainText());
180 #else
181 /* old implementation here... beware of jankiness */
178 SanitizeLineEndings(string); 182 SanitizeLineEndings(string);
179 RemoveHtmlTags(string); 183 RemoveHtmlTags(string);
180 ParseHtmlEntities(string); 184 ParseHtmlEntities(string);
185 #endif
181 } 186 }
182 187
183 /* let Qt handle the heavy lifting of locale shit 188 /* let Qt handle the heavy lifting of locale shit
184 * I don't want to deal with 189 * I don't want to deal with
185 */ 190 */
271 } 276 }
272 277
273 std::string BytesToHumanReadableSize(uint64_t bytes, int precision) { 278 std::string BytesToHumanReadableSize(uint64_t bytes, int precision) {
274 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) 279 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
275 /* QLocale in Qt >= 5.10.0 has a function for this */ 280 /* QLocale in Qt >= 5.10.0 has a function for this */
276 return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes), precision); 281 return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision));
277 #else 282 #else
278 static const std::unordered_map<uint64_t, std::string> map = { 283 static const std::unordered_map<uint64_t, std::string> map = {
279 {1ull << 10, "KiB"}, 284 {1ull << 10, "KiB"},
280 {1ull << 20, "MiB"}, 285 {1ull << 20, "MiB"},
281 {1ull << 30, "GiB"}, 286 {1ull << 30, "GiB"},