[DeckList] Harden plain-text parser regexes and move metadata clearing up

This commit is contained in:
Lukas Brübach 2026-09-18 09:52:10 +02:00 committed by GitHub
parent d71b614c44
commit 9e312a0ef4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 19 deletions

View file

@ -250,7 +250,10 @@ bool DeckList::loadFromStream_Plain(QTextStream &in,
bool preserveMetadata,
const std::function<QString(const QString &)> &cardNameNormalizer)
{
bool ok = DeckListPlainText::parse(in, preserveMetadata, cardNameNormalizer, metadata, tree);
if (!preserveMetadata) {
metadata = {};
}
bool ok = DeckListPlainText::parse(in, cardNameNormalizer, metadata, tree);
refreshDeckHash();
return ok;
}

View file

@ -10,30 +10,26 @@ namespace DeckListPlainText
{
bool parse(QTextStream &in,
bool preserveMetadata,
const std::function<QString(const QString &)> &cardNameNormalizer,
DeckList::Metadata &metadata,
DecklistNodeTree &tree)
{
tree.clear();
if (!preserveMetadata) {
metadata = {};
}
const QRegularExpression reCardLine(R"(^\s*[\w\[\(\{].*$)", QRegularExpression::UseUnicodePropertiesOption);
const QRegularExpression reEmpty("^\\s*$");
const QRegularExpression reComment(R"([\w\[\(\{].*$)", QRegularExpression::UseUnicodePropertiesOption);
const QRegularExpression reSBMark("^\\s*sb:\\s*(.+)", QRegularExpression::CaseInsensitiveOption);
const QRegularExpression reSBComment("^sideboard\\b.*$", QRegularExpression::CaseInsensitiveOption);
const QRegularExpression reDeckComment("^((main)?deck(list)?|mainboard)\\b",
QRegularExpression::CaseInsensitiveOption);
static const QRegularExpression reCardLine(R"(^\s*[\w\[\(\{].*$)", QRegularExpression::UseUnicodePropertiesOption);
static const QRegularExpression reEmpty("^\\s*$");
static const QRegularExpression reComment(R"([\w\[\(\{].*$)", QRegularExpression::UseUnicodePropertiesOption);
static const QRegularExpression reSBMark("^\\s*sb:\\s*(.+)", QRegularExpression::CaseInsensitiveOption);
static const QRegularExpression reSBComment("^sideboard\\b.*$", QRegularExpression::CaseInsensitiveOption);
static const QRegularExpression reDeckComment("^((main)?deck(list)?|mainboard)\\b",
QRegularExpression::CaseInsensitiveOption);
// Regex for advanced card parsing
const QRegularExpression reMultiplier(R"(^[xX\(\[]*(\d+)[xX\*\)\]]* ?(.+))");
static const QRegularExpression reMultiplier(R"(^[xX\(\[]*(\d+)[xX\*\)\]]* ?(.+))");
// Regex for extracting set code and collector number with attached symbols
const QRegularExpression reHyphenFormat(R"(\((\w{3,})\)\s+(\w{3,})-(\d+[^\w\s]*))");
const QRegularExpression reRegularFormat(R"(\((\w{3,})\)\s+(\d+[^\w\s]*))");
static const QRegularExpression reHyphenFormat(R"(\((\w{3,})\)\s+(\w{3,})-(\d+[^\w\s]*))");
static const QRegularExpression reRegularFormat(R"(\((\w{3,})\)\s+(\d+[^\w\s]*))");
auto inputs = in.readAll().trimmed().split('\n');
auto max_line = inputs.size();

View file

@ -14,11 +14,9 @@ namespace DeckListPlainText
/**
* @brief Parses a plain-text deck list into a tree and its metadata.
*
* Clears the tree first, and clears the metadata unless @p preserveMetadata is
* true, then fills both from the text.
* Clears the tree first, then fills both from the text.
*
* @param in The text to load
* @param preserveMetadata If true, don't clear the existing metadata
* @param cardNameNormalizer Function that takes the parsed card name string
* in the text and returns the name to store
* @param metadata Deck metadata written by the parser
@ -26,7 +24,6 @@ namespace DeckListPlainText
* @return False if the input was empty, true otherwise.
*/
bool parse(QTextStream &in,
bool preserveMetadata,
const std::function<QString(const QString &)> &cardNameNormalizer,
DeckList::Metadata &metadata,
DecklistNodeTree &tree);