Mercurial > minori
annotate include/core/strings.h @ 216:8a482049b968
dep/animia: win/wayland: add UNTESTED support for wlroots protocols
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 07 Jan 2024 12:42:10 -0500 |
| parents | 7cf53145de11 |
| children | c39ad2a8587d |
| rev | line source |
|---|---|
| 9 | 1 #ifndef __core__strings_h |
| 2 #define __core__strings_h | |
| 15 | 3 |
| 9 | 4 #include <string> |
| 5 #include <vector> | |
|
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
6 #include <sstream> |
| 15 | 7 |
| 64 | 8 class QString; |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
9 class QByteArray; |
| 64 | 10 |
| 10 | 11 namespace Strings { |
| 15 | 12 |
| 9 | 13 /* 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
|
14 * into a string, separated by delimiters. |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
15 */ |
| 9 | 16 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter); |
|
118
39521c47c7a3
*: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents:
116
diff
changeset
|
17 std::vector<std::string> Split(const std::string &text, const std::string& delimiter); |
| 9 | 18 |
| 19 /* Substring removal functions */ | |
|
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
85
diff
changeset
|
20 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace); |
| 9 | 21 std::string SanitizeLineEndings(const std::string& string); |
|
100
f5940a575d83
track/constants: add many more video formats
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
22 std::string RemoveHtmlTags(std::string string); |
|
f5940a575d83
track/constants: add many more video formats
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
23 std::string ParseHtmlEntities(std::string string); |
| 9 | 24 |
| 25 /* stupid HTML bullshit */ | |
| 26 std::string TextifySynopsis(const std::string& string); | |
| 15 | 27 |
| 28 std::string ToUpper(const std::string& string); | |
| 29 std::string ToLower(const std::string& string); | |
| 30 | |
|
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
85
diff
changeset
|
31 /* 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
|
32 * 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
|
33 * 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
|
34 * code) |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
35 */ |
| 62 | 36 std::wstring ToWstring(const std::string& string); |
| 64 | 37 std::wstring ToWstring(const QString& string); |
| 62 | 38 std::string ToUtf8String(const std::wstring& wstring); |
| 64 | 39 std::string ToUtf8String(const QString& string); |
|
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
40 std::string ToUtf8String(const QByteArray& ba); |
| 64 | 41 QString ToQString(const std::string& string); |
| 42 QString ToQString(const std::wstring& wstring); | |
| 62 | 43 |
|
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
44 /* not really an "int"... but who cares? */ |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
45 template<typename T = int, |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
46 std::enable_if_t<std::is_integral<T>::value, bool> = true> |
|
122
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
47 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
|
48 std::istringstream s(str); |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
49 s >> std::noboolalpha >> def; |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
50 return def; |
|
122
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
51 } |
|
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
52 |
|
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
53 template<typename T, |
|
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
54 std::enable_if_t<std::is_integral<T>::value && !std::is_same<T, bool>::value, bool> = true> |
|
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); |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
66 |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
67 std::string RemoveLeadingChars(std::string s, const char c); |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
68 std::string RemoveTrailingChars(std::string s, const char c); |
|
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
69 |
| 102 | 70 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub); |
| 71 | |
| 85 | 72 }; // namespace Strings |
| 73 | |
|
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
74 #endif // __core__strings_h |
