From 1da6da446bdfd748b52abb9594c24f084ae4d64c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 13 Nov 2023 23:43:57 +0100 Subject: [PATCH] More std::string conversion --- Common/UI/PopupScreens.cpp | 4 ++-- Common/UI/View.h | 4 ++-- Core/Config.cpp | 29 ++++++++++++++++++++++++++++- Core/ConfigValues.h | 28 ++-------------------------- Core/KeyMap.cpp | 4 ++-- Core/KeyMap.h | 3 +-- Core/Util/PPGeDraw.cpp | 14 ++++++++------ 7 files changed, 45 insertions(+), 41 deletions(-) diff --git a/Common/UI/PopupScreens.cpp b/Common/UI/PopupScreens.cpp index 60c343dada..f3dacb9971 100644 --- a/Common/UI/PopupScreens.cpp +++ b/Common/UI/PopupScreens.cpp @@ -17,9 +17,9 @@ void MessagePopupScreen::CreatePopupContents(UI::ViewGroup *parent) { using namespace UI; UIContext &dc = *screenManager()->getUIContext(); - std::vector messageLines; + std::vector messageLines; SplitString(message_, '\n', messageLines); - for (const auto &lineOfText : messageLines) + for (auto lineOfText : messageLines) parent->Add(new UI::TextView(lineOfText, ALIGN_LEFT | ALIGN_VCENTER, false))->SetTextColor(dc.theme->popupStyle.fgColor); } diff --git a/Common/UI/View.h b/Common/UI/View.h index 0e0c5f7dcc..190da0b115 100644 --- a/Common/UI/View.h +++ b/Common/UI/View.h @@ -927,10 +927,10 @@ private: class TextView : public InertView { public: - TextView(const std::string &text, LayoutParams *layoutParams = 0) + TextView(std::string_view text, LayoutParams *layoutParams = 0) : InertView(layoutParams), text_(text), textAlign_(0), textColor_(0xFFFFFFFF), small_(false), shadow_(false), focusable_(false), clip_(true) {} - TextView(const std::string &text, int textAlign, bool small, LayoutParams *layoutParams = 0) + TextView(std::string_view text, int textAlign, bool small, LayoutParams *layoutParams = 0) : InertView(layoutParams), text_(text), textAlign_(textAlign), textColor_(0xFFFFFFFF), small_(small), shadow_(false), focusable_(false), clip_(true) {} void GetContentDimensionsBySpec(const UIContext &dc, MeasureSpec horiz, MeasureSpec vert, float &w, float &h) const override; diff --git a/Core/Config.cpp b/Core/Config.cpp index 769beec74d..82fe04dc37 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -81,6 +81,33 @@ static const char *logSectionName = "LogDebug"; static const char *logSectionName = "Log"; #endif +std::string GPUBackendToString(GPUBackend backend) { + switch (backend) { + case GPUBackend::OPENGL: + return "OPENGL"; + case GPUBackend::DIRECT3D9: + return "DIRECT3D9"; + case GPUBackend::DIRECT3D11: + return "DIRECT3D11"; + case GPUBackend::VULKAN: + return "VULKAN"; + } + // Intentionally not a default so we get a warning. + return "INVALID"; +} + +GPUBackend GPUBackendFromString(std::string_view backend) { + if (!equalsNoCase(backend, "OPENGL") || backend == "0") + return GPUBackend::OPENGL; + if (!equalsNoCase(backend, "DIRECT3D9") || backend == "1") + return GPUBackend::DIRECT3D9; + if (!equalsNoCase(backend, "DIRECT3D11") || backend == "2") + return GPUBackend::DIRECT3D11; + if (!equalsNoCase(backend, "VULKAN") || backend == "3") + return GPUBackend::VULKAN; + return GPUBackend::OPENGL; +} + const char *DefaultLangRegion() { // Unfortunate default. There's no need to use bFirstRun, since this is only a default. static std::string defaultLangRegion = "en_US"; @@ -513,7 +540,7 @@ bool Config::IsBackendEnabled(GPUBackend backend, bool validate) { return true; } -template +template struct ConfigTranslator { static std::string To(int v) { return StringFromInt(v) + " (" + FTo(T(v)) + ")"; diff --git a/Core/ConfigValues.h b/Core/ConfigValues.h index 7d02b5c960..09837e0786 100644 --- a/Core/ConfigValues.h +++ b/Core/ConfigValues.h @@ -90,32 +90,8 @@ enum class RestoreSettingsBits : int { }; ENUM_CLASS_BITOPS(RestoreSettingsBits); -inline std::string GPUBackendToString(GPUBackend backend) { - switch (backend) { - case GPUBackend::OPENGL: - return "OPENGL"; - case GPUBackend::DIRECT3D9: - return "DIRECT3D9"; - case GPUBackend::DIRECT3D11: - return "DIRECT3D11"; - case GPUBackend::VULKAN: - return "VULKAN"; - } - // Intentionally not a default so we get a warning. - return "INVALID"; -} - -inline GPUBackend GPUBackendFromString(const std::string &backend) { - if (!strcasecmp(backend.c_str(), "OPENGL") || backend == "0") - return GPUBackend::OPENGL; - if (!strcasecmp(backend.c_str(), "DIRECT3D9") || backend == "1") - return GPUBackend::DIRECT3D9; - if (!strcasecmp(backend.c_str(), "DIRECT3D11") || backend == "2") - return GPUBackend::DIRECT3D11; - if (!strcasecmp(backend.c_str(), "VULKAN") || backend == "3") - return GPUBackend::VULKAN; - return GPUBackend::OPENGL; -} +std::string GPUBackendToString(GPUBackend backend); +GPUBackend GPUBackendFromString(std::string_view backend); enum AudioBackendType { AUDIO_BACKEND_AUTO, diff --git a/Core/KeyMap.cpp b/Core/KeyMap.cpp index 078f4dc4a8..7d46dfb515 100644 --- a/Core/KeyMap.cpp +++ b/Core/KeyMap.cpp @@ -906,9 +906,9 @@ const char *GetVirtKeyName(int vkey) { return g_vKeyNames[index]; } -MultiInputMapping MultiInputMapping::FromConfigString(const std::string &str) { +MultiInputMapping MultiInputMapping::FromConfigString(std::string_view str) { MultiInputMapping out; - std::vector parts; + std::vector parts; SplitString(str, ':', parts); for (auto iter : parts) { out.mappings.push_back(InputMapping::FromConfigString(iter)); diff --git a/Core/KeyMap.h b/Core/KeyMap.h index 2689cf4a02..3459433b2d 100644 --- a/Core/KeyMap.h +++ b/Core/KeyMap.h @@ -114,9 +114,8 @@ namespace KeyMap { mappings.push_back(mapping); } - static MultiInputMapping FromConfigString(const std::string &str); + static MultiInputMapping FromConfigString(std::string_view str); std::string ToConfigString() const; - std::string ToVisualString() const; bool operator <(const MultiInputMapping &other) { diff --git a/Core/Util/PPGeDraw.cpp b/Core/Util/PPGeDraw.cpp index 0505517176..8a4f3f90f0 100644 --- a/Core/Util/PPGeDraw.cpp +++ b/Core/Util/PPGeDraw.cpp @@ -1082,7 +1082,7 @@ void PPGeDrawText(const char *text, float x, float y, const PPGeStyle &style) { PPGeDrawCurrentText(style.color); } -static std::string StripTrailingWhite(const std::string &s) { +static std::string_view StripTrailingWhite(std::string_view s) { size_t lastChar = s.find_last_not_of(" \t\r\n"); if (lastChar != s.npos) { return s.substr(0, lastChar + 1); @@ -1090,8 +1090,8 @@ static std::string StripTrailingWhite(const std::string &s) { return s; } -static std::string CropLinesToCount(const std::string &s, int numLines) { - std::vector lines; +static std::string_view CropLinesToCount(std::string_view s, int numLines) { + std::vector lines; SplitString(s, '\n', lines); if ((int)lines.size() <= numLines) { return s; @@ -1099,7 +1099,7 @@ static std::string CropLinesToCount(const std::string &s, int numLines) { size_t len = 0; for (int i = 0; i < numLines; ++i) { - len += lines[i].length() + 1; + len += lines[i].size() + 1; } return s.substr(0, len); @@ -1138,7 +1138,8 @@ void PPGeDrawTextWrapped(const char *text, float x, float y, float wrapWidth, fl actualHeight = (maxLines + 1) * lineHeight; // Add an ellipsis if it's just too long to be readable. // On a PSP, it does this without scaling it down. - s2 = StripTrailingWhite(CropLinesToCount(s2, (int)maxLines)) + "\n..."; + s2 = StripTrailingWhite(CropLinesToCount(s2, (int)maxLines)); + s2.append("\n..."); } adjustedStyle.scale *= wrapHeight / actualHeight; @@ -1164,7 +1165,8 @@ void PPGeDrawTextWrapped(const char *text, float x, float y, float wrapWidth, fl actualHeight = (maxLines + 1) * char_lines_metrics.lineHeight; // Add an ellipsis if it's just too long to be readable. // On a PSP, it does this without scaling it down. - s = StripTrailingWhite(CropLinesToCount(s, (int)maxLines)) + "\n..."; + s = StripTrailingWhite(CropLinesToCount(s, (int)maxLines)); + s.append("\n..."); } // Measure the text again after scaling down.