From f50d2acf8ae31ef37606360f265e3f28fee7b5ca Mon Sep 17 00:00:00 2001 From: RickyRister Date: Tue, 11 Mar 2025 23:44:26 -0700 Subject: [PATCH] get thing to work --- oracle/src/parsehelpers.cpp | 49 ++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/oracle/src/parsehelpers.cpp b/oracle/src/parsehelpers.cpp index 779ba956f..893378eec 100644 --- a/oracle/src/parsehelpers.cpp +++ b/oracle/src/parsehelpers.cpp @@ -3,30 +3,51 @@ #include /** - * Parses the card text to determine if the card enters the battlefield tapped. + * Parses the card text to determine if the card should have the cipt tag * - * The parsing logic will be able to handle the following cases: - * - " enters tapped..." - * - shortname, if it appears at the start of the name. - * - "This enters tapped..." + * The parsing logic is able to handle the following cases: + * - " enters tapped" + * - " enters tapped", if the card name starts with the shortname + * - "This enters tapped" + * - "..., it enters tapped" * - Any naming scheme that appends a non-alphanumeric character plus extra text to the end of the name. - * (e.g. name is "Card Name_SET" or "Card Name (Set)" and text contains "Card Name enters tapped...") + * (e.g. name is "Card Name_SET" or "Card Name (Set)" and text contains "Card Name enters tapped") * - * However, it will still miss certain cases: - * - shortnames that aren't the first word in the card name + * However, it will still miss on certain cases: + * - shortnames that aren't the at the beginning of the card name + * + * Note that "...enters tapped unless..." returns false. * * @param name The name of the card * @param text The oracle text of the card */ bool parseCipt(const QString &name, const QString &text) { - static auto WHITESPACE_REGEX = QRegularExpression("\\s+"); - QString shortname = name.split(WHITESPACE_REGEX, Qt::SkipEmptyParts).first(); + // try to split shortname on all non-alphanumeric characters (including _) + static auto SHORTNAME_DIVIDERS = QRegularExpression("[^\\w]|_"); - QString namePattern = QString("( it|this [^ ]* |%1|%2.*)") - .arg(QRegularExpression::escape(name), QRegularExpression::escape(shortname)); + // Try all possible shortnames. + // This also handles the case of extra text appended at end. + QStringList possibleNames; + qsizetype i = 0; + while ((i = name.indexOf(SHORTNAME_DIVIDERS, i)) != -1) { + possibleNames.append(QRegularExpression::escape(name.first(i))); + ++i; + } - QRegularExpression ciptRegex(namePattern + " enters( the battlefield)? tapped(?! unless)"); + // and the full name + possibleNames.append(QRegularExpression::escape(name)); - return ciptRegex.match(text).hasMatch(); + QString subject = "(it|" // "..., it enters tapped" + "(T|t)his [^ ]+|" // "This enters tapped" + + possibleNames.join("|") + ")"; + + auto ciptPattern = QRegularExpression( + // cipt phrase is either first sentence of line, or is after a punctuation mark + "(^|(, |\\. ))" + subject + + // support old wording, and exclude the "unless" case + " enters( the battlefield)? tapped(?! unless)", + QRegularExpression::MultilineOption); + + return ciptPattern.match(text).hasMatch(); }