More std::string conversion

This commit is contained in:
Henrik Rydgård 2023-11-13 23:43:57 +01:00
parent 19eeaef2ea
commit 1da6da446b
7 changed files with 45 additions and 41 deletions

View File

@ -17,9 +17,9 @@ void MessagePopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
using namespace UI;
UIContext &dc = *screenManager()->getUIContext();
std::vector<std::string> messageLines;
std::vector<std::string_view> 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);
}

View File

@ -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;

View File

@ -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 <typename T, std::string (*FTo)(T), T (*FFrom)(const std::string &)>
template <typename T, std::string (*FTo)(T), T (*FFrom)(std::string_view)>
struct ConfigTranslator {
static std::string To(int v) {
return StringFromInt(v) + " (" + FTo(T(v)) + ")";

View File

@ -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,

View File

@ -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<std::string> parts;
std::vector<std::string_view> parts;
SplitString(str, ':', parts);
for (auto iter : parts) {
out.mappings.push_back(InputMapping::FromConfigString(iter));

View File

@ -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) {

View File

@ -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<std::string> lines;
static std::string_view CropLinesToCount(std::string_view s, int numLines) {
std::vector<std::string_view> 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.