Mercurial > minori
annotate src/core/strings.cc @ 365:f81bed4e04ac
*: megacommit that probably breaks things
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Wed, 02 Oct 2024 23:06:43 -0400 |
| parents | a0aa8c8c4307 |
| children | 47c9f8502269 |
| rev | line source |
|---|---|
| 9 | 1 /** |
| 2 * strings.cpp: Useful functions for manipulating strings | |
| 3 **/ | |
| 4 #include "core/strings.h" | |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
5 #include "core/session.h" // locale |
|
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
6 |
| 64 | 7 #include <QByteArray> |
|
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
8 #include <QDebug> |
| 258 | 9 #include <QLocale> |
| 64 | 10 #include <QString> |
|
274
f6a756c19bfb
anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents:
273
diff
changeset
|
11 #include <QTextDocument> |
|
322
c32467cd06bb
core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
12 #include <QCoreApplication> |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
13 |
| 15 | 14 #include <algorithm> |
| 15 #include <cctype> | |
| 62 | 16 #include <codecvt> |
| 258 | 17 #include <iostream> |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
18 #include <iomanip> |
| 9 | 19 #include <locale> |
| 20 #include <string> | |
| 258 | 21 #include <unordered_map> |
| 9 | 22 #include <vector> |
| 23 | |
| 264 | 24 #include "utf8proc.h" |
| 25 | |
| 9 | 26 namespace Strings { |
| 27 | |
|
99
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
28 /* ew */ |
| 9 | 29 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { |
| 30 if (vector.size() < 1) | |
| 250 | 31 return ""; |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
32 |
|
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
33 std::string out; |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
34 |
| 9 | 35 for (unsigned long long i = 0; i < vector.size(); i++) { |
| 36 out.append(vector.at(i)); | |
| 37 if (i < vector.size() - 1) | |
| 38 out.append(delimiter); | |
| 39 } | |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
40 |
| 9 | 41 return out; |
| 42 } | |
| 43 | |
| 258 | 44 std::vector<std::string> Split(const std::string& text, const std::string& delimiter) { |
| 250 | 45 if (text.length() < 1) |
| 46 return {}; | |
| 47 | |
|
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
48 std::vector<std::string> tokens; |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
49 |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
50 std::size_t start = 0, end = 0; |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
51 while ((end = text.find(delimiter, start)) != std::string::npos) { |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
52 tokens.push_back(text.substr(start, end - start)); |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
53 start = end + delimiter.length(); |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
54 } |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
55 tokens.push_back(text.substr(start)); |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
56 |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
57 return tokens; |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
58 } |
|
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
59 |
|
99
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
60 /* This function is really only used for cleaning up the synopsis of |
|
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
61 * horrible HTML debris from AniList :) |
| 258 | 62 */ |
|
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
63 void ReplaceAll(std::string& string, std::string_view find, std::string_view replace) { |
|
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
64 size_t pos = 0; |
|
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
65 while ((pos = string.find(find, pos)) != std::string::npos) { |
|
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
66 string.replace(pos, find.length(), replace); |
|
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
67 pos += replace.length(); |
| 9 | 68 } |
| 69 } | |
| 70 | |
|
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
71 void ConvertRomanNumerals(std::string& string) { |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
72 static const std::vector<std::pair<std::string_view, std::string_view>> vec = { |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
73 {"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"}, |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
74 {"7", "VII"}, {"8", "VIII"}, {"9", "IX"}, {"11", "XI"}, {"12", "XII"}, |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
75 {"13", "XIII"} |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
76 }; |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
77 |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
78 for (const auto& item : vec) |
|
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
79 ReplaceAll(string, item.second, item.first); |
| 9 | 80 } |
| 81 | |
| 264 | 82 /* this also performs case folding, so our string is lowercase after this */ |
| 83 void NormalizeUnicode(std::string& string) { | |
| 84 static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>( | |
|
365
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
85 UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE | |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
86 UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK | |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
87 UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS |
| 264 | 88 ); |
| 89 | |
| 90 /* ack */ | |
| 91 utf8proc_uint8_t* buf = nullptr; | |
| 92 | |
| 93 const utf8proc_ssize_t size = utf8proc_map( | |
| 94 reinterpret_cast<const utf8proc_uint8_t*>(string.data()), | |
| 95 string.size(), | |
| 96 &buf, | |
| 97 options | |
| 98 ); | |
| 99 | |
|
365
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
100 if (buf) { |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
101 if (size) |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
102 string.assign(reinterpret_cast<const char*>(buf), size); |
| 264 | 103 |
|
365
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
104 std::free(buf); |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
105 } |
| 264 | 106 } |
| 107 | |
| 108 void NormalizeAnimeTitle(std::string& string) { | |
| 109 ConvertRomanNumerals(string); | |
| 110 NormalizeUnicode(string); | |
| 111 RemoveLeadingChars(string, ' '); | |
| 112 RemoveTrailingChars(string, ' '); | |
| 113 } | |
| 114 | |
|
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
115 void TextifySynopsis(std::string& string) { |
|
274
f6a756c19bfb
anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents:
273
diff
changeset
|
116 /* Just let Qt deal with it. */ |
|
f6a756c19bfb
anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents:
273
diff
changeset
|
117 QTextDocument text; |
|
f6a756c19bfb
anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents:
273
diff
changeset
|
118 text.setHtml(Strings::ToQString(string)); |
|
f6a756c19bfb
anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents:
273
diff
changeset
|
119 string = Strings::ToUtf8String(text.toPlainText()); |
|
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
120 } |
|
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
121 |
|
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
122 /* let Qt handle the heavy lifting of locale shit |
|
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
123 * I don't want to deal with |
| 258 | 124 */ |
| 15 | 125 std::string ToUpper(const std::string& string) { |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
126 return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string))); |
| 15 | 127 } |
| 128 | |
| 129 std::string ToLower(const std::string& string) { | |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
130 return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string))); |
| 15 | 131 } |
| 132 | |
| 62 | 133 std::wstring ToWstring(const std::string& string) { |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
134 static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L""); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
135 |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
136 std::wstring wstr; |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
137 try { |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
138 wstr = converter.from_bytes(string); |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
139 } catch (std::range_error const& ex) { |
|
347
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
140 /* XXX how? */ |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
141 std::cerr << "Failed to convert UTF-8 to wide string!" << std::endl; |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
142 } |
|
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
143 return wstr; |
| 62 | 144 } |
| 145 | |
| 64 | 146 std::wstring ToWstring(const QString& string) { |
| 147 std::wstring arr(string.size(), L'\0'); | |
| 148 string.toWCharArray(&arr.front()); | |
| 149 return arr; | |
| 150 } | |
| 151 | |
| 62 | 152 std::string ToUtf8String(const std::wstring& wstring) { |
|
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
153 static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L""); |
| 62 | 154 return converter.to_bytes(wstring); |
| 155 } | |
| 156 | |
|
347
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
157 std::string ToUtf8String(const std::u32string& u32string) { |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
158 static std::wstring_convert<std::codecvt_utf8_utf16<char32_t>, char32_t> converter; |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
159 return converter.to_bytes(u32string); |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
160 } |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
161 |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
162 std::u32string ToUcs4String(const std::string& string) { |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
163 static std::wstring_convert<std::codecvt_utf8_utf16<char32_t>, char32_t> converter; |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
164 return converter.from_bytes(string); |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
165 } |
|
a0aa8c8c4307
dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents:
322
diff
changeset
|
166 |
| 64 | 167 std::string ToUtf8String(const QString& string) { |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
168 const QByteArray ba = string.toUtf8(); |
| 77 | 169 return std::string(ba.constData(), ba.size()); |
| 170 } | |
| 171 | |
| 172 std::string ToUtf8String(const QByteArray& ba) { | |
| 173 return std::string(ba.constData(), ba.size()); | |
| 64 | 174 } |
| 175 | |
| 176 QString ToQString(const std::string& string) { | |
| 177 return QString::fromUtf8(string.c_str(), string.length()); | |
| 178 } | |
| 179 | |
| 180 QString ToQString(const std::wstring& wstring) { | |
| 181 return QString::fromWCharArray(wstring.c_str(), wstring.length()); | |
| 182 } | |
| 183 | |
|
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
184 std::string ToUtf8String(const bool b) { |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
185 return b ? "true" : "false"; // lol |
|
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
186 } |
|
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
187 |
|
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
188 bool ToBool(const std::string& str, bool def) { |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
189 std::istringstream s(Strings::ToLower(str)); |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
190 s >> std::boolalpha >> def; |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
191 return def; |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
192 } |
|
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
193 |
|
365
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
194 template<typename T> |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
195 constexpr T ipow(T num, unsigned int pow) { |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
196 return (pow >= sizeof(unsigned int)*8) ? 0 : |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
197 pow == 0 ? 1 : num * ipow(num, pow-1); |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
198 } |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
199 |
|
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
200 /* util funcs */ |
|
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
201 uint64_t HumanReadableSizeToBytes(const std::string& str) { |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
202 static const std::unordered_map<std::string, uint64_t> bytes_map = { |
|
365
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
203 {"KB", 1e3}, |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
204 {"MB", 1e6}, |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
205 {"GB", 1e9}, |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
206 {"TB", 1e12}, |
|
f81bed4e04ac
*: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents:
347
diff
changeset
|
207 {"PB", 1e15}, |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
208 {"KiB", 1ull << 10}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
209 {"MiB", 1ull << 20}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
210 {"GiB", 1ull << 30}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
211 {"TiB", 1ull << 40}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
212 {"PiB", 1ull << 50} /* surely we won't need more than this */ |
|
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
213 }; |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
214 |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
215 for (const auto& suffix : bytes_map) { |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
216 if (str.find(suffix.first) != std::string::npos) { |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
217 try { |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
218 uint64_t size = std::stod(str) * suffix.second; |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
219 return size; |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
220 } catch (std::invalid_argument const& ex) { |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
221 continue; |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
222 } |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
223 } |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
224 } |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
225 |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
226 return ToInt(str, 0); |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
227 } |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
228 |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
229 std::string BytesToHumanReadableSize(uint64_t bytes, int precision) { |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
230 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
231 /* QLocale in Qt >= 5.10.0 has a function for this */ |
|
274
f6a756c19bfb
anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents:
273
diff
changeset
|
232 return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision)); |
|
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
233 #else |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
234 static const std::unordered_map<uint64_t, std::string> map = { |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
235 {1ull << 10, "KiB"}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
236 {1ull << 20, "MiB"}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
237 {1ull << 30, "GiB"}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
238 {1ull << 40, "TiB"}, |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
239 {1ull << 50, "PiB"} |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
240 }; |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
241 |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
242 for (const auto& suffix : map) { |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
243 if (bytes / suffix.first < 1) |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
244 continue; |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
245 |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
246 std::stringstream ss; |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
247 ss << std::setprecision(precision) |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
248 << (static_cast<double>(bytes) / suffix.first) << " " |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
249 << suffix.second; |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
250 return ss.str(); |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
251 } |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
252 |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
253 /* better luck next time */ |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
254 return "0 bytes"; |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
255 #endif |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
256 } |
|
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
257 |
| 264 | 258 void RemoveLeadingChars(std::string& s, const char c) { |
|
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
259 s.erase(0, std::min(s.find_first_not_of(c), s.size() - 1)); |
|
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
260 } |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
261 |
| 264 | 262 void RemoveTrailingChars(std::string& s, const char c) { |
|
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
263 s.erase(s.find_last_not_of(c) + 1, std::string::npos); |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
264 } |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
265 |
| 102 | 266 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub) { |
| 267 for (unsigned long long i = 0; i < str.length() && i < sub.length(); i++) | |
| 268 if (str[i] != sub[i]) | |
| 269 return false; | |
|
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
270 |
| 102 | 271 return true; |
| 272 } | |
| 273 | |
|
322
c32467cd06bb
core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
274 std::string Translate(const char* str) { |
|
c32467cd06bb
core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
275 return Strings::ToUtf8String(QCoreApplication::tr(str)); |
|
c32467cd06bb
core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
276 } |
|
c32467cd06bb
core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
277 |
| 9 | 278 } // namespace Strings |
