From f9f241ac259dcf6244628bd79854f10ce8b93469 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Wed, 26 Jul 2017 19:28:26 +0300 Subject: [PATCH] Fix variable name regular expression. Forbid characters "." and ",". --HG-- branch : develop --- src/libs/ifc/schema/pattern/v0.5.1.xsd | 2 +- src/libs/qmuparser/qmudef.cpp | 55 +++++++++++++++++++++++ src/libs/qmuparser/qmudef.h | 45 ++++++++----------- src/libs/qmuparser/qmuformulabase.cpp | 2 + src/test/ValentinaTest/tst_nameregexp.cpp | 12 +++++ 5 files changed, 89 insertions(+), 27 deletions(-) diff --git a/src/libs/ifc/schema/pattern/v0.5.1.xsd b/src/libs/ifc/schema/pattern/v0.5.1.xsd index 383ed1097..86976e8f5 100644 --- a/src/libs/ifc/schema/pattern/v0.5.1.xsd +++ b/src/libs/ifc/schema/pattern/v0.5.1.xsd @@ -647,7 +647,7 @@ - + diff --git a/src/libs/qmuparser/qmudef.cpp b/src/libs/qmuparser/qmudef.cpp index 9db7512f6..5a55325bd 100644 --- a/src/libs/qmuparser/qmudef.cpp +++ b/src/libs/qmuparser/qmudef.cpp @@ -77,6 +77,8 @@ static QChar EatWhiteSpace(const QString &formula, int &index) static int CheckChar(QChar &c, const QLocale &locale, const QChar &decimal, const QChar &thousand) { INIT_LOCALE_VARIABLES(locale); + Q_UNUSED(decimalPoint) + Q_UNUSED(groupSeparator) if (c == positiveSign) { @@ -175,6 +177,8 @@ int ReadVal(const QString &formula, qreal &val, const QLocale &locale, const QCh } INIT_LOCALE_VARIABLES(locale); + Q_UNUSED(decimalPoint) + Q_UNUSED(groupSeparator) QSet reserved; reserved << positiveSign @@ -272,3 +276,54 @@ int ReadVal(const QString &formula, qreal &val, const QLocale &locale, const QCh return -1; } + +//--------------------------------------------------------------------------------------------------------------------- +QString NameRegExp() +{ + static QString regex; + + if (regex.isEmpty()) + { + const QList allLocales = + QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); + + QString positiveSigns; + QString negativeSigns; + QString decimalPoints; + QString groupSeparators; + + for(int i = 0; i < allLocales.size(); ++i) + { + if (not positiveSigns.contains(allLocales.at(i).positiveSign())) + { + positiveSigns.append(allLocales.at(i).positiveSign()); + } + + if (not negativeSigns.contains(allLocales.at(i).negativeSign())) + { + negativeSigns.append(allLocales.at(i).negativeSign()); + } + + if (not decimalPoints.contains(allLocales.at(i).decimalPoint())) + { + decimalPoints.append(allLocales.at(i).decimalPoint()); + } + + if (not groupSeparators.contains(allLocales.at(i).groupSeparator())) + { + groupSeparators.append(allLocales.at(i).groupSeparator()); + } + } + + negativeSigns.replace('-', "\\-"); + + //Same regexp in pattern.xsd shema file. Don't forget to synchronize. + // \p{Nd} - \p{Decimal_Digit_Number} + // \p{Zs} - \p{Space_Separator} + regex = QString("^([^\\p{Nd}\\p{Zs}*/&|!<>^\\()%1%2%3%4=?:;'\"]){1,1}" + "([^\\p{Zs}*/&|!<>^\\()%1%2%3%4=?:;\"]){0,}$") + .arg(negativeSigns).arg(positiveSigns).arg(decimalPoints).arg(groupSeparators); + } + + return regex; +} diff --git a/src/libs/qmuparser/qmudef.h b/src/libs/qmuparser/qmudef.h index f0706ba6e..5a4c30e53 100644 --- a/src/libs/qmuparser/qmudef.h +++ b/src/libs/qmuparser/qmudef.h @@ -24,8 +24,6 @@ #include "qmuparser_global.h" -#include - #if QT_VERSION < QT_VERSION_CHECK(5, 5, 0) /* @@ -92,32 +90,27 @@ QT_WARNING_DISABLE_GCC("-Wattributes") #endif /* Q_CC_MSVC */ class QLocale; +class QChar; -#define INIT_LOCALE_VARIABLES(locale) \ -const QChar positiveSign = (locale).positiveSign(); \ -const QChar negativeSign = (locale).negativeSign(); \ -const QChar sign0 = (locale).toString(0).at(0); \ -const QChar sign1 = (locale).toString(1).at(0); \ -const QChar sign2 = (locale).toString(2).at(0); \ -const QChar sign3 = (locale).toString(3).at(0); \ -const QChar sign4 = (locale).toString(4).at(0); \ -const QChar sign5 = (locale).toString(5).at(0); \ -const QChar sign6 = (locale).toString(6).at(0); \ -const QChar sign7 = (locale).toString(7).at(0); \ -const QChar sign8 = (locale).toString(8).at(0); \ -const QChar sign9 = (locale).toString(9).at(0); \ -const QChar expUpper = (locale).exponential().toUpper(); \ -const QChar expLower = (locale).exponential().toLower() \ +#define INIT_LOCALE_VARIABLES(locale) \ +const QChar positiveSign = (locale).positiveSign(); \ +const QChar negativeSign = (locale).negativeSign(); \ +const QChar sign0 = (locale).toString(0).at(0); \ +const QChar sign1 = (locale).toString(1).at(0); \ +const QChar sign2 = (locale).toString(2).at(0); \ +const QChar sign3 = (locale).toString(3).at(0); \ +const QChar sign4 = (locale).toString(4).at(0); \ +const QChar sign5 = (locale).toString(5).at(0); \ +const QChar sign6 = (locale).toString(6).at(0); \ +const QChar sign7 = (locale).toString(7).at(0); \ +const QChar sign8 = (locale).toString(8).at(0); \ +const QChar sign9 = (locale).toString(9).at(0); \ +const QChar expUpper = (locale).exponential().toUpper(); \ +const QChar expLower = (locale).exponential().toLower(); \ +const QChar decimalPoint = (locale).decimalPoint(); \ +const QChar groupSeparator = (locale).groupSeparator() \ -//--------------------------------------------------------------------------------------------------------------------- -inline QString NameRegExp() -{ - //Same regexp in pattern.xsd shema file. Don't forget to synchronize. - // \p{Nd} - \p{Decimal_Digit_Number} - // \p{Zs} - \p{Space_Separator} - // Note. All three minus characters are different! - return QStringLiteral("^([^\\p{Nd}\\p{Zs}*/&|!<>^\\-()–+−=?:;'\"]){1,1}([^\\p{Zs}*/&|!<>^\\-()–+−=?:;\"]){0,}$"); -} +QString NameRegExp(); QT_WARNING_POP diff --git a/src/libs/qmuparser/qmuformulabase.cpp b/src/libs/qmuparser/qmuformulabase.cpp index d43aae10f..58b3b9f4e 100644 --- a/src/libs/qmuparser/qmuformulabase.cpp +++ b/src/libs/qmuparser/qmuformulabase.cpp @@ -71,6 +71,8 @@ void QmuFormulaBase::InitCharSets() INIT_LOCALE_VARIABLES(m_locale); Q_UNUSED(expUpper) Q_UNUSED(expLower) + Q_UNUSED(decimalPoint) + Q_UNUSED(groupSeparator) // Defining identifier character sets const QString nameChars = QString() + sign0 + sign1 + sign2 + sign3 + sign4 + sign5 + sign6 + sign7 + sign8 + diff --git a/src/test/ValentinaTest/tst_nameregexp.cpp b/src/test/ValentinaTest/tst_nameregexp.cpp index 8f6995fe7..000f4bea9 100644 --- a/src/test/ValentinaTest/tst_nameregexp.cpp +++ b/src/test/ValentinaTest/tst_nameregexp.cpp @@ -90,8 +90,20 @@ void TST_NameRegExp::TestNameRegExp_data() tag = localeName+QLatin1String(". First character can't be \"")+negativeSign+QLatin1String("\""); QTest::newRow(qUtf8Printable(tag)) << negativeSign+QLatin1String("a") << false; + tag = localeName+QLatin1String(". First character can't be \"")+decimalPoint+QLatin1String("\""); + QTest::newRow(qUtf8Printable(tag)) << decimalPoint+QLatin1String("a") << false; + + tag = localeName+QLatin1String(". First character can't be \"")+groupSeparator+QLatin1String("\""); + QTest::newRow(qUtf8Printable(tag)) << groupSeparator+QLatin1String("a") << false; + tag = localeName+QLatin1String(". Any next character can't be \"")+negativeSign+QLatin1String("\""); QTest::newRow(qUtf8Printable(tag)) << QLatin1String("a")+negativeSign << false; + + tag = localeName+QLatin1String(". Any next character can't be \"")+decimalPoint+QLatin1String("\""); + QTest::newRow(qUtf8Printable(tag)) << QLatin1String("a")+decimalPoint << false; + + tag = localeName+QLatin1String(". Any next character can't be \"")+groupSeparator+QLatin1String("\""); + QTest::newRow(qUtf8Printable(tag)) << QLatin1String("a")+groupSeparator << false; } QTest::newRow("First character can't be \"+\"") << "+a" << false;