From 732b32c8b4eb745f585b5df53b02fce2586d8972 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 18 Dec 2017 10:33:59 -0800 Subject: [PATCH] [muparser] Clean up constructors and other miscellaneous --- muparser-2.2.5/include/muParserBase.h | 19 +++++--------- muparser-2.2.5/include/muParserBytecode.h | 5 +--- muparser-2.2.5/src/muParserBase.cpp | 32 +---------------------- muparser-2.2.5/src/muParserBytecode.cpp | 17 +++++++----- 4 files changed, 18 insertions(+), 55 deletions(-) diff --git a/muparser-2.2.5/include/muParserBase.h b/muparser-2.2.5/include/muParserBase.h index 6aaa1863f..b65d92a83 100644 --- a/muparser-2.2.5/include/muParserBase.h +++ b/muparser-2.2.5/include/muParserBase.h @@ -67,15 +67,9 @@ class ParserBase { */ typedef ValueOrError (ParserBase::*ParseFunction)() const; - /** \brief Type used for storing an array of values. */ - typedef std::vector valbuf_type; - /** \brief Type for a vector of strings. */ typedef std::vector stringbuf_type; - /** \brief Typedef for the token reader. */ - typedef ParserTokenReader token_reader_type; - /** \brief Type used for parser tokens. */ typedef ParserToken token_type; @@ -199,7 +193,6 @@ class ParserBase { }; private: - void InitTokenReader(); void ReInit() const; OptionalError AddCallback(const string_type &a_strName, const ParserCallback &a_Callback, @@ -247,8 +240,7 @@ class ParserBase { m_vStringBuf; ///< String buffer, used for storing string function arguments stringbuf_type m_vStringVarBuf; - std::auto_ptr - m_pTokenReader; ///< Managed pointer to the token reader object. + std::unique_ptr m_pTokenReader; funmap_type m_FunDef; ///< Map of function names and pointers. funmap_type m_PostOprtDef; ///< Postfix operator callbacks @@ -258,16 +250,17 @@ class ParserBase { strmap_type m_StrVarDef; ///< user defined string constants varmap_type m_VarDef; ///< user defind variables. - bool m_bBuiltInOp; ///< Flag that can be used for switching built in operators on and off + bool m_bBuiltInOp = + true; ///< Flag that can be used for switching built in operators on and off string_type m_sNameChars; ///< Charset for names string_type m_sOprtChars; ///< Charset for postfix/ binary operator tokens string_type m_sInfixOprtChars; ///< Charset for infix operator tokens // items merely used for caching state information - mutable valbuf_type - m_vStackBuffer; ///< This is merely a buffer used for the stack in the cmd parsing routine - mutable int m_nFinalResultIdx; + /// This is merely a buffer used for the stack in the cmd parsing routine + mutable std::vector m_vStackBuffer; + mutable int m_nFinalResultIdx = 0; }; } // namespace mu diff --git a/muparser-2.2.5/include/muParserBytecode.h b/muparser-2.2.5/include/muParserBytecode.h index 9d4ffc2c3..951282646 100644 --- a/muparser-2.2.5/include/muParserBytecode.h +++ b/muparser-2.2.5/include/muParserBytecode.h @@ -83,9 +83,6 @@ class ParserByteCode { /** \brief Token type for internal use only. */ typedef ParserToken token_type; - /** \brief Token vector for storing the RPN. */ - typedef std::vector rpn_type; - /** \brief Position in the Calculation array. */ unsigned m_iStackPos; @@ -93,7 +90,7 @@ class ParserByteCode { std::size_t m_iMaxStackSize; /** \brief The actual rpn storage. */ - rpn_type m_vRPN; + std::vector m_vRPN; public: ParserByteCode(); diff --git a/muparser-2.2.5/src/muParserBase.cpp b/muparser-2.2.5/src/muParserBase.cpp index 32f909bf3..dbdbc8963 100644 --- a/muparser-2.2.5/src/muParserBase.cpp +++ b/muparser-2.2.5/src/muParserBase.cpp @@ -64,25 +64,7 @@ const char_type *ParserBase::c_DefaultOprt[] = { \param a_szFormula the formula to interpret. \throw ParserException if a_szFormula is null. */ -ParserBase::ParserBase() - : m_vRPN(), - m_vStringBuf(), - m_pTokenReader(), - m_FunDef(), - m_PostOprtDef(), - m_InfixOprtDef(), - m_OprtDef(), - m_ConstDef(), - m_StrVarDef(), - m_VarDef(), - m_bBuiltInOp(true), - m_sNameChars(), - m_sOprtChars(), - m_sInfixOprtChars(), - m_vStackBuffer(), - m_nFinalResultIdx(0) { - InitTokenReader(); -} +ParserBase::ParserBase() : m_pTokenReader(new ParserTokenReader(this)) {} //--------------------------------------------------------------------------- ParserBase::~ParserBase() = default; @@ -124,16 +106,6 @@ void ParserBase::ResetLocale() { SetArgSep(','); } -//--------------------------------------------------------------------------- -/** \brief Initialize the token reader. - - Create new token reader object and submit pointers to function, operator, - constant and variable definitions. - - \post m_pTokenReader.get()!=0 -*/ -void ParserBase::InitTokenReader() { m_pTokenReader.reset(new token_reader_type(this)); } - //--------------------------------------------------------------------------- /** \brief Reset parser to string parsing mode and clear internal buffers. @@ -539,7 +511,6 @@ OptionalError ParserBase::ApplyStrFunc(const token_type &a_FunTok, return Error(ecVAL_EXPECTED, m_pTokenReader->GetPos(), a_FunTok.GetAsString()); } - // string functions won't be optimized m_vRPN.AddStrFun(pFunc, a_FunTok.GetArgCount(), a_vArg.back().GetIdx()); return {}; } @@ -766,7 +737,6 @@ ValueOrError ParserBase::InvokeFunction(generic_fun_type func, const value_type ValueOrError ParserBase::ExecuteRPN() const { assert(! m_vRPN.empty() && "Missing RPN"); value_type *Stack = &m_vStackBuffer[0]; - value_type buf; int sidx(0); for (const SToken *pTok = m_vRPN.GetBase(); pTok->Cmd != cmEND; ++pTok) { switch (pTok->Cmd) { diff --git a/muparser-2.2.5/src/muParserBytecode.cpp b/muparser-2.2.5/src/muParserBytecode.cpp index e0f3bd896..48883469f 100644 --- a/muparser-2.2.5/src/muParserBytecode.cpp +++ b/muparser-2.2.5/src/muParserBytecode.cpp @@ -179,27 +179,30 @@ void ParserByteCode::Finalize() { SToken tok; tok.Cmd = cmEND; m_vRPN.push_back(tok); - rpn_type(m_vRPN).swap(m_vRPN); // shrink bytecode vector to fit + m_vRPN.shrink_to_fit(); // Determine the if-then-else jump offsets ParserStack stIf, stElse; - int idx; for (int i = 0; i < (int)m_vRPN.size(); ++i) { switch (m_vRPN[i].Cmd) { case cmIF: stIf.push_back(i); break; - case cmELSE: + case cmELSE: { + int idx = stIf.back(); + stIf.pop_back(); + m_vRPN[idx].Oprt.offset = i - idx; stElse.push_back(i); - idx = stIf.pop(); - m_vRPN[idx].Oprt.offset = i - idx; break; + } - case cmENDIF: - idx = stElse.pop(); + case cmENDIF: { + int idx = stElse.back(); + stElse.pop_back(); m_vRPN[idx].Oprt.offset = i - idx; break; + } default: break;