diff options
| author | Léo Lam <leo@leolam.fr> | 2021-09-20 22:17:20 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-20 22:17:20 +0200 |
| commit | 22f7c07caf2dd0906a75433f5b2e52dc4359dad4 (patch) | |
| tree | a2b408edad73c99ed8ec6ef7eb42ee2b43a3d001 /Source/Core | |
| parent | 7ec02ee4d37cef6a73ac7a678901904ca6855728 (diff) | |
| parent | 94cba464675f748ae3df1296549aa5c486143fa5 (diff) | |
Merge pull request #10081 from sepalani/mem-hex0
MemoryWidget: Simplify the search logic
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/DolphinQt/Debugger/MemoryWidget.cpp | 116 | ||||
| -rw-r--r-- | Source/Core/DolphinQt/Debugger/MemoryWidget.h | 4 |
2 files changed, 42 insertions, 78 deletions
diff --git a/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp b/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp index 5d065baf2f..fe1307ec41 100644 --- a/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp +++ b/Source/Core/DolphinQt/Debugger/MemoryWidget.cpp @@ -13,6 +13,7 @@ #include <QLineEdit> #include <QPushButton> #include <QRadioButton> +#include <QRegularExpression> #include <QScrollArea> #include <QSpacerItem> #include <QSplitter> @@ -116,7 +117,7 @@ void MemoryWidget::CreateWidgets() m_find_next = new QPushButton(tr("Find &Next")); m_find_previous = new QPushButton(tr("Find &Previous")); m_find_ascii = new QRadioButton(tr("ASCII")); - m_find_hex = new QRadioButton(tr("Hex")); + m_find_hex = new QRadioButton(tr("Hex string")); m_result_label = new QLabel; search_layout->addWidget(m_find_next); @@ -479,16 +480,10 @@ void MemoryWidget::ValidateSearchValue() QFont font; QPalette palette; - if (m_find_hex->isChecked() && !m_data_edit->text().isEmpty()) + if (!IsValueValid()) { - bool good; - m_data_edit->text().toULongLong(&good, 16); - - if (!good) - { - font.setBold(true); - palette.setColor(QPalette::Text, Qt::red); - } + font.setBold(true); + palette.setColor(QPalette::Text, Qt::red); } m_data_edit->setFont(font); @@ -523,45 +518,18 @@ void MemoryWidget::OnSetValue() return; } - AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); - - if (m_find_ascii->isChecked()) - { - const QByteArray bytes = m_data_edit->text().toUtf8(); - - for (char c : bytes) - accessors->WriteU8(addr++, static_cast<u8>(c)); - } - else + if (!IsValueValid()) { - bool good_value; - const QString text = m_data_edit->text(); - const int length = - text.startsWith(QStringLiteral("0x"), Qt::CaseInsensitive) ? text.size() - 2 : text.size(); - const u64 value = text.toULongLong(&good_value, 16); - - if (!good_value) - { - ModalMessageBox::critical(this, tr("Error"), tr("Bad value provided.")); - return; - } - - if (length <= 2) - { - accessors->WriteU8(addr, static_cast<u8>(value)); - } - else if (length <= 4) - { - accessors->WriteU16(addr, static_cast<u16>(value)); - } - else if (length <= 8) - { - accessors->WriteU32(addr, static_cast<u32>(value)); - } - else - accessors->WriteU64(addr, value); + ModalMessageBox::critical(this, tr("Error"), tr("Bad value provided.")); + return; } + AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); + + const QByteArray bytes = GetValueData(); + for (const char c : bytes) + accessors->WriteU8(addr++, static_cast<u8>(c)); + Update(); } @@ -616,46 +584,40 @@ void MemoryWidget::OnDumpFakeVMEM() std::distance(accessors->begin(), accessors->end())); } -std::vector<u8> MemoryWidget::GetValueData() const +bool MemoryWidget::IsValueValid() const { - std::vector<u8> search_for; // Series of bytes we want to look for - if (m_find_ascii->isChecked()) - { - const QByteArray bytes = m_data_edit->text().toUtf8(); - search_for.assign(bytes.begin(), bytes.end()); - } - else - { - bool good; - u64 value = m_data_edit->text().toULongLong(&good, 16); - - if (!good) - return {}; + return true; + const QRegularExpression is_hex(QStringLiteral("^([0-9A-F]{2})*$"), + QRegularExpression::CaseInsensitiveOption); + const QRegularExpressionMatch match = is_hex.match(m_data_edit->text()); + return match.hasMatch(); +} - int size; +QByteArray MemoryWidget::GetValueData() const +{ + if (!IsValueValid()) + return QByteArray(); - if (value == static_cast<u8>(value)) - size = sizeof(u8); - else if (value == static_cast<u16>(value)) - size = sizeof(u16); - else if (value == static_cast<u32>(value)) - size = sizeof(u32); - else - size = sizeof(u64); + const QByteArray value = m_data_edit->text().toUtf8(); - for (int i = size - 1; i >= 0; i--) - search_for.push_back((value >> (i * 8)) & 0xFF); - } + if (m_find_ascii->isChecked()) + return value; - return search_for; + return QByteArray::fromHex(value); } void MemoryWidget::FindValue(bool next) { - std::vector<u8> search_for = GetValueData(); + if (!IsValueValid()) + { + m_result_label->setText(tr("Bad value provided.")); + return; + } + + const QByteArray search_for = GetValueData(); - if (search_for.empty()) + if (search_for.isEmpty()) { m_result_label->setText(tr("No Value Given")); return; @@ -671,8 +633,8 @@ void MemoryWidget::FindValue(bool next) AddressSpace::Accessors* accessors = AddressSpace::GetAccessors(m_memory_view->GetAddressSpace()); - auto found_addr = - accessors->Search(addr, search_for.data(), static_cast<u32>(search_for.size()), next); + const auto found_addr = accessors->Search(addr, reinterpret_cast<const u8*>(search_for.data()), + static_cast<u32>(search_for.size()), next); if (found_addr.has_value()) { diff --git a/Source/Core/DolphinQt/Debugger/MemoryWidget.h b/Source/Core/DolphinQt/Debugger/MemoryWidget.h index c0240c7085..2a96f9d2ec 100644 --- a/Source/Core/DolphinQt/Debugger/MemoryWidget.h +++ b/Source/Core/DolphinQt/Debugger/MemoryWidget.h @@ -5,6 +5,7 @@ #include <vector> +#include <QByteArray> #include <QDockWidget> #include "Common/CommonTypes.h" @@ -56,7 +57,8 @@ private: void OnDumpARAM(); void OnDumpFakeVMEM(); - std::vector<u8> GetValueData() const; + bool IsValueValid() const; + QByteArray GetValueData() const; void FindValue(bool next); |
