Mercurial > minori
comparison src/core/strings.cc @ 273:f31305b9f60a
*: various code safety changes
this also makes the code build on Qt 5.7. I can't test it though
because I don't have it working... FAIL!
| author | Paper <paper@paper.us.eu.org> |
|---|---|
| date | Thu, 18 Apr 2024 16:53:17 -0400 |
| parents | 9a04802848c0 |
| children | f6a756c19bfb |
comparison
equal
deleted
inserted
replaced
| 272:5437009cb10e | 273:f31305b9f60a |
|---|---|
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <cctype> | 13 #include <cctype> |
| 14 #include <codecvt> | 14 #include <codecvt> |
| 15 #include <iostream> | 15 #include <iostream> |
| 16 #include <iomanip> | |
| 16 #include <locale> | 17 #include <locale> |
| 17 #include <string> | 18 #include <string> |
| 18 #include <unordered_map> | 19 #include <unordered_map> |
| 19 #include <vector> | 20 #include <vector> |
| 20 | 21 |
| 241 } | 242 } |
| 242 | 243 |
| 243 /* util funcs */ | 244 /* util funcs */ |
| 244 uint64_t HumanReadableSizeToBytes(const std::string& str) { | 245 uint64_t HumanReadableSizeToBytes(const std::string& str) { |
| 245 static const std::unordered_map<std::string, uint64_t> bytes_map = { | 246 static const std::unordered_map<std::string, uint64_t> bytes_map = { |
| 246 {"KB", 1ull << 10}, | 247 {"KB", 1000ull}, |
| 247 {"MB", 1ull << 20}, | 248 {"MB", 1000000ull}, |
| 248 {"GB", 1ull << 30}, | 249 {"GB", 1000000000ull}, |
| 249 {"TB", 1ull << 40}, | 250 {"TB", 1000000000000ull}, |
| 250 {"PB", 1ull << 50} /* surely we won't need more than this */ | 251 {"PB", 1000000000000000ull}, |
| 252 {"KiB", 1ull << 10}, | |
| 253 {"MiB", 1ull << 20}, | |
| 254 {"GiB", 1ull << 30}, | |
| 255 {"TiB", 1ull << 40}, | |
| 256 {"PiB", 1ull << 50} /* surely we won't need more than this */ | |
| 251 }; | 257 }; |
| 252 | 258 |
| 253 for (const auto& suffix : bytes_map) { | 259 for (const auto& suffix : bytes_map) { |
| 254 if (str.find(suffix.first) != std::string::npos) { | 260 if (str.find(suffix.first) != std::string::npos) { |
| 255 try { | 261 try { |
| 262 } | 268 } |
| 263 | 269 |
| 264 return ToInt(str, 0); | 270 return ToInt(str, 0); |
| 265 } | 271 } |
| 266 | 272 |
| 273 std::string BytesToHumanReadableSize(uint64_t bytes, int precision) { | |
| 274 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) | |
| 275 /* QLocale in Qt >= 5.10.0 has a function for this */ | |
| 276 return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes), precision); | |
| 277 #else | |
| 278 static const std::unordered_map<uint64_t, std::string> map = { | |
| 279 {1ull << 10, "KiB"}, | |
| 280 {1ull << 20, "MiB"}, | |
| 281 {1ull << 30, "GiB"}, | |
| 282 {1ull << 40, "TiB"}, | |
| 283 {1ull << 50, "PiB"} | |
| 284 }; | |
| 285 | |
| 286 for (const auto& suffix : map) { | |
| 287 if (bytes / suffix.first < 1) | |
| 288 continue; | |
| 289 | |
| 290 std::stringstream ss; | |
| 291 ss << std::setprecision(precision) | |
| 292 << (static_cast<double>(bytes) / suffix.first) << " " | |
| 293 << suffix.second; | |
| 294 return ss.str(); | |
| 295 } | |
| 296 | |
| 297 /* better luck next time */ | |
| 298 return "0 bytes"; | |
| 299 #endif | |
| 300 } | |
| 301 | |
| 267 void RemoveLeadingChars(std::string& s, const char c) { | 302 void RemoveLeadingChars(std::string& s, const char c) { |
| 268 s.erase(0, std::min(s.find_first_not_of(c), s.size() - 1)); | 303 s.erase(0, std::min(s.find_first_not_of(c), s.size() - 1)); |
| 269 } | 304 } |
| 270 | 305 |
| 271 void RemoveTrailingChars(std::string& s, const char c) { | 306 void RemoveTrailingChars(std::string& s, const char c) { |
