Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
98:582b2fca1561 | 99:503bc1547d49 |
---|---|
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 namespace Strings { | 15 namespace Strings { |
16 | 16 |
17 /* ew */ | |
17 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { | 18 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { |
18 if (vector.size() < 1) | 19 if (vector.size() < 1) |
19 return "-"; | 20 return "-"; |
20 std::string out = ""; | 21 std::string out = ""; |
21 for (unsigned long long i = 0; i < vector.size(); i++) { | 22 for (unsigned long long i = 0; i < vector.size(); i++) { |
24 out.append(delimiter); | 25 out.append(delimiter); |
25 } | 26 } |
26 return out; | 27 return out; |
27 } | 28 } |
28 | 29 |
30 /* This function is really only used for cleaning up the synopsis of | |
31 horrible HTML debris from AniList :) */ | |
29 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace) { | 32 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace) { |
30 size_t pos = 0; | 33 size_t pos = 0; |
31 while ((pos = string.find(find, pos)) != std::string::npos) { | 34 while ((pos = string.find(find, pos)) != std::string::npos) { |
32 string.replace(pos, find.length(), replace); | 35 string.replace(pos, find.length(), replace); |
33 pos += replace.length(); | 36 pos += replace.length(); |
34 } | 37 } |
35 return string; | 38 return string; |
36 } | 39 } |
37 | 40 |
38 /* :) */ | |
39 std::string SanitizeLineEndings(const std::string& string) { | 41 std::string SanitizeLineEndings(const std::string& string) { |
40 return ReplaceAll(ReplaceAll(ReplaceAll(string, "\r\n", "\n"), "<br>", "\n"), "\n\n\n", "\n\n"); | 42 return ReplaceAll(ReplaceAll(ReplaceAll(string, "\r\n", "\n"), "<br>", "\n"), "\n\n\n", "\n\n"); |
41 } | 43 } |
42 | 44 |
43 /* removes dumb HTML tags because anilist is aids and | 45 /* removes dumb HTML tags because anilist is aids and |
44 gives us HTML for synopses :/ */ | 46 gives us HTML for synopses :/ */ |
45 std::string RemoveHtmlTags(const std::string& string) { | 47 std::string RemoveHtmlTags(std::string string) { |
46 std::string html(string); | 48 while (string.find("<") != std::string::npos) { |
47 while (html.find("<") != std::string::npos) { | 49 auto startpos = string.find("<"); |
48 auto startpos = html.find("<"); | 50 auto endpos = string.find(">") + 1; |
49 auto endpos = html.find(">") + 1; | |
50 | 51 |
51 if (endpos != std::string::npos) { | 52 if (endpos != std::string::npos) |
52 html.erase(startpos, endpos - startpos); | 53 string.erase(startpos, endpos - startpos); |
53 } | |
54 } | 54 } |
55 return html; | 55 return string; |
56 } | 56 } |
57 | 57 |
58 /* e.g. "<" for "<" */ | 58 /* e.g. "<" for "<" */ |
59 std::string ParseHtmlEntities(const std::string& string) { | 59 std::string ParseHtmlEntities(std::string string) { |
60 const std::unordered_map<std::string, std::string> map = { | 60 const std::unordered_map<std::string, std::string> map = { |
61 /* The only one of these I can understand using are the first | |
62 three. why do the rest of these exist? */ | |
61 {"<", "<"}, | 63 {"<", "<"}, |
62 {"&rt;", ">"}, | 64 {"&rt;", ">"}, |
63 {" ", "\xA0"}, | 65 {" ", "\xA0"}, |
64 {"&", "&"}, | 66 {"&", "&"}, |
65 {""", "\""}, | 67 {""", "\""}, |
71 {"©", "©"}, | 73 {"©", "©"}, |
72 {"®", "®"}, | 74 {"®", "®"}, |
73 {"’", "’"} // Haibane Renmei, AniList | 75 {"’", "’"} // Haibane Renmei, AniList |
74 }; | 76 }; |
75 | 77 |
76 std::string ret = string; | |
77 for (const auto& item : map) | 78 for (const auto& item : map) |
78 ret = ReplaceAll(ret, item.first, item.second); | 79 if (string.find(item.first) != std::string::npos) |
79 return ret; | 80 string = ReplaceAll(string, item.first, item.second); |
81 return string; | |
80 } | 82 } |
81 | 83 |
82 /* */ | 84 /* removes stupid HTML stuff */ |
83 std::string TextifySynopsis(const std::string& string) { | 85 std::string TextifySynopsis(const std::string& string) { |
84 return ParseHtmlEntities(RemoveHtmlTags(SanitizeLineEndings(string))); | 86 return ParseHtmlEntities(RemoveHtmlTags(SanitizeLineEndings(string))); |
85 } | 87 } |
86 | 88 |
87 /* let Qt handle the heavy lifting of locale shit | 89 /* let Qt handle the heavy lifting of locale shit |
88 I don't want to deal with */ | 90 I don't want to deal with */ |
89 std::string ToUpper(const std::string& string) { | 91 std::string ToUpper(const std::string& string) { |
90 /* todo: this "locale" will have to be moved to session.h */ | 92 /* todo: this "locale" will have to be moved to session.h |
93 it also defaults to en-US, which sucks very much for | |
94 anyone who doesn't speak american english... */ | |
91 QLocale locale; | 95 QLocale locale; |
92 return ToUtf8String(locale.toUpper(ToQString(string))); | 96 return ToUtf8String(locale.toUpper(ToQString(string))); |
93 } | 97 } |
94 | 98 |
95 std::string ToLower(const std::string& string) { | 99 std::string ToLower(const std::string& string) { |