save_data: Fix save data list back button. (#1003)
Some checks are pending
Reuse / reuse (push) Waiting to run
Clang Format / clang-format (push) Waiting to run
Linux-Qt / build (push) Waiting to run
Linux / build (push) Waiting to run
macOS-Qt / build (push) Waiting to run
macOS / build (push) Waiting to run
Windows-Qt / build (push) Waiting to run
Windows / build (push) Waiting to run

* save_data: Fix save data list back button.

* common: Add more null checks in CString.

Co-authored-by: Vinicius Rangel <me@viniciusrangel.dev>

---------

Co-authored-by: Vinicius Rangel <me@viniciusrangel.dev>
This commit is contained in:
squidbus 2024-09-21 00:21:53 -07:00 committed by GitHub
parent 4fe81231f6
commit 171e475f80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 4 deletions

View file

@ -9,6 +9,9 @@
namespace Common { namespace Common {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-undefined-compare"
/** /**
* @brief A null-terminated string with a fixed maximum length * @brief A null-terminated string with a fixed maximum length
* This class is not meant to be used as a general-purpose string class * This class is not meant to be used as a general-purpose string class
@ -29,20 +32,27 @@ public:
explicit CString(const CString<M>& other) explicit CString(const CString<M>& other)
requires(M <= N) requires(M <= N)
{ {
if (this == nullptr) {
return;
}
std::ranges::copy(other.begin(), other.end(), data); std::ranges::copy(other.begin(), other.end(), data);
} }
void FromString(const std::basic_string_view<T>& str) { void FromString(const std::basic_string_view<T>& str) {
if (this == nullptr) {
return;
}
size_t p = str.copy(data, N - 1); size_t p = str.copy(data, N - 1);
data[p] = '\0'; data[p] = '\0';
} }
void Zero() { void Zero() {
if (this == nullptr) {
return;
}
std::ranges::fill(data, 0); std::ranges::fill(data, 0);
} }
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-undefined-compare"
explicit(false) operator std::basic_string_view<T>() const { explicit(false) operator std::basic_string_view<T>() const {
if (this == nullptr) { if (this == nullptr) {
return {}; return {};
@ -70,21 +80,32 @@ public:
} }
return std::basic_string_view<T>{data}; return std::basic_string_view<T>{data};
} }
#pragma clang diagnostic pop
char* begin() { char* begin() {
if (this == nullptr) {
return nullptr;
}
return data; return data;
} }
const char* begin() const { const char* begin() const {
if (this == nullptr) {
return nullptr;
}
return data; return data;
} }
char* end() { char* end() {
if (this == nullptr) {
return nullptr;
}
return data + N; return data + N;
} }
const char* end() const { const char* end() const {
if (this == nullptr) {
return nullptr;
}
return data + N; return data + N;
} }
@ -127,7 +148,10 @@ public:
} }
}; };
}; };
static_assert(sizeof(CString<13>) == sizeof(char[13])); // Ensure size still matches a simple array static_assert(sizeof(CString<13>) == sizeof(char[13])); // Ensure size still matches a simple array
static_assert(std::weakly_incrementable<CString<13>::Iterator>); static_assert(std::weakly_incrementable<CString<13>::Iterator>);
#pragma clang diagnostic pop
} // namespace Common } // namespace Common

View file

@ -620,7 +620,7 @@ void SaveDialogUi::DrawList() {
SetCursorPosX(GetContentRegionAvail().x - button_size.x); SetCursorPosX(GetContentRegionAvail().x - button_size.x);
if (Button(back, button_size)) { if (Button(back, button_size)) {
result->dir_name.clear(); result->dir_name.clear();
Finish(ButtonId::INVALID); Finish(ButtonId::INVALID, Result::USER_CANCELED);
} }
if (IsKeyPressed(ImGuiKey_GamepadFaceRight)) { if (IsKeyPressed(ImGuiKey_GamepadFaceRight)) {
SetItemCurrentNavFocus(); SetItemCurrentNavFocus();