Mercurial > minori
comparison src/core/strings.cc @ 202:71832ffe425a
animia: re-add kvm fd source
this is all being merged from my wildly out-of-date laptop. SORRY!
in other news, I edited the CI file to install the wayland client
as well, so the linux CI build might finally get wayland stuff.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Tue, 02 Jan 2024 06:05:06 -0500 |
parents | 9613d72b097e |
children | 7cf53145de11 |
comparison
equal
deleted
inserted
replaced
201:8f6f8dd2eb23 | 202:71832ffe425a |
---|---|
1 /** | 1 /** |
2 * strings.cpp: Useful functions for manipulating strings | 2 * strings.cpp: Useful functions for manipulating strings |
3 **/ | 3 **/ |
4 #include "core/strings.h" | 4 #include "core/strings.h" |
5 #include "core/session.h" // locale | |
6 | |
5 #include <QByteArray> | 7 #include <QByteArray> |
6 #include <QDebug> | 8 #include <QDebug> |
7 #include <QString> | 9 #include <QString> |
8 #include <QLocale> | 10 #include <QLocale> |
11 | |
9 #include <algorithm> | 12 #include <algorithm> |
10 #include <cctype> | 13 #include <cctype> |
11 #include <codecvt> | 14 #include <codecvt> |
12 #include <locale> | 15 #include <locale> |
13 #include <string> | 16 #include <string> |
18 | 21 |
19 /* ew */ | 22 /* ew */ |
20 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { | 23 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { |
21 if (vector.size() < 1) | 24 if (vector.size() < 1) |
22 return "-"; | 25 return "-"; |
26 | |
23 std::string out = ""; | 27 std::string out = ""; |
28 | |
24 for (unsigned long long i = 0; i < vector.size(); i++) { | 29 for (unsigned long long i = 0; i < vector.size(); i++) { |
25 out.append(vector.at(i)); | 30 out.append(vector.at(i)); |
26 if (i < vector.size() - 1) | 31 if (i < vector.size() - 1) |
27 out.append(delimiter); | 32 out.append(delimiter); |
28 } | 33 } |
34 | |
29 return out; | 35 return out; |
30 } | 36 } |
31 | 37 |
32 std::vector<std::string> Split(const std::string &text, const std::string& delimiter) { | 38 std::vector<std::string> Split(const std::string &text, const std::string& delimiter) { |
33 std::vector<std::string> tokens; | 39 std::vector<std::string> tokens; |
111 } | 117 } |
112 | 118 |
113 /* let Qt handle the heavy lifting of locale shit | 119 /* let Qt handle the heavy lifting of locale shit |
114 I don't want to deal with */ | 120 I don't want to deal with */ |
115 std::string ToUpper(const std::string& string) { | 121 std::string ToUpper(const std::string& string) { |
116 /* todo: this "locale" will have to be moved to session.h | 122 return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string))); |
117 it also defaults to en-US, which sucks very much for | |
118 anyone who doesn't speak american english... */ | |
119 QLocale locale; | |
120 return ToUtf8String(locale.toUpper(ToQString(string))); | |
121 } | 123 } |
122 | 124 |
123 std::string ToLower(const std::string& string) { | 125 std::string ToLower(const std::string& string) { |
124 QLocale locale; | 126 return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string))); |
125 return ToUtf8String(locale.toLower(ToQString(string))); | |
126 } | 127 } |
127 | 128 |
128 std::wstring ToWstring(const std::string& string) { | 129 std::wstring ToWstring(const std::string& string) { |
129 std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; | 130 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; |
130 return converter.from_bytes(string); | 131 return converter.from_bytes(string); |
131 } | 132 } |
132 | 133 |
133 std::wstring ToWstring(const QString& string) { | 134 std::wstring ToWstring(const QString& string) { |
134 std::wstring arr(string.size(), L'\0'); | 135 std::wstring arr(string.size(), L'\0'); |
135 string.toWCharArray(&arr.front()); | 136 string.toWCharArray(&arr.front()); |
136 return arr; | 137 return arr; |
137 } | 138 } |
138 | 139 |
139 std::string ToUtf8String(const std::wstring& wstring) { | 140 std::string ToUtf8String(const std::wstring& wstring) { |
140 std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; | 141 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; |
141 return converter.to_bytes(wstring); | 142 return converter.to_bytes(wstring); |
142 } | 143 } |
143 | 144 |
144 std::string ToUtf8String(const QString& string) { | 145 std::string ToUtf8String(const QString& string) { |
145 QByteArray ba = string.toUtf8(); | 146 const QByteArray ba = string.toUtf8(); |
146 return std::string(ba.constData(), ba.size()); | 147 return std::string(ba.constData(), ba.size()); |
147 } | 148 } |
148 | 149 |
149 std::string ToUtf8String(const QByteArray& ba) { | 150 std::string ToUtf8String(const QByteArray& ba) { |
150 return std::string(ba.constData(), ba.size()); | 151 return std::string(ba.constData(), ba.size()); |
171 } | 172 } |
172 | 173 |
173 bool ToBool(const std::string& s, const bool def) { | 174 bool ToBool(const std::string& s, const bool def) { |
174 if (s.length() < 4) | 175 if (s.length() < 4) |
175 return def; | 176 return def; |
176 std::string l = Strings::ToLower(s); | 177 const std::string l = Strings::ToLower(s); |
177 if (Strings::BeginningMatchesSubstring(l, "true")) | 178 if (Strings::BeginningMatchesSubstring(l, "true")) |
178 return true; | 179 return true; |
179 else if (Strings::BeginningMatchesSubstring(l, "false")) | 180 else if (Strings::BeginningMatchesSubstring(l, "false")) |
180 return false; | 181 return false; |
181 return def; | 182 return def; |
184 std::string ToUtf8String(const bool b) { | 185 std::string ToUtf8String(const bool b) { |
185 return b ? "true" : "false"; | 186 return b ? "true" : "false"; |
186 } | 187 } |
187 | 188 |
188 uint64_t HumanReadableSizeToBytes(const std::string& str) { | 189 uint64_t HumanReadableSizeToBytes(const std::string& str) { |
189 const std::unordered_map<std::string, uint64_t> bytes_map = { | 190 static const std::unordered_map<std::string, uint64_t> bytes_map = { |
190 {"KB", 1ull << 10}, | 191 {"KB", 1ull << 10}, |
191 {"MB", 1ull << 20}, | 192 {"MB", 1ull << 20}, |
192 {"GB", 1ull << 30}, | 193 {"GB", 1ull << 30}, |
193 {"TB", 1ull << 40}, | 194 {"TB", 1ull << 40}, |
194 {"PB", 1ull << 50} /* surely we won't need more than this */ | 195 {"PB", 1ull << 50} /* surely we won't need more than this */ |
220 | 221 |
221 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub) { | 222 bool BeginningMatchesSubstring(const std::string& str, const std::string& sub) { |
222 for (unsigned long long i = 0; i < str.length() && i < sub.length(); i++) | 223 for (unsigned long long i = 0; i < str.length() && i < sub.length(); i++) |
223 if (str[i] != sub[i]) | 224 if (str[i] != sub[i]) |
224 return false; | 225 return false; |
226 | |
225 return true; | 227 return true; |
226 } | 228 } |
227 | 229 |
228 } // namespace Strings | 230 } // namespace Strings |