From e68510cc49173a4e3627089f6c5201a2dc22fccb Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 20 Jan 2018 15:36:50 +0200 Subject: [PATCH] Speed optimization for method QmuTokenParser::IsSingle(). --HG-- branch : develop --- src/libs/qmuparser/qmutokenparser.cpp | 42 ++----------------- src/test/ValentinaTest/tst_qmutokenparser.cpp | 3 ++ 2 files changed, 7 insertions(+), 38 deletions(-) diff --git a/src/libs/qmuparser/qmutokenparser.cpp b/src/libs/qmuparser/qmutokenparser.cpp index f32833e06..df538db5e 100644 --- a/src/libs/qmuparser/qmutokenparser.cpp +++ b/src/libs/qmuparser/qmutokenparser.cpp @@ -96,44 +96,10 @@ QmuTokenParser::QmuTokenParser(const QString &formula, bool osSeparator, */ bool QmuTokenParser::IsSingle(const QString &formula) { - if (formula.isEmpty()) - { - return false;// if don't know say no - } - - QScopedPointer 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 tokens = cal->GetTokens();// Tokens (variables, measurements) - const QMap 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; - } + QLocale c(QLocale::C); + bool ok = false; + c.toDouble(formula, &ok); + return ok; } }// namespace qmu diff --git a/src/test/ValentinaTest/tst_qmutokenparser.cpp b/src/test/ValentinaTest/tst_qmutokenparser.cpp index 9f4758ff3..4002050a7 100644 --- a/src/test/ValentinaTest/tst_qmutokenparser.cpp +++ b/src/test/ValentinaTest/tst_qmutokenparser.cpp @@ -45,8 +45,10 @@ void TST_QmuTokenParser::IsSingle_data() QTest::addColumn("formula"); QTest::addColumn("result"); + QTest::newRow("Empty string") << "" << false; QTest::newRow("Single value") << "15.5" << true; QTest::newRow("Two digits") << "2+2" << false; + QTest::newRow("Two digits") << "2-2" << false; QTest::newRow("Negative single value") << "-2" << true; QTest::newRow("Digit and variable") << "2+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("Several spaces") << " " << 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("Correct C locale 15500") << "15500" << true; QTest::newRow("Correct C locale 15,500") << "15,500" << true;