Mercurial > minori
annotate include/core/strings.h @ 146:d8a61e7e2a36
dep/animia: move fd stuff to a new fd.cc, don't force the user
to include windows.h
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Mon, 13 Nov 2023 13:52:58 -0500 |
parents | bc218c9d2ea6 |
children | bc8d2ccff09c |
rev | line source |
---|---|
9 | 1 #ifndef __core__strings_h |
2 #define __core__strings_h | |
15 | 3 |
9 | 4 #include <string> |
5 #include <vector> | |
105
6d8da6e64d61
theme: add dark stylesheet, make it actually usable
Paper <mrpapersonic@gmail.com>
parents:
102
diff
changeset
|
6 #include <array> |
122
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
7 #include <limits> |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
8 #include <stdexcept> |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
9 #include <cstdint> |
15 | 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 |
17 into a string, separated by delimiters. */ | |
18 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
|
19 std::vector<std::string> Split(const std::string &text, const std::string& delimiter); |
9 | 20 |
21 /* Substring removal functions */ | |
98
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
85
diff
changeset
|
22 std::string ReplaceAll(std::string string, const std::string& find, const std::string& replace); |
9 | 23 std::string SanitizeLineEndings(const std::string& string); |
100
f5940a575d83
track/constants: add many more video formats
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
24 std::string RemoveHtmlTags(std::string string); |
f5940a575d83
track/constants: add many more video formats
Paper <mrpapersonic@gmail.com>
parents:
98
diff
changeset
|
25 std::string ParseHtmlEntities(std::string string); |
9 | 26 |
27 /* stupid HTML bullshit */ | |
28 std::string TextifySynopsis(const 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 |
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents:
85
diff
changeset
|
34 different string formats universal */ |
62 | 35 std::wstring ToWstring(const std::string& string); |
64 | 36 std::wstring ToWstring(const QString& string); |
62 | 37 std::string ToUtf8String(const std::wstring& wstring); |
64 | 38 std::string ToUtf8String(const QString& string); |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
39 std::string ToUtf8String(const QByteArray& ba); |
64 | 40 QString ToQString(const std::string& string); |
41 QString ToQString(const std::wstring& wstring); | |
62 | 42 |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
43 /* arithmetic :) */ |
122
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
44 template<typename T = int> |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
45 T ToInt(const std::string& str, T def = 0) { |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
46 auto clamp = [](const T& val, const T& min, const T& max){ |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
47 return std::max(min, std::min(val, max)); |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
48 }; |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
49 |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
50 try { |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
51 if constexpr (std::is_signed<T>::value) { |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
52 return clamp(std::stoll(str), std::numeric_limits<T>::min(), std::numeric_limits<T>::max()); |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
53 } else if constexpr (std::is_unsigned<T>::value) { |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
54 return clamp(std::stoull(str), std::numeric_limits<T>::max(), std::numeric_limits<T>::max()); |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
55 } else { |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
56 throw std::invalid_argument("it no worky"); |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
57 } |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
58 } catch (std::invalid_argument const& ex) { |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
59 return def; |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
60 } |
bc218c9d2ea6
strings: convert ToInt() to be a template
Paper <mrpapersonic@gmail.com>
parents:
118
diff
changeset
|
61 } |
101
c537996cf67b
*: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents:
100
diff
changeset
|
62 |
116
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
63 bool ToBool(const std::string& s, const bool def = false); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
64 std::string ToUtf8String(const bool b); |
254b1d2b7096
settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents:
114
diff
changeset
|
65 |
114
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
66 uint64_t HumanReadableSizeToBytes(const std::string& str); |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
67 |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
68 std::string RemoveLeadingChars(std::string s, const char c); |
ab191e28e69d
*: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents:
105
diff
changeset
|
69 std::string RemoveTrailingChars(std::string s, const char c); |
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 | |
85 | 73 }; // namespace Strings |
74 | |
9 | 75 #endif // __core__strings_h |