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