Optimization. Use FindFirstNotOf() that supports QString natively.

--HG--
branch : develop
This commit is contained in:
Roman Telezhynskyi 2018-04-22 18:32:38 +03:00
parent 279674f491
commit e845453f95
4 changed files with 35 additions and 27 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}

View File

@ -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<int>(m_strFormulaStd.find_first_not_of ( a_szCharSetStd, static_cast<std::size_t>(a_iPos) ));
if ( iEnd == static_cast<int>(string_type::npos) )
if (iEnd == -1)
{
iEnd = static_cast<int>(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<int>( m_strFormulaStd.find_first_not_of ( oprtCharsStd, static_cast<std::size_t>(a_iPos) ) );
if ( iEnd == static_cast<int>( string_type::npos ) )
if ( iEnd == -1 )
{
iEnd = static_cast<int>( 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