Fix variable name regular expression.

Forbid characters "." and ",".

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2017-07-26 19:28:26 +03:00
parent 842d21016e
commit f9f241ac25
5 changed files with 89 additions and 27 deletions

View File

@ -647,7 +647,7 @@
</xs:element> </xs:element>
<xs:simpleType name="shortName"> <xs:simpleType name="shortName">
<xs:restriction base="xs:string"> <xs:restriction base="xs:string">
<xs:pattern value="^([^\p{Nd}\p{Zs}*/&amp;|!&lt;&gt;^\-()+=?:;'\&quot;]){1,1}([^\p{Zs}*/&amp;|!&lt;&gt;^\-()+=?:;\&quot;]){0,}$"/> <xs:pattern value="^([^\p{Nd}\p{Zs}*/&amp;|!&lt;&gt;^\()\-+.,٫, ٬.'=?:;'\&quot;]){1,1}([^\p{Zs}*/&amp;|!&lt;&gt;^\()\-+.,٫, ٬.'=?:;\&quot;]){0,}$"/>
</xs:restriction> </xs:restriction>
</xs:simpleType> </xs:simpleType>
<xs:simpleType name="units"> <xs:simpleType name="units">

View File

@ -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) static int CheckChar(QChar &c, const QLocale &locale, const QChar &decimal, const QChar &thousand)
{ {
INIT_LOCALE_VARIABLES(locale); INIT_LOCALE_VARIABLES(locale);
Q_UNUSED(decimalPoint)
Q_UNUSED(groupSeparator)
if (c == positiveSign) if (c == positiveSign)
{ {
@ -175,6 +177,8 @@ int ReadVal(const QString &formula, qreal &val, const QLocale &locale, const QCh
} }
INIT_LOCALE_VARIABLES(locale); INIT_LOCALE_VARIABLES(locale);
Q_UNUSED(decimalPoint)
Q_UNUSED(groupSeparator)
QSet<QChar> reserved; QSet<QChar> reserved;
reserved << positiveSign reserved << positiveSign
@ -272,3 +276,54 @@ int ReadVal(const QString &formula, qreal &val, const QLocale &locale, const QCh
return -1; return -1;
} }
//---------------------------------------------------------------------------------------------------------------------
QString NameRegExp()
{
static QString regex;
if (regex.isEmpty())
{
const QList<QLocale> 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;
}

View File

@ -24,8 +24,6 @@
#include "qmuparser_global.h" #include "qmuparser_global.h"
#include <QString>
#if QT_VERSION < QT_VERSION_CHECK(5, 5, 0) #if QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
/* /*
@ -92,32 +90,27 @@ QT_WARNING_DISABLE_GCC("-Wattributes")
#endif /* Q_CC_MSVC */ #endif /* Q_CC_MSVC */
class QLocale; class QLocale;
class QChar;
#define INIT_LOCALE_VARIABLES(locale) \ #define INIT_LOCALE_VARIABLES(locale) \
const QChar positiveSign = (locale).positiveSign(); \ const QChar positiveSign = (locale).positiveSign(); \
const QChar negativeSign = (locale).negativeSign(); \ const QChar negativeSign = (locale).negativeSign(); \
const QChar sign0 = (locale).toString(0).at(0); \ const QChar sign0 = (locale).toString(0).at(0); \
const QChar sign1 = (locale).toString(1).at(0); \ const QChar sign1 = (locale).toString(1).at(0); \
const QChar sign2 = (locale).toString(2).at(0); \ const QChar sign2 = (locale).toString(2).at(0); \
const QChar sign3 = (locale).toString(3).at(0); \ const QChar sign3 = (locale).toString(3).at(0); \
const QChar sign4 = (locale).toString(4).at(0); \ const QChar sign4 = (locale).toString(4).at(0); \
const QChar sign5 = (locale).toString(5).at(0); \ const QChar sign5 = (locale).toString(5).at(0); \
const QChar sign6 = (locale).toString(6).at(0); \ const QChar sign6 = (locale).toString(6).at(0); \
const QChar sign7 = (locale).toString(7).at(0); \ const QChar sign7 = (locale).toString(7).at(0); \
const QChar sign8 = (locale).toString(8).at(0); \ const QChar sign8 = (locale).toString(8).at(0); \
const QChar sign9 = (locale).toString(9).at(0); \ const QChar sign9 = (locale).toString(9).at(0); \
const QChar expUpper = (locale).exponential().toUpper(); \ const QChar expUpper = (locale).exponential().toUpper(); \
const QChar expLower = (locale).exponential().toLower() \ const QChar expLower = (locale).exponential().toLower(); \
const QChar decimalPoint = (locale).decimalPoint(); \
const QChar groupSeparator = (locale).groupSeparator() \
//--------------------------------------------------------------------------------------------------------------------- QString NameRegExp();
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,}$");
}
QT_WARNING_POP QT_WARNING_POP

View File

@ -71,6 +71,8 @@ void QmuFormulaBase::InitCharSets()
INIT_LOCALE_VARIABLES(m_locale); INIT_LOCALE_VARIABLES(m_locale);
Q_UNUSED(expUpper) Q_UNUSED(expUpper)
Q_UNUSED(expLower) Q_UNUSED(expLower)
Q_UNUSED(decimalPoint)
Q_UNUSED(groupSeparator)
// Defining identifier character sets // Defining identifier character sets
const QString nameChars = QString() + sign0 + sign1 + sign2 + sign3 + sign4 + sign5 + sign6 + sign7 + sign8 + const QString nameChars = QString() + sign0 + sign1 + sign2 + sign3 + sign4 + sign5 + sign6 + sign7 + sign8 +

View File

@ -90,8 +90,20 @@ void TST_NameRegExp::TestNameRegExp_data()
tag = localeName+QLatin1String(". First character can't be \"")+negativeSign+QLatin1String("\""); tag = localeName+QLatin1String(". First character can't be \"")+negativeSign+QLatin1String("\"");
QTest::newRow(qUtf8Printable(tag)) << negativeSign+QLatin1String("a") << false; 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("\""); tag = localeName+QLatin1String(". Any next character can't be \"")+negativeSign+QLatin1String("\"");
QTest::newRow(qUtf8Printable(tag)) << QLatin1String("a")+negativeSign << false; 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; QTest::newRow("First character can't be \"+\"") << "+a" << false;