annotate src/core/strings.cc @ 413:192da585a0a8 default tip

*: fix formatting the clang-format file was broken andjust using LLVM defaults. now it's fixed and is more in line with what I actually prefer my code to look like -- esp. with regards to tabulators
author Paper <paper@tflc.us>
date Sat, 25 Jul 2026 14:54:32 -0400
parents 02a670a8e1c4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 /**
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 * strings.cpp: Useful functions for manipulating strings
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 **/
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
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
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
7 #include <QByteArray>
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
8 #include <QCoreApplication>
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 100
diff changeset
9 #include <QDebug>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
10 #include <QLocale>
64
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
11 #include <QString>
274
f6a756c19bfb anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
12 #include <QTextDocument>
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
13
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
14 #include <algorithm>
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
15 #include <cctype>
62
4c6dd5999b39 *: update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
16 #include <codecvt>
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
17 #include <iomanip>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
18 #include <iostream>
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 #include <locale>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 #include <string>
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
21 #include <unordered_map>
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 #include <vector>
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
24 #include "utf8proc.h"
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
25
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 namespace Strings {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27
99
503bc1547d49 strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents: 98
diff changeset
28 /* ew */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
29 std::string Implode(const std::vector<std::string> &vector, const std::string &delimiter)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
30 {
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 if (vector.size() < 1)
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 231
diff changeset
32 return "";
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
33
221
53211cb1e7f5 library: add initial library stuff
Paper <paper@paper.us.eu.org>
parents: 211
diff changeset
34 std::string out;
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
35
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 for (unsigned long long i = 0; i < vector.size(); i++) {
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 out.append(vector.at(i));
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 if (i < vector.size() - 1)
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 out.append(delimiter);
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 }
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
41
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 return out;
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
45 std::vector<std::string> Split(const std::string &text, const std::string &delimiter)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
46 {
250
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 231
diff changeset
47 if (text.length() < 1)
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 231
diff changeset
48 return {};
c130f47f6f48 *: many many changes
Paper <paper@paper.us.eu.org>
parents: 231
diff changeset
49
118
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
50 std::vector<std::string> tokens;
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
51
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
52 std::size_t start = 0, end = 0;
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
53 while ((end = text.find(delimiter, start)) != std::string::npos) {
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
54 tokens.push_back(text.substr(start, end - start));
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
55 start = end + delimiter.length();
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
56 }
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
57 tokens.push_back(text.substr(start));
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
58
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
59 return tokens;
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
60 }
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
61
99
503bc1547d49 strings: clarify on some functions and make some of them miniscule
Paper <mrpapersonic@gmail.com>
parents: 98
diff changeset
62 /* 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
63 * horrible HTML debris from AniList :)
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
64 */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
65 void ReplaceAll(std::string &string, std::string_view find, std::string_view replace)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
66 {
98
582b2fca1561 strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
67 size_t pos = 0;
582b2fca1561 strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
68 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
69 string.replace(pos, find.length(), replace);
582b2fca1561 strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
70 pos += replace.length();
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
71 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
72 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
74 void ConvertRomanNumerals(std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
75 {
260
dd211ff68b36 pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
76 static const std::vector<std::pair<std::string_view, std::string_view>> vec = {
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
77 {"2", "II" },
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
78 {"3", "III" },
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
79 {"4", "IV" },
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
80 {"5", "V" },
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
81 {"6", "VI" },
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
82 {"7", "VII" },
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
83 {"8", "VIII"},
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
84 {"9", "IX" },
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
85 {"11", "XI" },
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
86 {"12", "XII" },
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
87 {"13", "XIII"}
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
88 };
260
dd211ff68b36 pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
89
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
90 for (const auto &item : vec)
260
dd211ff68b36 pages/seasons: add initial functionality
Paper <paper@paper.us.eu.org>
parents: 258
diff changeset
91 ReplaceAll(string, item.second, item.first);
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92 }
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
94 /* this also performs case folding, so our string is lowercase after this */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
95 void NormalizeUnicode(std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
96 {
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
97 static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>(
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
98 UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK |
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
99 UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS);
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
100
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
101 /* ack */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
102 utf8proc_uint8_t *buf = nullptr;
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
103
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
104 const utf8proc_ssize_t size =
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
105 utf8proc_map(reinterpret_cast<const utf8proc_uint8_t *>(string.data()), string.size(), &buf, options);
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
106
365
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 347
diff changeset
107 if (buf) {
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 347
diff changeset
108 if (size)
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
109 string.assign(reinterpret_cast<const char *>(buf), size);
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
110
365
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 347
diff changeset
111 std::free(buf);
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 347
diff changeset
112 }
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
113 }
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
114
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
115 void NormalizeAnimeTitle(std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
116 {
264
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
117 ConvertRomanNumerals(string);
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
118 NormalizeUnicode(string);
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
119 RemoveLeadingChars(string, ' ');
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
120 RemoveTrailingChars(string, ' ');
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
121 }
9a04802848c0 *: improve multiple things
Paper <paper@paper.us.eu.org>
parents: 260
diff changeset
122
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
123 void TextifySynopsis(std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
124 {
274
f6a756c19bfb anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
125 /* Just let Qt deal with it. */
f6a756c19bfb anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
126 QTextDocument text;
f6a756c19bfb anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
127 text.setHtml(Strings::ToQString(string));
f6a756c19bfb anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
128 string = Strings::ToUtf8String(text.toPlainText());
98
582b2fca1561 strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
129 }
582b2fca1561 strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
130
582b2fca1561 strings: parse HTML entities when reading synopsis, make the
Paper <mrpapersonic@gmail.com>
parents: 81
diff changeset
131 /* 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
132 * I don't want to deal with
258
862d0d8619f6 *: HUUUGE changes
Paper <paper@paper.us.eu.org>
parents: 250
diff changeset
133 */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
134 std::string ToUpper(const std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
135 {
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
136 return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string)));
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
137 }
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
138
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
139 std::string ToLower(const std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
140 {
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
141 return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string)));
15
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
142 }
cde8f67a7c7d *: update, megacommit :)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
143
411
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
144 #ifdef __GNUC__
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
145 # pragma GCC diagnostic push
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
146 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
147 #endif
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
148 std::wstring ToWstring(const std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
149 {
230
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
150 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
151
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
152 std::wstring wstr;
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
153 try {
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
154 wstr = converter.from_bytes(string);
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
155 } catch (std::range_error const &ex) {
347
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
156 /* XXX how? */
230
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
157 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
158 }
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
159 return wstr;
62
4c6dd5999b39 *: update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
160 }
4c6dd5999b39 *: update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
161
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
162 std::string ToUtf8String(const std::wstring &wstring)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
163 {
230
2f5a9247e501 torrents: implement download button
Paper <paper@paper.us.eu.org>
parents: 221
diff changeset
164 static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L"");
62
4c6dd5999b39 *: update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
165 return converter.to_bytes(wstring);
4c6dd5999b39 *: update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
166 }
4c6dd5999b39 *: update
Paper <mrpapersonic@gmail.com>
parents: 15
diff changeset
167
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
168 std::string ToUtf8String(const std::u32string &u32string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
169 {
347
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
170 static std::wstring_convert<std::codecvt_utf8_utf16<char32_t>, char32_t> converter;
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
171 return converter.to_bytes(u32string);
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
172 }
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
173
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
174 std::u32string ToUcs4String(const std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
175 {
347
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
176 static std::wstring_convert<std::codecvt_utf8_utf16<char32_t>, char32_t> converter;
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
177 return converter.from_bytes(string);
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
178 }
411
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
179 #ifdef __GNUC__
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
180 # pragma GCC diagnostic pop
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
181 #endif
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
182
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
183 std::wstring ToWstring(const QString &string)
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
184 {
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
185 std::wstring arr(string.size(), L'\0');
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
186 string.toWCharArray(&arr.front());
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
187 return arr;
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
188 }
347
a0aa8c8c4307 dep/anitomy: port to use UCS-4 rather than wide strings
Paper <paper@paper.us.eu.org>
parents: 322
diff changeset
189
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
190 std::string ToUtf8String(const QString &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
191 {
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
192 const QByteArray ba = string.toUtf8();
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 76
diff changeset
193 return std::string(ba.constData(), ba.size());
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 76
diff changeset
194 }
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 76
diff changeset
195
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
196 std::string ToUtf8String(const QByteArray &ba)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
197 {
77
6f7385bd334c *: update
Paper <mrpapersonic@gmail.com>
parents: 76
diff changeset
198 return std::string(ba.constData(), ba.size());
64
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
199 }
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
200
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
201 QString ToQString(const std::string &string)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
202 {
64
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
203 return QString::fromUtf8(string.c_str(), string.length());
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
204 }
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
205
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
206 QString ToQString(const std::wstring &wstring)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
207 {
64
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
208 return QString::fromWCharArray(wstring.c_str(), wstring.length());
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
209 }
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 62
diff changeset
210
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
211 std::string ToUtf8String(const bool b)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
212 {
211
7cf53145de11 strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
213 return b ? "true" : "false"; // lol
101
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 100
diff changeset
214 }
c537996cf67b *: multitude of config changes
Paper <mrpapersonic@gmail.com>
parents: 100
diff changeset
215
405
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
216 #if defined(__APPLE__) && defined(__MACH__)
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
217 CFStringRef ToCFString(const std::string &string)
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
218 {
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
219 return CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(string.data()), string.size(),
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
220 kCFStringEncodingUTF8, false);
405
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
221 }
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
222 std::string ToUtf8String(CFStringRef str)
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
223 {
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
224 if (const char *ptr = CFStringGetCStringPtr(str, kCFStringEncodingUTF8))
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
225 return std::string(ptr); // easy!
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
226
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
227 // ...
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
228 const CFIndex len = CFStringGetLength(str);
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
229 std::string buf(CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8), 0);
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
230 CFRange range;
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
231 range.length = len;
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
232 range.location = 0;
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
233 CFIndex used;
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
234 CFStringGetBytes(
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
235 str, range, kCFStringEncodingUTF8, 0, false, reinterpret_cast<UInt8 *>(buf.data()), buf.size(), &used);
405
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
236 buf.resize(used);
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
237 return buf;
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
238 }
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
239 #endif
4562bc8bfdff strings: add conversion to/from CFString on Mac OS X
Paper <paper@tflc.us>
parents: 403
diff changeset
240
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
241 bool ToBool(const std::string &str, bool def)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
242 {
411
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
243 std::string l = Strings::ToLower(str);
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
244
02a670a8e1c4 *: fix build fails
Paper <paper@tflc.us>
parents: 405
diff changeset
245 return (l == "true") ? true : (l == "false") ? false : def;
116
254b1d2b7096 settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents: 114
diff changeset
246 }
254b1d2b7096 settings: add torrents page, make rss feed configurable
Paper <mrpapersonic@gmail.com>
parents: 114
diff changeset
247
365
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 347
diff changeset
248 template<typename T>
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
249 constexpr T ipow(T num, unsigned int pow)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
250 {
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
251 return (pow >= sizeof(unsigned int) * 8) ? 0 : pow == 0 ? 1 : num * ipow(num, pow - 1);
365
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 347
diff changeset
252 }
f81bed4e04ac *: megacommit that probably breaks things
Paper <paper@paper.us.eu.org>
parents: 347
diff changeset
253
211
7cf53145de11 strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents: 187
diff changeset
254 /* util funcs */
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
255 uint64_t HumanReadableSizeToBytes(const std::string &str)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
256 {
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
257 static const std::unordered_map<std::string, uint64_t> bytes_map = {
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
258 {"KB", 1e3 },
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
259 {"MB", 1e6 },
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
260 {"GB", 1e9 },
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
261 {"TB", 1e12 },
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
262 {"PB", 1e15 },
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
263 {"KiB", 1ull << 10},
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
264 {"MiB", 1ull << 20},
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
265 {"GiB", 1ull << 30},
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
266 {"TiB", 1ull << 40},
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
267 {"PiB", 1ull << 50} /* surely we won't need more than this */
114
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
268 };
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
269
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
270 for (const auto &suffix : bytes_map) {
114
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
271 if (str.find(suffix.first) != std::string::npos) {
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
272 try {
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
273 uint64_t size = std::stod(str) * suffix.second;
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
274 return size;
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
275 } catch (std::invalid_argument const &ex) {
114
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
276 continue;
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
277 }
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
278 }
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
279 }
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
280
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
281 return ToInt(str, 0);
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
282 }
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
283
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
284 std::string BytesToHumanReadableSize(uint64_t bytes, int precision)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
285 {
273
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
286 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
287 /* QLocale in Qt >= 5.10.0 has a function for this */
274
f6a756c19bfb anime_list.cc: use mutexes so we don't sex the stack
Paper <paper@paper.us.eu.org>
parents: 273
diff changeset
288 return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision));
273
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
289 #else
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
290 static const std::unordered_map<uint64_t, std::string> map = {
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
291 {1ull << 10, "KiB"},
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
292 {1ull << 20, "MiB"},
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
293 {1ull << 30, "GiB"},
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
294 {1ull << 40, "TiB"},
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
295 {1ull << 50, "PiB"}
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
296 };
273
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
297
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
298 for (const auto &suffix : map) {
273
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
299 if (bytes / suffix.first < 1)
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
300 continue;
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
301
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
302 std::stringstream ss;
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
303 ss << std::setprecision(precision) << (static_cast<double>(bytes) / suffix.first) << " " << suffix.second;
273
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
304 return ss.str();
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
305 }
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
306
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
307 /* better luck next time */
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
308 return "0 bytes";
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
309 #endif
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
310 }
f31305b9f60a *: various code safety changes
Paper <paper@paper.us.eu.org>
parents: 264
diff changeset
311
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
312 void RemoveLeadingChars(std::string &s, const char c)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
313 {
118
39521c47c7a3 *: another huge megacommit, SORRY
Paper <mrpapersonic@gmail.com>
parents: 116
diff changeset
314 s.erase(0, std::min(s.find_first_not_of(c), s.size() - 1));
114
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
315 }
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
316
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
317 void RemoveTrailingChars(std::string &s, const char c)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
318 {
114
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
319 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
320 }
ab191e28e69d *: add initial torrent stuff
Paper <mrpapersonic@gmail.com>
parents: 102
diff changeset
321
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
322 bool BeginningMatchesSubstring(const std::string &str, const std::string &sub)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
323 {
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 101
diff changeset
324 for (unsigned long long i = 0; i < str.length() && i < sub.length(); i++)
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 101
diff changeset
325 if (str[i] != sub[i])
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 101
diff changeset
326 return false;
187
9613d72b097e *: multiple performance improvements
Paper <mrpapersonic@gmail.com>
parents: 120
diff changeset
327
102
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 101
diff changeset
328 return true;
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 101
diff changeset
329 }
b315f3759c56 *: big patch
Paper <mrpapersonic@gmail.com>
parents: 101
diff changeset
330
369
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
331 std::string Translate(const char *str)
47c9f8502269 *: clang-format all the things
Paper <paper@tflc.us>
parents: 365
diff changeset
332 {
322
c32467cd06bb core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents: 320
diff changeset
333 return Strings::ToUtf8String(QCoreApplication::tr(str));
c32467cd06bb core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents: 320
diff changeset
334 }
c32467cd06bb core/strings: add Strings::Translate function as tr() -> ToUtf8String
Paper <paper@paper.us.eu.org>
parents: 320
diff changeset
335
403
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
336 /* Moved here from glib code because xsettings parser needs it */
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
337 bool IsGTKThemeDark(const std::string_view str)
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
338 {
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
339 /* if that doesn't exist, use the GTK theme and check for some known
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
340 * suffixes. if one is found, return
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
341 *
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
342 * XXX probably better to use case folding here */
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
343 static constexpr std::array<std::string_view, 3> suffixes = {
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
344 "-dark", /* Adwaita-dark */
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
345 "-Dark", /* Arc-Dark */
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
346 "-Darker", /* Arc-Darker */
403
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
347 };
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
348
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
349 for (const auto &suffix : suffixes) {
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
350 if (str.size() < suffix.size())
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
351 continue;
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
352
413
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
353 if (std::equal(
192da585a0a8 *: fix formatting
Paper <paper@tflc.us>
parents: 411
diff changeset
354 str.data() + str.size() - suffix.length(), str.data() + str.size(), suffix.begin(), suffix.end()))
403
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
355 return true;
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
356 }
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
357
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
358 return false;
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
359 }
df4a027623d0 x11: fix build fail when glib is not detected
Paper <paper@tflc.us>
parents: 369
diff changeset
360
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
361 } // namespace Strings