Mercurial > minori
diff src/core/strings.cc @ 187:9613d72b097e
*: multiple performance improvements
like marking `static const` when it makes sense...
date: change old stupid heap-based method to a structure which should
make copying the thing actually make a copy.
also many performance-based changes, like removing the std::tie
dependency and forward-declaring nlohmann json
*: replace every instance of QString::fromUtf8 to Strings::ToQString.
the main difference is that our function will always convert exactly
what is in the string, while some other times it would only convert
up to the nearest NUL byte
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Wed, 06 Dec 2023 13:43:54 -0500 |
parents | 275da698697d |
children | 7cf53145de11 |
line wrap: on
line diff
--- a/src/core/strings.cc Wed Dec 06 11:47:59 2023 -0500 +++ b/src/core/strings.cc Wed Dec 06 13:43:54 2023 -0500 @@ -2,10 +2,13 @@ * strings.cpp: Useful functions for manipulating strings **/ #include "core/strings.h" +#include "core/session.h" // locale + #include <QByteArray> #include <QDebug> #include <QString> #include <QLocale> + #include <algorithm> #include <cctype> #include <codecvt> @@ -20,12 +23,15 @@ std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { if (vector.size() < 1) return "-"; + std::string out = ""; + for (unsigned long long i = 0; i < vector.size(); i++) { out.append(vector.at(i)); if (i < vector.size() - 1) out.append(delimiter); } + return out; } @@ -113,20 +119,15 @@ /* let Qt handle the heavy lifting of locale shit I don't want to deal with */ std::string ToUpper(const std::string& string) { - /* todo: this "locale" will have to be moved to session.h - it also defaults to en-US, which sucks very much for - anyone who doesn't speak american english... */ - QLocale locale; - return ToUtf8String(locale.toUpper(ToQString(string))); + return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string))); } std::string ToLower(const std::string& string) { - QLocale locale; - return ToUtf8String(locale.toLower(ToQString(string))); + return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string))); } std::wstring ToWstring(const std::string& string) { - std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; + static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; return converter.from_bytes(string); } @@ -137,12 +138,12 @@ } std::string ToUtf8String(const std::wstring& wstring) { - std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; + static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; return converter.to_bytes(wstring); } std::string ToUtf8String(const QString& string) { - QByteArray ba = string.toUtf8(); + const QByteArray ba = string.toUtf8(); return std::string(ba.constData(), ba.size()); } @@ -173,7 +174,7 @@ bool ToBool(const std::string& s, const bool def) { if (s.length() < 4) return def; - std::string l = Strings::ToLower(s); + const std::string l = Strings::ToLower(s); if (Strings::BeginningMatchesSubstring(l, "true")) return true; else if (Strings::BeginningMatchesSubstring(l, "false")) @@ -186,7 +187,7 @@ } uint64_t HumanReadableSizeToBytes(const std::string& str) { - const std::unordered_map<std::string, uint64_t> bytes_map = { + static const std::unordered_map<std::string, uint64_t> bytes_map = { {"KB", 1ull << 10}, {"MB", 1ull << 20}, {"GB", 1ull << 30}, @@ -222,6 +223,7 @@ for (unsigned long long i = 0; i < str.length() && i < sub.length(); i++) if (str[i] != sub[i]) return false; + return true; }