Speed optimization for method QmuTokenParser::IsSingle().
--HG-- branch : develop
This commit is contained in:
parent
d03cd7be06
commit
e68510cc49
|
@ -96,44 +96,10 @@ QmuTokenParser::QmuTokenParser(const QString &formula, bool osSeparator,
|
||||||
*/
|
*/
|
||||||
bool QmuTokenParser::IsSingle(const QString &formula)
|
bool QmuTokenParser::IsSingle(const QString &formula)
|
||||||
{
|
{
|
||||||
if (formula.isEmpty())
|
QLocale c(QLocale::C);
|
||||||
{
|
bool ok = false;
|
||||||
return false;// if don't know say no
|
c.toDouble(formula, &ok);
|
||||||
}
|
return ok;
|
||||||
|
|
||||||
QScopedPointer<QmuTokenParser> cal(new QmuTokenParser());
|
|
||||||
|
|
||||||
// Parser doesn't know any variable on this stage. So, we just use variable factory that for each unknown
|
|
||||||
// variable set value to 0.
|
|
||||||
cal->SetVarFactory(AddVariable, cal.data());
|
|
||||||
cal->SetSepForEval();//Reset separators options
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
cal->SetExpr(formula);
|
|
||||||
cal->Eval();// We don't need save result, only parse formula
|
|
||||||
}
|
|
||||||
catch (const qmu::QmuParserError &e)
|
|
||||||
{
|
|
||||||
Q_UNUSED(e)
|
|
||||||
return false;// something wrong with formula, say no
|
|
||||||
}
|
|
||||||
|
|
||||||
QMap<int, QString> tokens = cal->GetTokens();// Tokens (variables, measurements)
|
|
||||||
const QMap<int, QString> numbers = cal->GetNumbers();// All numbers in expression
|
|
||||||
delete cal.take();
|
|
||||||
|
|
||||||
// Remove "-" from tokens list if exist. If don't do that unary minus operation will broken.
|
|
||||||
RemoveAll(tokens, QStringLiteral("-"));
|
|
||||||
|
|
||||||
if (tokens.isEmpty() && numbers.size() == 1)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}// namespace qmu
|
}// namespace qmu
|
||||||
|
|
|
@ -45,8 +45,10 @@ void TST_QmuTokenParser::IsSingle_data()
|
||||||
QTest::addColumn<QString>("formula");
|
QTest::addColumn<QString>("formula");
|
||||||
QTest::addColumn<bool>("result");
|
QTest::addColumn<bool>("result");
|
||||||
|
|
||||||
|
QTest::newRow("Empty string") << "" << false;
|
||||||
QTest::newRow("Single value") << "15.5" << true;
|
QTest::newRow("Single value") << "15.5" << true;
|
||||||
QTest::newRow("Two digits") << "2+2" << false;
|
QTest::newRow("Two digits") << "2+2" << false;
|
||||||
|
QTest::newRow("Two digits") << "2-2" << false;
|
||||||
QTest::newRow("Negative single value") << "-2" << true;
|
QTest::newRow("Negative single value") << "-2" << true;
|
||||||
QTest::newRow("Digit and variable") << "2+a" << false;
|
QTest::newRow("Digit and variable") << "2+a" << false;
|
||||||
QTest::newRow("One variable twice") << "a+a" << false;
|
QTest::newRow("One variable twice") << "a+a" << false;
|
||||||
|
@ -54,6 +56,7 @@ void TST_QmuTokenParser::IsSingle_data()
|
||||||
QTest::newRow("Empty string") << "" << false;
|
QTest::newRow("Empty string") << "" << false;
|
||||||
QTest::newRow("Several spaces") << " " << false;
|
QTest::newRow("Several spaces") << " " << false;
|
||||||
QTest::newRow("Invalid formula") << "2*)))" << false;
|
QTest::newRow("Invalid formula") << "2*)))" << false;
|
||||||
|
QTest::newRow("Invalid formula") << "2*" << false;
|
||||||
QTest::newRow("Incorrect thousand separator 15 500") << "15 500" << false;
|
QTest::newRow("Incorrect thousand separator 15 500") << "15 500" << false;
|
||||||
QTest::newRow("Correct C locale 15500") << "15500" << true;
|
QTest::newRow("Correct C locale 15500") << "15500" << true;
|
||||||
QTest::newRow("Correct C locale 15,500") << "15,500" << true;
|
QTest::newRow("Correct C locale 15,500") << "15,500" << true;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user