Skip to content

Commit

Permalink
Port coordinate editor to use QRegularExpression
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew J. Milner <[email protected]>
  • Loading branch information
matterhorn103 committed Oct 23, 2024
1 parent 37af396 commit b6d8de8
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions avogadro/qtplugins/coordinateeditor/coordinateeditordialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <QtGui/QClipboard>
#include <QtGui/QFont>
#include <QtGui/QIcon>
#include <QtGui/QRegExpValidator>
#include <QtGui/QRegularExpressionValidator>
#include <QtGui/QTextCursor>
#include <QtGui/QTextDocument>
#include <QtWidgets/QApplication>
Expand All @@ -29,7 +29,7 @@
#include <QtCore/QDebug>
#include <QtCore/QMimeData>
#include <QtCore/QMutableListIterator>
#include <QtCore/QRegExp>
#include <QtCore/QRegularExpression>
#include <QtCore/QString>
#include <QtCore/QTimer>

Expand Down Expand Up @@ -77,18 +77,19 @@ enum TokenType
};

// Some frequently used regexes:
static const QRegExp TOKEN_SEPARATOR("[\\s,;]+");
static const QRegExp VALID_TOKEN("[^\\s,;]+");
static const QRegExp INT_CHECKER("(:?[+-])?\\d+");
static const QRegExp DOUBLE_CHECKER("(:?[+-])?" // Leading sign
"(:?" // Must match one of the following:
"\\d*\\.\\d*" // Fractional part
"|" // or
"\\d+[Ee](:?[+-])?\\d+" // Exponential part
"|" // or
"\\d*\\.\\d*" // Fractional part and
"[Ee](:?[+-])?\\d+" // Exponential part
")");
static const QRegularExpression TOKEN_SEPARATOR("[\\s,;]+");
static const QRegularExpression VALID_TOKEN("[^\\s,;]+");
// These two need to be exact
static const QRegularExpression INT_CHECKER(QRegularExpression::anchoredPattern("(:?[+-])?\\d+"));
static const QRegularExpression DOUBLE_CHECKER(QRegularExpression::anchoredPattern("(:?[+-])?" // Leading sign
"(:?" // Must match one of the following:
"\\d*\\.\\d*" // Fractional part
"|" // or
"\\d+[Ee](:?[+-])?\\d+" // Exponential part
"|" // or
"\\d*\\.\\d*" // Fractional part and
"[Ee](:?[+-])?\\d+" // Exponential part
")"));

struct AtomStruct
{
Expand Down Expand Up @@ -140,8 +141,8 @@ CoordinateEditorDialog::CoordinateEditorDialog(QWidget* parent_)
SLOT(textModified(bool)));

// Setup spec edit
QRegExp specRegExp("[#ZGSLNabcxyz01_]*");
auto* specValidator = new QRegExpValidator(specRegExp, this);
QRegularExpression specRegExp("[#ZGSLNabcxyz01_]*");
auto* specValidator = new QRegularExpressionValidator(specRegExp, this);
m_ui->spec->setValidator(specValidator);
connect(m_ui->presets, SIGNAL(currentIndexChanged(int)),
SLOT(presetChanged(int)));
Expand Down Expand Up @@ -392,13 +393,14 @@ void CoordinateEditorDialog::validateInputWorker()
cleanToken.replace(0, 1, cleanToken[0].toUpper());

// Split the label into symbol and number
QRegExp labelSplitter("([A-Z][a-z]?)(\\d+)");
if (labelSplitter.indexIn(cleanToken) == -1) {
QRegularExpression labelSplitter("([A-Z][a-z]?)(\\d+)");
QRegularExpressionMatch match = labelSplitter.match(cleanToken);
if (match.hasMatch()) {
m_ui->text->markInvalid(tokenCursor, tr("Invalid atom label."));
break;
}
// check the symbol
std::string tokenStd(labelSplitter.cap(1).toStdString());
std::string tokenStd(match.captured(1).toStdString());
atom.atomicNumber = Elements::atomicNumberFromSymbol(tokenStd);
if (atom.atomicNumber == Avogadro::InvalidElement)
m_ui->text->markInvalid(tokenCursor, tr("Invalid element symbol."));
Expand Down Expand Up @@ -682,9 +684,9 @@ QString CoordinateEditorDialog::detectInputFormat() const

foreach (const QString& token, tokens) {
TokenType tokenType = String;
if (INT_CHECKER.exactMatch(token))
if (INT_CHECKER.match(token).hasMatch())
tokenType = Integer;
else if (DOUBLE_CHECKER.exactMatch(token))
else if (DOUBLE_CHECKER.match(token).hasMatch())
tokenType = Double;
++tokenTypeCounts[tokenType];
tokenTypes << tokenType;
Expand Down Expand Up @@ -797,8 +799,8 @@ QString CoordinateEditorDialog::detectInputFormat() const

// Check the current specification -- if a|b|c appears before x|y|z, assume
// that the specified coordinates are lattice coords
static QRegExp cartesianSniffer("x|y|z");
static QRegExp fractionalSniffer("a|b|c");
static QRegularExpression cartesianSniffer("x|y|z");
static QRegularExpression fractionalSniffer("a|b|c");
const QString currentSpec(m_ui->spec->text());
int cartesianIndex = currentSpec.indexOf(cartesianSniffer);
int fractionalIndex = currentSpec.indexOf(fractionalSniffer);
Expand Down

0 comments on commit b6d8de8

Please sign in to comment.