From 5b59bdcb6042f8ed83a268463923165379af966e Mon Sep 17 00:00:00 2001 From: pi1024e <49824824+pi1024e@users.noreply.github.com> Date: Tue, 16 Jul 2019 15:53:34 -0400 Subject: [PATCH] Removed need for trimIdx and fDigitsFound, resulting in less code. (#576) Having an extra integer variable to hold onto an i or i-1 value is inefficient, for "if (m_token.at(i) != chZero)", the code under "fDigitsFound" will always run, so it makes sense to put the fDigitsFound code under the if statement, which can return from the function entirely instead of breaking. ### How changes were validated: - Manual Testing verified the code having identical behavior as before, with no side effects. --- src/CalcManager/ExpressionCommand.cpp | 31 +++++++++++---------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/CalcManager/ExpressionCommand.cpp b/src/CalcManager/ExpressionCommand.cpp index 079e02a..2d0547c 100644 --- a/src/CalcManager/ExpressionCommand.cpp +++ b/src/CalcManager/ExpressionCommand.cpp @@ -137,6 +137,7 @@ void COpndCommand::AppendCommand(int command) { m_commands->Append(command); } + if (command == IDC_PNT) { m_fDecimal = true; @@ -256,38 +257,30 @@ const wstring& COpndCommand::GetToken(wchar_t decimalSymbol) } // Remove zeros - bool fDigitsFound = false; - int trimIdx = 0; for (unsigned int i = 0; i < m_token.size(); i++) { if (m_token.at(i) != chZero) { if (m_token.at(i) == decimalSymbol) { - trimIdx = i - 1; + m_token.erase(0, i - 1); } else { - trimIdx = i; + m_token.erase(0, i); } - fDigitsFound = true; - break; + + if (m_fNegative) + { + m_token.insert(0, &chNegate); + } + + return m_token; } } - if (fDigitsFound) - { - m_token.erase(0, trimIdx); - if (m_fNegative) - { - m_token.insert(0, &chNegate); - } - } - else - { - m_token.clear(); - m_token.append(&chZero); - } + m_token.clear(); + m_token.append(&chZero); return m_token; }