Mercurial > minori
annotate include/core/strings.h @ 301:b1f625b0227c
*: convert all files CRLF -> LF
some files were in DOS format, others were in unix. now everything
(that at least is under our control) should all be the same format
| author | Paper <paper@paper.us.eu.org> | 
|---|---|
| date | Mon, 13 May 2024 15:04:51 -0400 | 
| parents | f31305b9f60a | 
| children | 1b5c04268d6a | 
| rev | line source | 
|---|---|
| 261 
3ec7804abf17
include: make header guards more sane
 Paper <paper@paper.us.eu.org> parents: 
260diff
changeset | 1 #ifndef MINORI_CORE_STRINGS_H_ | 
| 
3ec7804abf17
include: make header guards more sane
 Paper <paper@paper.us.eu.org> parents: 
260diff
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: 
114diff
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: 
178diff
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); | 
| 226 | 20 std::string Implode(const std::set<std::string>& set, const std::string& delimiter); | 
| 258 | 21 std::vector<std::string> Split(const std::string& text, const std::string& delimiter); | 
| 9 | 22 | 
| 23 /* Substring removal functions */ | |
| 260 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 24 void ReplaceAll(std::string& string, std::string_view find, std::string_view replace); | 
| 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 25 void SanitizeLineEndings(std::string& string); | 
| 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 26 void RemoveHtmlTags(std::string& string); | 
| 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 27 void ParseHtmlEntities(std::string& string); | 
| 264 | 28 void NormalizeUnicode(std::string& string); | 
| 29 void NormalizeAnimeTitle(std::string& string); | |
| 9 | 30 | 
| 31 /* stupid HTML bullshit */ | |
| 260 
dd211ff68b36
pages/seasons: add initial functionality
 Paper <paper@paper.us.eu.org> parents: 
258diff
changeset | 32 void TextifySynopsis(std::string& string); | 
| 15 | 33 | 
| 34 std::string ToUpper(const std::string& string); | |
| 35 std::string ToLower(const std::string& string); | |
| 36 | |
| 98 
582b2fca1561
strings: parse HTML entities when reading synopsis, make the
 Paper <mrpapersonic@gmail.com> parents: 
85diff
changeset | 37 /* 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: 
178diff
changeset | 38 * different string formats universal (and these functions | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 39 * 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: 
178diff
changeset | 40 * code) | 
| 258 | 41 */ | 
| 62 | 42 std::wstring ToWstring(const std::string& string); | 
| 64 | 43 std::wstring ToWstring(const QString& string); | 
| 62 | 44 std::string ToUtf8String(const std::wstring& wstring); | 
| 64 | 45 std::string ToUtf8String(const QString& string); | 
| 116 
254b1d2b7096
settings: add torrents page, make rss feed configurable
 Paper <mrpapersonic@gmail.com> parents: 
114diff
changeset | 46 std::string ToUtf8String(const QByteArray& ba); | 
| 64 | 47 QString ToQString(const std::string& string); | 
| 48 QString ToQString(const std::wstring& wstring); | |
| 62 | 49 | 
| 211 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 50 /* not really an "int"... but who cares? */ | 
| 258 | 51 template<typename T = int, 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: 
118diff
changeset | 52 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: 
178diff
changeset | 53 std::istringstream s(str); | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 54 s >> std::noboolalpha >> def; | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 55 return def; | 
| 122 
bc218c9d2ea6
strings: convert ToInt() to be a template
 Paper <mrpapersonic@gmail.com> parents: 
118diff
changeset | 56 } | 
| 101 
c537996cf67b
*: multitude of config changes
 Paper <mrpapersonic@gmail.com> parents: 
100diff
changeset | 57 | 
| 258 | 58 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: 
178diff
changeset | 59 std::string ToUtf8String(T i) { | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 60 std::ostringstream s; | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 61 s << i; | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 62 return s.str(); | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 63 } | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 64 | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 65 bool ToBool(const std::string& s, bool def); | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 66 | 
| 
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
 Paper <mrpapersonic@gmail.com> parents: 
178diff
changeset | 67 std::string ToUtf8String(bool b); | 
| 116 
254b1d2b7096
settings: add torrents page, make rss feed configurable
 Paper <mrpapersonic@gmail.com> parents: 
114diff
changeset | 68 | 
| 114 
ab191e28e69d
*: add initial torrent stuff
 Paper <mrpapersonic@gmail.com> parents: 
105diff
changeset | 69 uint64_t HumanReadableSizeToBytes(const std::string& str); | 
| 273 
f31305b9f60a
*: various code safety changes
 Paper <paper@paper.us.eu.org> parents: 
264diff
changeset | 70 std::string BytesToHumanReadableSize(uint64_t bytes, int precision = 2); | 
| 114 
ab191e28e69d
*: add initial torrent stuff
 Paper <mrpapersonic@gmail.com> parents: 
105diff
changeset | 71 | 
| 264 | 72 void RemoveLeadingChars(std::string& s, const char c); | 
| 73 void RemoveTrailingChars(std::string& s, const char c); | |
| 114 
ab191e28e69d
*: add initial torrent stuff
 Paper <mrpapersonic@gmail.com> parents: 
105diff
changeset | 74 | 
| 102 | 75 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub); | 
| 76 | |
| 85 | 77 }; // namespace Strings | 
| 78 | |
| 261 
3ec7804abf17
include: make header guards more sane
 Paper <paper@paper.us.eu.org> parents: 
260diff
changeset | 79 #endif // MINORI_CORE_STRINGS_H_ | 
