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