Mercurial > minori
annotate include/core/strings.h @ 327:b5d6c27c308f
anime: refactor Anime::SeriesSeason to Season class
ToLocalString has also been altered to take in both season
and year because lots of locales actually treat formatting
seasons differently! most notably is Russian which adds a
suffix at the end to notate seasons(??)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Thu, 13 Jun 2024 01:49:18 -0400 |
parents | c32467cd06bb |
children | a0aa8c8c4307 |
rev | line source |
---|---|
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
260
diff
changeset
|
1 #ifndef MINORI_CORE_STRINGS_H_ |
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
260
diff
changeset
|
2 #define MINORI_CORE_STRINGS_H_ |
15 | 3 |
258 | 4 #include <set> |
5 #include <sstream> | |
9 | 6 #include <string> |
7 #include <vector> | |
15 | 8 |
218 | 9 #include <cstdint> |
10 | |
64 | 11 class QString; |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
12 class QByteArray; |
64 | 13 |
10 | 14 namespace Strings { |
15 | 15 |
9 | 16 /* Implode function: takes a vector of strings and turns it |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
17 * into a string, separated by delimiters. |
258 | 18 */ |
9 | 19 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter); |
258 | 20 std::vector<std::string> Split(const std::string& text, const std::string& delimiter); |
9 | 21 |
22 /* Substring removal functions */ | |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
23 void ReplaceAll(std::string& string, std::string_view find, std::string_view replace); |
264 | 24 void NormalizeUnicode(std::string& string); |
25 void NormalizeAnimeTitle(std::string& string); | |
9 | 26 |
27 /* stupid HTML bullshit */ | |
260
dd211ff68b36
pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents:
258
diff
changeset
|
28 void TextifySynopsis(std::string& string); |
15 | 29 |
30 std::string ToUpper(const std::string& string); | |
31 std::string ToLower(const std::string& string); | |
32 | |
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
85
diff
changeset
|
33 /* functions that make the way we convert from and to |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
34 * different string formats universal (and these functions |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
35 * typically do things the right way so we avoid retarded |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
36 * code) |
258 | 37 */ |
62 | 38 std::wstring ToWstring(const std::string& string); |
64 | 39 std::wstring ToWstring(const QString& string); |
62 | 40 std::string ToUtf8String(const std::wstring& wstring); |
64 | 41 std::string ToUtf8String(const QString& string); |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
42 std::string ToUtf8String(const QByteArray& ba); |
64 | 43 QString ToQString(const std::string& string); |
44 QString ToQString(const std::wstring& wstring); | |
62 | 45 |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
46 /* not really an "int"... but who cares? */ |
320
1b5c04268d6a
services/kitsu: ACTUALLY finish GetAnimeList
Paper <paper@paper.us.eu.org>
parents:
273
diff
changeset
|
47 template<typename T = int, std::enable_if_t<std::is_arithmetic<T>::value, bool> = true> |
122
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
48 T ToInt(const std::string& str, T def = 0) { |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
49 std::istringstream s(str); |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
50 s >> std::noboolalpha >> def; |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
51 return def; |
122
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
52 } |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
53 |
258 | 54 template<typename T, std::enable_if_t<std::is_integral<T>::value && !std::is_same<T, bool>::value, bool> = true> |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
55 std::string ToUtf8String(T i) { |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
56 std::ostringstream s; |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
57 s << i; |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
58 return s.str(); |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
59 } |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
60 |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
61 bool ToBool(const std::string& s, bool def); |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
62 |
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
63 std::string ToUtf8String(bool b); |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
64 |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
65 uint64_t HumanReadableSizeToBytes(const std::string& str); |
273
f31305b9f60a
*: various code safety changes
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
66 std::string BytesToHumanReadableSize(uint64_t bytes, int precision = 2); |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
67 |
264 | 68 void RemoveLeadingChars(std::string& s, const char c); |
69 void RemoveTrailingChars(std::string& s, const char c); | |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
70 |
102 | 71 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub); |
72 | |
322
c32467cd06bb
core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents:
320
diff
changeset
|
73 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
|
74 |
85 | 75 }; // namespace Strings |
76 | |
261
3ec7804abf17
include: make header guards more sane
Paper <paper@paper.us.eu.org>
parents:
260
diff
changeset
|
77 #endif // MINORI_CORE_STRINGS_H_ |