diff --git a/src/config.cpp b/src/config.cpp index 9b48664a..77cb9992 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -92,7 +92,7 @@ void KeyValueConfigBase::load() { QTextStream in(&file); QList configLines; - QRegularExpression re("^(?[^=]+)=(?.*)$"); + static const QRegularExpression re("^(?[^=]+)=(?.*)$"); while (!in.atEnd()) { QString line = in.readLine().trimmed(); int commentIndex = line.indexOf("#"); @@ -1132,7 +1132,7 @@ QString ShortcutsConfig::cfgKey(const QObject *object) const { cfg_key = parentWidget->window()->objectName() + '_'; cfg_key += object->objectName(); - QRegularExpression re("[A-Z]"); + static const QRegularExpression re("[A-Z]"); int i = cfg_key.indexOf(re, 1); while (i != -1) { if (cfg_key.at(i - 1) != '_') diff --git a/src/core/map.cpp b/src/core/map.cpp index 68f21589..c0ee4d77 100644 --- a/src/core/map.cpp +++ b/src/core/map.cpp @@ -31,9 +31,11 @@ void Map::setName(QString mapName) { QString Map::mapConstantFromName(QString mapName) { // Transform map names of the form 'GraniteCave_B1F` into map constants like 'MAP_GRANITE_CAVE_B1F'. - QString nameWithUnderscores = mapName.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2"); + static const QRegularExpression caseChange("([a-z])([A-Z])"); + QString nameWithUnderscores = mapName.replace(caseChange, "\\1_\\2"); QString withMapAndUppercase = "MAP_" + nameWithUnderscores.toUpper(); - QString constantName = withMapAndUppercase.replace(QRegularExpression("_+"), "_"); + static const QRegularExpression underscores("_+"); + QString constantName = withMapAndUppercase.replace(underscores, "_"); // Handle special cases. // SSTidal needs to be SS_TIDAL, rather than SSTIDAL diff --git a/src/core/maplayout.cpp b/src/core/maplayout.cpp index 310017be..0f5deecd 100644 --- a/src/core/maplayout.cpp +++ b/src/core/maplayout.cpp @@ -4,9 +4,11 @@ QString MapLayout::layoutConstantFromName(QString mapName) { // Transform map names of the form 'GraniteCave_B1F` into layout constants like 'LAYOUT_GRANITE_CAVE_B1F'. - QString nameWithUnderscores = mapName.replace(QRegularExpression("([a-z])([A-Z])"), "\\1_\\2"); + static const QRegularExpression caseChange("([a-z])([A-Z])"); + QString nameWithUnderscores = mapName.replace(caseChange, "\\1_\\2"); QString withMapAndUppercase = "LAYOUT_" + nameWithUnderscores.toUpper(); - QString constantName = withMapAndUppercase.replace(QRegularExpression("_+"), "_"); + static const QRegularExpression underscores("_+"); + QString constantName = withMapAndUppercase.replace(underscores, "_"); // Handle special cases. // SSTidal should be SS_TIDAL, rather than SSTIDAL diff --git a/src/core/paletteutil.cpp b/src/core/paletteutil.cpp index 4bc7eab3..da281ce6 100644 --- a/src/core/paletteutil.cpp +++ b/src/core/paletteutil.cpp @@ -118,7 +118,7 @@ QList parseJASC(QString filepath, bool *error) { } QList palette; - QRegularExpression re("(?\\d+)\\s(?\\d+)\\s(?\\d+)"); + static const QRegularExpression re("(?\\d+)\\s(?\\d+)\\s(?\\d+)"); while (!in.atEnd() && numColors > 0) { numColors--; QString line = in.readLine(); diff --git a/src/core/parseutil.cpp b/src/core/parseutil.cpp index 83954f8e..0f551bf3 100644 --- a/src/core/parseutil.cpp +++ b/src/core/parseutil.cpp @@ -40,7 +40,8 @@ void ParseUtil::logRecordedErrors() { } QString ParseUtil::createErrorMessage(const QString &message, const QString &expression) { - QStringList lines = this->text.split(QRegularExpression("[\r\n]")); + static const QRegularExpression newline("[\r\n]"); + QStringList lines = this->text.split(newline); int lineNum = 0, colNum = 0; for (QString line : lines) { lineNum++; @@ -90,9 +91,11 @@ QList ParseUtil::parseAsm(const QString &filename) { // There should not be anything else on the line. // gas will raise a syntax error if there is. } else { - int index = trimmedLine.indexOf(QRegularExpression("\\s+")); + static const QRegularExpression re_spaces("\\s+"); + int index = trimmedLine.indexOf(re_spaces); const QString macro = trimmedLine.left(index); - QStringList params(trimmedLine.right(trimmedLine.length() - index).trimmed().split(QRegularExpression("\\s*,\\s*"))); + static const QRegularExpression re_spacesCommaSpaces("\\s*,\\s*"); + QStringList params(trimmedLine.right(trimmedLine.length() - index).trimmed().split(re_spacesCommaSpaces)); params.prepend(macro); parsed.append(params); } @@ -110,7 +113,7 @@ QList ParseUtil::tokenizeExpression(QString expression, const QMap tokens; QStringList tokenTypes = (QStringList() << "hex" << "decimal" << "identifier" << "operator" << "leftparen" << "rightparen"); - QRegularExpression re("^(?0x[0-9a-fA-F]+)|(?[0-9]+)|(?[a-zA-Z_0-9]+)|(?[+\\-*\\/<>|^%]+)|(?\\()|(?\\))"); + static const QRegularExpression re("^(?0x[0-9a-fA-F]+)|(?[0-9]+)|(?[a-zA-Z_0-9]+)|(?[+\\-*\\/<>|^%]+)|(?\\()|(?\\))"); expression = expression.trimmed(); while (!expression.isEmpty()) { @@ -257,7 +260,7 @@ QString ParseUtil::readCIncbin(const QString &filename, const QString &label) { this->text = readTextFile(this->root + "/" + filename); - QRegularExpression re(QString( + static const QRegularExpression re(QString( "\\b%1\\b" "\\s*\\[?\\s*\\]?\\s*=\\s*" "INCBIN_[US][0-9][0-9]?" @@ -282,14 +285,14 @@ QStringList ParseUtil::readCIncbinArray(const QString &filename, const QString & this->text = readTextFile(this->root + "/" + filename); // Get the text starting after the label all the way to the definition's end - QRegularExpression re(QString("\\b%1\\b(.*?)};").arg(label), QRegularExpression::DotMatchesEverythingOption); - QRegularExpressionMatch arrayMatch = re.match(this->text); + static const QRegularExpression re_labelGroup(QString("\\b%1\\b(.*?)};").arg(label), QRegularExpression::DotMatchesEverythingOption); + QRegularExpressionMatch arrayMatch = re_labelGroup.match(this->text); if (!arrayMatch.hasMatch()) return paths; // Extract incbin paths from the array - re.setPattern("INCBIN_[US][0-9][0-9]?\\(\\s*\"([^\"]*)\"\\s*\\)"); - QRegularExpressionMatchIterator iter = re.globalMatch(arrayMatch.captured(1)); + static const QRegularExpression re_incbin("INCBIN_[US][0-9][0-9]?\\(\\s*\"([^\"]*)\"\\s*\\)"); + QRegularExpressionMatchIterator iter = re_incbin.globalMatch(arrayMatch.captured(1)); while (iter.hasNext()) { paths.append(iter.next().captured(1)); } @@ -316,12 +319,14 @@ QMap ParseUtil::readCDefines(const QString &filename, return filteredDefines; } - this->text.replace(QRegularExpression("(//.*)|(\\/+\\*+[^*]*\\*+\\/+)"), ""); - this->text.replace(QRegularExpression("(\\\\\\s+)"), ""); + static const QRegularExpression re_extraChars("(//.*)|(\\/+\\*+[^*]*\\*+\\/+)"); + this->text.replace(re_extraChars, ""); + static const QRegularExpression re_extraSpaces("(\\\\\\s+)"); + this->text.replace(re_extraSpaces, ""); allDefines.insert("FALSE", 0); allDefines.insert("TRUE", 1); - QRegularExpression re("#define\\s+(?\\w+)[^\\S\\n]+(?.+)"); + static const QRegularExpression re("#define\\s+(?\\w+)[^\\S\\n]+(?.+)"); QRegularExpressionMatchIterator iter = re.globalMatch(this->text); this->errorMap.clear(); while (iter.hasNext()) { @@ -368,7 +373,7 @@ QStringList ParseUtil::readCArray(const QString &filename, const QString &label) this->file = filename; this->text = readTextFile(this->root + "/" + filename); - QRegularExpression re(QString(R"(\b%1\b\s*(\[?[^\]]*\])?\s*=\s*\{([^\}]*)\})").arg(label)); + static const QRegularExpression re(QString(R"(\b%1\b\s*(\[?[^\]]*\])?\s*=\s*\{([^\}]*)\})").arg(label)); QRegularExpressionMatch match = re.match(this->text); if (match.hasMatch()) { @@ -376,7 +381,8 @@ QStringList ParseUtil::readCArray(const QString &filename, const QString &label) QStringList split = body.split(','); for (QString item : split) { item = item.trimmed(); - if (!item.contains(QRegularExpression("[^A-Za-z0-9_&()\\s]"))) list.append(item); + static const QRegularExpression validChars("[^A-Za-z0-9_&()\\s]"); + if (!item.contains(validChars)) list.append(item); // do not print error info here because this is called dozens of times } } @@ -388,10 +394,11 @@ QMap ParseUtil::readNamedIndexCArray(const QString &filename, this->text = readTextFile(this->root + "/" + filename); QMap map; - QRegularExpression re_text(QString(R"(\b%1\b\s*(\[?[^\]]*\])?\s*=\s*\{([^\}]*)\})").arg(label)); - QString body = re_text.match(this->text).captured(2).replace(QRegularExpression("\\s*"), ""); + static const QRegularExpression re_text(QString(R"(\b%1\b\s*(\[?[^\]]*\])?\s*=\s*\{([^\}]*)\})").arg(label)); + static const QRegularExpression re_spaces("\\s*"); + QString body = re_text.match(this->text).captured(2).replace(re_spaces, ""); - QRegularExpression re("\\[(?[A-Za-z0-9_]*)\\]=(?&?[A-Za-z0-9_]*)"); + static const QRegularExpression re("\\[(?[A-Za-z0-9_]*)\\]=(?&?[A-Za-z0-9_]*)"); QRegularExpressionMatchIterator iter = re.globalMatch(body); while (iter.hasNext()) { diff --git a/src/core/regionmap.cpp b/src/core/regionmap.cpp index 19597e75..f9c1fb9d 100644 --- a/src/core/regionmap.cpp +++ b/src/core/regionmap.cpp @@ -161,8 +161,8 @@ bool RegionMap::loadLayout(poryjson::Json layoutJson) { ParseUtil parser; QString text = parser.readTextFile(fullPath(this->layout_path)); - QRegularExpression re("(?static)?\\s?(?const)?\\s?(?[A-Za-z0-9_]+)?\\s+(?