Skip to content

Commit

Permalink
More std::string conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Nov 13, 2023
1 parent 19eeaef commit 1da6da4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Common/UI/PopupScreens.cpp
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions Common/UI/View.h
Expand Up @@ -927,10 +927,10 @@ class BorderView : public InertView {

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;
Expand Down
29 changes: 28 additions & 1 deletion Core/Config.cpp
Expand Up @@ -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";
Expand Down Expand Up @@ -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)) + ")";
Expand Down
28 changes: 2 additions & 26 deletions Core/ConfigValues.h
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Core/KeyMap.cpp
Expand Up @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions Core/KeyMap.h
Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions Core/Util/PPGeDraw.cpp
Expand Up @@ -1082,24 +1082,24 @@ 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);
}
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;
}

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);
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down

0 comments on commit 1da6da4

Please sign in to comment.