Mercurial > minori
comparison src/core/strings.cc @ 211:7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Sun, 07 Jan 2024 09:54:17 -0500 |
parents | 9613d72b097e |
children | 53211cb1e7f5 |
comparison
equal
deleted
inserted
replaced
200:da91af31ae73 | 211:7cf53145de11 |
---|---|
157 | 157 |
158 QString ToQString(const std::wstring& wstring) { | 158 QString ToQString(const std::wstring& wstring) { |
159 return QString::fromWCharArray(wstring.c_str(), wstring.length()); | 159 return QString::fromWCharArray(wstring.c_str(), wstring.length()); |
160 } | 160 } |
161 | 161 |
162 /* not really an "int"... but who cares? */ | 162 std::string ToUtf8String(const bool b) { |
163 int ToInt(const std::string& str, int def) { | 163 return b ? "true" : "false"; // lol |
164 int tmp = 0; | 164 } |
165 try { | 165 |
166 tmp = std::stoi(str); | 166 bool ToBool(const std::string& str, bool def) { |
167 } catch (std::invalid_argument const& ex) { | 167 std::istringstream s(Strings::ToLower(str)); |
168 qDebug() << "Failed to parse int from std::string: no number found in " << ToQString(str) << " defaulting to " << def; | 168 s >> std::boolalpha >> def; |
169 tmp = def; | |
170 } | |
171 return tmp; | |
172 } | |
173 | |
174 bool ToBool(const std::string& s, const bool def) { | |
175 if (s.length() < 4) | |
176 return def; | |
177 const std::string l = Strings::ToLower(s); | |
178 if (Strings::BeginningMatchesSubstring(l, "true")) | |
179 return true; | |
180 else if (Strings::BeginningMatchesSubstring(l, "false")) | |
181 return false; | |
182 return def; | 169 return def; |
183 } | 170 } |
184 | 171 |
185 std::string ToUtf8String(const bool b) { | 172 /* util funcs */ |
186 return b ? "true" : "false"; | |
187 } | |
188 | |
189 uint64_t HumanReadableSizeToBytes(const std::string& str) { | 173 uint64_t HumanReadableSizeToBytes(const std::string& str) { |
190 static const std::unordered_map<std::string, uint64_t> bytes_map = { | 174 static const std::unordered_map<std::string, uint64_t> bytes_map = { |
191 {"KB", 1ull << 10}, | 175 {"KB", 1ull << 10}, |
192 {"MB", 1ull << 20}, | 176 {"MB", 1ull << 20}, |
193 {"GB", 1ull << 30}, | 177 {"GB", 1ull << 30}, |