Skip to content

Commit d556c65

Browse files
committed
Switch to using QLatin1StringView, pull arrays out of AsmHighlighter into datawidget.cpp
1 parent 81823fe commit d556c65

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

gui/qt/datawidget.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,33 @@
33
#include "mainwindow.h"
44

55
#include <QtWidgets/QApplication>
6+
#include <QStringView>
7+
#include <QtCore/QLatin1StringView>
8+
9+
#include <algorithm>
10+
#include <array>
11+
12+
13+
using namespace Qt::StringLiterals;
14+
15+
namespace {
16+
inline constexpr std::array controlFlowMnemonics {
17+
"CALL"_L1, "DJNZ"_L1, "JP"_L1, "JR"_L1, "RET"_L1, "RETI"_L1, "RETN"_L1, "RST"_L1
18+
};
19+
20+
inline constexpr std::array noTargetMnemonics {
21+
"RET"_L1, "RETI"_L1, "RETN"_L1
22+
};
23+
24+
inline constexpr std::array reservedTokens {
25+
"A"_L1, "B"_L1, "C"_L1, "D"_L1, "E"_L1, "H"_L1, "L"_L1, "I"_L1, "R"_L1,
26+
"AF"_L1, "BC"_L1, "DE"_L1, "HL"_L1, "SP"_L1,
27+
"IX"_L1, "IY"_L1, "IXH"_L1, "IXL"_L1,
28+
"IYH"_L1, "IYL"_L1, "MB"_L1,
29+
"NZ"_L1, "Z"_L1, "NC"_L1, "C"_L1,
30+
"PO"_L1, "PE"_L1, "P"_L1, "M"_L1
31+
};
32+
}
633

734
DataWidget::DataWidget(QWidget *parent) : QPlainTextEdit{parent} {
835
moveable = false;
@@ -193,7 +220,11 @@ void AsmHighlighter::highlightBlock(const QString &text) {
193220

194221
const QString fullMnemonic = match.captured(6);
195222
const QString primary = fullMnemonic.section('.', 0, 0).toUpper();
196-
const bool isControlFlow = std::find(controlFlowMnemonics.begin(), controlFlowMnemonics.end(), primary.toStdString()) != controlFlowMnemonics.end();
223+
const QStringView pv{primary};
224+
225+
const bool isControlFlow = std::any_of(controlFlowMnemonics.begin(), controlFlowMnemonics.end(),
226+
[&](const QLatin1StringView tok){ return pv == tok; });
227+
197228
if (isControlFlow) {
198229
setFormat(match.capturedStart(6), match.capturedLength(6), controlFlowFormat);
199230
}
@@ -212,7 +243,8 @@ void AsmHighlighter::highlightBlock(const QString &text) {
212243
return;
213244
}
214245

215-
if (std::find(noTargetMnemonics.begin(), noTargetMnemonics.end(), primary.toStdString()) != noTargetMnemonics.end()) {
246+
if (std::any_of(noTargetMnemonics.begin(), noTargetMnemonics.end(),
247+
[&](const QLatin1StringView tok){ return pv == tok; })) {
216248
return;
217249
}
218250

@@ -239,7 +271,8 @@ void AsmHighlighter::highlightBlock(const QString &text) {
239271
const auto m3 = lIt.next();
240272
if (m3.hasMatch()) {
241273
const QString tok = m3.captured().toUpper();
242-
if (std::find(reservedTokens.begin(), reservedTokens.end(), tok.toStdString()) != reservedTokens.end()) {
274+
if (std::any_of(reservedTokens.begin(), reservedTokens.end(),
275+
[&](const QLatin1StringView t){ return tok == t; })) {
243276
continue;
244277
}
245278
const int start = operandStart + m3.capturedStart();

gui/qt/datawidget.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,6 @@ class AsmHighlighter : public QSyntaxHighlighter
4747
QTextCharFormat parenFormat;
4848
QTextCharFormat registerFormat;
4949

50-
std::array<std::string_view, 8> controlFlowMnemonics {
51-
"CALL",
52-
"DJNZ",
53-
"JP",
54-
"JR",
55-
"RET",
56-
"RETI",
57-
"RETN",
58-
"RST"
59-
};
60-
61-
std::array<std::string_view, 3> noTargetMnemonics {
62-
"RET",
63-
"RETI",
64-
"RETN"
65-
};
66-
67-
std::array<std::string_view, 29> reservedTokens {
68-
"A", "B", "C", "D", "E", "H", "L", "I", "R",
69-
"AF", "BC", "DE", "HL", "SP",
70-
"IX", "IY", "IXH", "IXL",
71-
"IYH", "IYL", "MB",
72-
"NZ", "Z", "NC", "C",
73-
"PO", "PE", "P", "M"
74-
};
75-
7650
QRegularExpression labelPattern;
7751
QRegularExpression instructionPattern;
7852
};

0 commit comments

Comments
 (0)