From e845453f957e6fa4cb0bc5e4194e123c6fbd040f Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sun, 22 Apr 2018 18:32:38 +0300 Subject: [PATCH] Optimization. Use FindFirstNotOf() that supports QString natively. --HG-- branch : develop --- src/libs/qmuparser/qmudef.cpp | 19 +++++++++++++++ src/libs/qmuparser/qmudef.h | 3 +++ src/libs/qmuparser/qmuparserbase.cpp | 14 ++++------- src/libs/qmuparser/qmuparsertokenreader.cpp | 26 +++++++-------------- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/libs/qmuparser/qmudef.cpp b/src/libs/qmuparser/qmudef.cpp index 0233e1d65..db4a51da0 100644 --- a/src/libs/qmuparser/qmudef.cpp +++ b/src/libs/qmuparser/qmudef.cpp @@ -328,3 +328,22 @@ QString NameRegExp() return regex; } + +//--------------------------------------------------------------------------------------------------------------------- +int FindFirstNotOf(const QString &string, const QString &chars, int pos) +{ + int chPos = pos; + QString::const_iterator it = string.constBegin() + pos; + QString::const_iterator end = string.constEnd(); + while (it != end) + { + if (not chars.contains(*it)) + { + return chPos; + } + ++it; + ++chPos; + } + + return -1; +} diff --git a/src/libs/qmuparser/qmudef.h b/src/libs/qmuparser/qmudef.h index c0bd99f39..cbe2ba56c 100644 --- a/src/libs/qmuparser/qmudef.h +++ b/src/libs/qmuparser/qmudef.h @@ -91,6 +91,7 @@ QT_WARNING_DISABLE_GCC("-Wattributes") class QLocale; class QChar; +class QString; #define INIT_LOCALE_VARIABLES(locale) \ const QChar positiveSign = (locale).positiveSign(); \ @@ -134,4 +135,6 @@ static inline bool QmuFuzzyComparePossibleNulls(double p1, double p2) QMUPARSERSHARED_EXPORT int ReadVal(const QString &formula, qreal &val, const QLocale &locale, const QChar &decimal, const QChar &thousand); +int FindFirstNotOf(const QString &string, const QString &chars, int pos = 0); + #endif // QMUDEF_H diff --git a/src/libs/qmuparser/qmuparserbase.cpp b/src/libs/qmuparser/qmuparserbase.cpp index bc69f66a0..d5b61bf1c 100644 --- a/src/libs/qmuparser/qmuparserbase.cpp +++ b/src/libs/qmuparser/qmuparserbase.cpp @@ -381,11 +381,8 @@ void QmuParserBase::AddCallback(const QString &a_strName, const QmuParserCallbac void QmuParserBase::CheckOprt(const QString &a_sName, const QmuParserCallback &a_Callback, const QString &a_szCharSet) const { - const std::wstring a_sNameStd = a_sName.toStdWString(); - const std::wstring a_szCharSetStd = a_szCharSet.toStdWString(); - - if ( a_sNameStd.length() == false || (a_sNameStd.find_first_not_of(a_szCharSetStd)!=string_type::npos) || - (a_sNameStd.at(0)>='0' && a_sNameStd.at(0)<='9')) + if ( a_sName.isEmpty() || (FindFirstNotOf(a_sName, a_szCharSet) != -1) || + (a_sName.at(0)>='0' && a_sName.at(0)<='9')) { switch (a_Callback.GetCode()) { @@ -410,11 +407,8 @@ void QmuParserBase::CheckOprt(const QString &a_sName, const QmuParserCallback &a */ void QmuParserBase::CheckName(const QString &a_sName, const QString &a_szCharSet) const { - std::wstring a_sNameStd = a_sName.toStdWString(); - std::wstring a_szCharSetStd = a_szCharSet.toStdWString(); - - if ( a_sNameStd.length() == false || (a_sNameStd.find_first_not_of(a_szCharSetStd)!=string_type::npos) || - (a_sNameStd[0]>='0' && a_sNameStd[0]<='9')) + if ( a_sName.isEmpty() || (FindFirstNotOf(a_sName, a_szCharSet) != -1) || + (a_sName.at(0)>='0' && a_sName.at(0)<='9')) { Error(ecINVALID_NAME); } diff --git a/src/libs/qmuparser/qmuparsertokenreader.cpp b/src/libs/qmuparser/qmuparsertokenreader.cpp index 74e9db1bf..e7c6ffef3 100644 --- a/src/libs/qmuparser/qmuparsertokenreader.cpp +++ b/src/libs/qmuparser/qmuparsertokenreader.cpp @@ -325,21 +325,17 @@ QT_WARNING_PUSH QT_WARNING_DISABLE_MSVC(4309) int QmuParserTokenReader::ExtractToken ( const QString &a_szCharSet, QString &a_sTok, int a_iPos ) const { - const std::wstring m_strFormulaStd = m_strFormula.toStdWString(); - const std::wstring a_szCharSetStd = a_szCharSet.toStdWString(); + int iEnd = FindFirstNotOf(m_strFormula, a_szCharSet, a_iPos); - int iEnd = static_cast(m_strFormulaStd.find_first_not_of ( a_szCharSetStd, static_cast(a_iPos) )); - - if ( iEnd == static_cast(string_type::npos) ) + if (iEnd == -1) { - iEnd = static_cast(m_strFormulaStd.length()); + iEnd = m_strFormula.length(); } // Assign token string if there was something found - if ( a_iPos != iEnd ) + if (a_iPos != iEnd) { - a_sTok = QString().fromStdWString ( std::wstring ( m_strFormulaStd.begin() + a_iPos, - m_strFormulaStd.begin() + iEnd ) ); + a_sTok = m_strFormula.mid(a_iPos, iEnd - a_iPos); } return iEnd; @@ -355,21 +351,17 @@ int QmuParserTokenReader::ExtractToken ( const QString &a_szCharSet, QString &a_ */ int QmuParserTokenReader::ExtractOperatorToken ( QString &a_sTok, int a_iPos ) const { - const std::wstring m_strFormulaStd = m_strFormula.toStdWString(); - // Changed as per Issue 6: https://code.google.com/p/muparser/issues/detail?id=6 - const std::wstring oprtCharsStd = m_pParser->ValidOprtChars().toStdWString(); + int iEnd = FindFirstNotOf(m_strFormula, m_pParser->ValidOprtChars(), a_iPos); - int iEnd = static_cast( m_strFormulaStd.find_first_not_of ( oprtCharsStd, static_cast(a_iPos) ) ); - if ( iEnd == static_cast( string_type::npos ) ) + if ( iEnd == -1 ) { - iEnd = static_cast( m_strFormulaStd.length() ); + iEnd = m_strFormula.length(); } // Assign token string if there was something found if ( a_iPos != iEnd ) { - a_sTok = QString().fromStdWString ( string_type ( m_strFormulaStd.begin() + a_iPos, - m_strFormulaStd.begin() + iEnd ) ); + a_sTok = m_strFormula.mid(a_iPos, iEnd - a_iPos); return iEnd; } else