Mercurial > minori
annotate src/core/strings.cc @ 269:3efac0541151
chore: merge
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 12 Apr 2024 05:23:45 -0400 |
parents | 9a04802848c0 |
children | f31305b9f60a |
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> |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
11 |
15 | 12 #include <algorithm> |
13 #include <cctype> | |
62 | 14 #include <codecvt> |
258 | 15 #include <iostream> |
9 | 16 #include <locale> |
17 #include <string> | |
258 | 18 #include <unordered_map> |
9 | 19 #include <vector> |
20 | |
264 | 21 #include "utf8proc.h" |
22 | |
9 | 23 namespace Strings { |
24 | |
99
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
25 /* ew */ |
9 | 26 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { |
27 if (vector.size() < 1) | |
250 | 28 return ""; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
29 |
221
53211cb1e7f5
library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents:
211
diff
changeset
|
30 std::string out; |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
31 |
9 | 32 for (unsigned long long i = 0; i < vector.size(); i++) { |
33 out.append(vector.at(i)); | |
34 if (i < vector.size() - 1) | |
35 out.append(delimiter); | |
36 } | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
37 |
9 | 38 return out; |
39 } | |
40 | |
226 | 41 std::string Implode(const std::set<std::string>& set, const std::string& delimiter) { |
42 if (set.size() < 1) | |
250 | 43 return ""; |
226 | 44 |
45 std::string out; | |
46 | |
47 for (auto it = set.cbegin(); it != set.cend(); it++) { | |
48 out.append(*it); | |
49 if (it != std::prev(set.cend(), 1)) | |
50 out.append(delimiter); | |
51 } | |
52 | |
53 return out; | |
54 } | |
55 | |
258 | 56 std::vector<std::string> Split(const std::string& text, const std::string& delimiter) { |
250 | 57 if (text.length() < 1) |
58 return {}; | |
59 | |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
60 std::vector<std::string> tokens; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
61 |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
62 std::size_t start = 0, end = 0; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
63 while ((end = text.find(delimiter, start)) != std::string::npos) { |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
64 tokens.push_back(text.substr(start, end - start)); |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
65 start = end + delimiter.length(); |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
66 } |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
67 tokens.push_back(text.substr(start)); |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
68 |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
69 return tokens; |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
70 } |
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
71 |
99
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
72 /* 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
|
73 * horrible HTML debris from AniList :) |
258 | 74 */ |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
75 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
|
76 size_t pos = 0; |
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
77 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
|
78 string.replace(pos, find.length(), replace); |
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
79 pos += replace.length(); |
9 | 80 } |
81 } | |
82 | |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
83 void SanitizeLineEndings(std::string& string) { |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
84 /* LOL */ |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
85 ReplaceAll(string, "\r\n", "\n"); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
86 ReplaceAll(string, "</p>", "\n"); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
87 ReplaceAll(string, "<br>", "\n"); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
88 ReplaceAll(string, "<br />", "\n"); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
89 ReplaceAll(string, "\n\n\n", "\n\n"); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
90 } |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
91 |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
92 void ConvertRomanNumerals(std::string& string) { |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
93 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
|
94 {"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
|
95 {"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
|
96 {"13", "XIII"} |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
97 }; |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
98 |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
99 for (const auto& item : vec) |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
100 ReplaceAll(string, item.second, item.first); |
9 | 101 } |
102 | |
264 | 103 /* this also performs case folding, so our string is lowercase after this */ |
104 void NormalizeUnicode(std::string& string) { | |
105 static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>( | |
106 UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE | | |
107 UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK | | |
108 UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS | |
109 ); | |
110 | |
111 /* ack */ | |
112 utf8proc_uint8_t* buf = nullptr; | |
113 | |
114 const utf8proc_ssize_t size = utf8proc_map( | |
115 reinterpret_cast<const utf8proc_uint8_t*>(string.data()), | |
116 string.size(), | |
117 &buf, | |
118 options | |
119 ); | |
120 | |
121 if (size) | |
122 string = std::string(reinterpret_cast<const char*>(buf), size); | |
123 | |
124 if (buf) | |
125 free(buf); | |
126 } | |
127 | |
128 void NormalizeAnimeTitle(std::string& string) { | |
129 ConvertRomanNumerals(string); | |
130 NormalizeUnicode(string); | |
131 RemoveLeadingChars(string, ' '); | |
132 RemoveTrailingChars(string, ' '); | |
133 } | |
134 | |
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
135 /* removes dumb HTML tags because anilist is aids and |
250 | 136 * gives us HTML for synopses :/ |
258 | 137 */ |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
138 void RemoveHtmlTags(std::string& string) { |
99
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
139 while (string.find("<") != std::string::npos) { |
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
140 auto startpos = string.find("<"); |
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
141 auto endpos = string.find(">") + 1; |
9 | 142 |
99
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
143 if (endpos != std::string::npos) |
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
144 string.erase(startpos, endpos - startpos); |
9 | 145 } |
146 } | |
147 | |
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
148 /* e.g. "<" for "<" */ |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
149 void ParseHtmlEntities(std::string& string) { |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
150 /* The only one of these I can understand using are the first |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
151 * three. why do the rest of these exist? |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
152 * |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
153 * probably mojibake. |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
154 */ |
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
155 const std::unordered_map<std::string, std::string> map = { |
258 | 156 {"<", "<" }, |
157 {"&rt;", ">" }, | |
158 {" ", "\xA0"}, | |
159 {"&", "&" }, | |
160 {""", "\"" }, | |
161 {"'", "'" }, | |
162 {"¢", "¢" }, | |
163 {"£", "£" }, | |
164 {"€", "€" }, | |
165 {"¥", "Â¥" }, | |
166 {"©", "©" }, | |
167 {"®", "®" }, | |
168 {"’", "’" } // Haibane Renmei, AniList | |
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
169 }; |
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
170 |
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
171 for (const auto& item : map) |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
172 ReplaceAll(string, item.first, item.second); |
9 | 173 } |
174 | |
99
503bc1547d49
strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
175 /* removes stupid HTML stuff */ |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
176 void TextifySynopsis(std::string& string) { |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
177 SanitizeLineEndings(string); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
178 RemoveHtmlTags(string); |
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
179 ParseHtmlEntities(string); |
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
180 } |
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
181 |
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
182 /* 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
|
183 * I don't want to deal with |
258 | 184 */ |
15 | 185 std::string ToUpper(const std::string& string) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
186 return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string))); |
15 | 187 } |
188 | |
189 std::string ToLower(const std::string& string) { | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
190 return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string))); |
15 | 191 } |
192 | |
62 | 193 std::wstring ToWstring(const std::string& string) { |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
194 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
|
195 |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
196 std::wstring wstr; |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
197 try { |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
198 wstr = converter.from_bytes(string); |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
199 } catch (std::range_error const& ex) { |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
200 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
|
201 } |
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
202 return wstr; |
62 | 203 } |
204 | |
64 | 205 std::wstring ToWstring(const QString& string) { |
206 std::wstring arr(string.size(), L'\0'); | |
207 string.toWCharArray(&arr.front()); | |
208 return arr; | |
209 } | |
210 | |
62 | 211 std::string ToUtf8String(const std::wstring& wstring) { |
230
2f5a9247e501
torrents: implement download button
Paper <paper@paper.us.eu.org>
parents:
221
diff
changeset
|
212 static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L""); |
62 | 213 return converter.to_bytes(wstring); |
214 } | |
215 | |
64 | 216 std::string ToUtf8String(const QString& string) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
217 const QByteArray ba = string.toUtf8(); |
77 | 218 return std::string(ba.constData(), ba.size()); |
219 } | |
220 | |
221 std::string ToUtf8String(const QByteArray& ba) { | |
222 return std::string(ba.constData(), ba.size()); | |
64 | 223 } |
224 | |
225 QString ToQString(const std::string& string) { | |
226 return QString::fromUtf8(string.c_str(), string.length()); | |
227 } | |
228 | |
229 QString ToQString(const std::wstring& wstring) { | |
230 return QString::fromWCharArray(wstring.c_str(), wstring.length()); | |
231 } | |
232 | |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
233 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
|
234 return b ? "true" : "false"; // lol |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
235 } |
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
236 |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
237 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
|
238 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
|
239 s >> std::boolalpha >> def; |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
240 return def; |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
241 } |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
242 |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
187
diff
changeset
|
243 /* util funcs */ |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
244 uint64_t HumanReadableSizeToBytes(const std::string& str) { |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
245 static const std::unordered_map<std::string, uint64_t> bytes_map = { |
258 | 246 {"KB", 1ull << 10}, |
247 {"MB", 1ull << 20}, | |
248 {"GB", 1ull << 30}, | |
249 {"TB", 1ull << 40}, | |
250 {"PB", 1ull << 50} /* surely we won't need more than this */ | |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
251 }; |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
252 |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
253 for (const auto& suffix : bytes_map) { |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
254 if (str.find(suffix.first) != std::string::npos) { |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
255 try { |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
256 uint64_t size = std::stod(str) * suffix.second; |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
257 return size; |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
258 } catch (std::invalid_argument const& ex) { |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
259 continue; |
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 } |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
262 } |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
263 |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
264 return ToInt(str, 0); |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
265 } |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
266 |
264 | 267 void RemoveLeadingChars(std::string& s, const char c) { |
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
268 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
|
269 } |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
270 |
264 | 271 void RemoveTrailingChars(std::string& s, const char c) { |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
272 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
|
273 } |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
274 |
102 | 275 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub) { |
276 for (unsigned long long i = 0; i < str.length() && i < sub.length(); i++) | |
277 if (str[i] != sub[i]) | |
278 return false; | |
187
9613d72b097e
*: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents:
120
diff
changeset
|
279 |
102 | 280 return true; |
281 } | |
282 | |
9 | 283 } // namespace Strings |