From 582e10faed005c3123528ddcff947ed616721ac1 Mon Sep 17 00:00:00 2001 From: Scott Freeman Date: Mon, 18 Nov 2019 22:02:45 -0500 Subject: [PATCH] Updating RemoveUnwantedCharsFromString to be a template (#808) --- src/CalcViewModel/Common/CopyPasteManager.cpp | 8 ++++---- src/CalcViewModel/Common/Utils.cpp | 14 ++------------ src/CalcViewModel/Common/Utils.h | 13 ++++++++++++- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/CalcViewModel/Common/CopyPasteManager.cpp b/src/CalcViewModel/Common/CopyPasteManager.cpp index 815f3a8..ade3a3f 100644 --- a/src/CalcViewModel/Common/CopyPasteManager.cpp +++ b/src/CalcViewModel/Common/CopyPasteManager.cpp @@ -423,9 +423,9 @@ CopyPasteManager::GetMaxOperandLengthAndValue(ViewMode mode, CategoryGroupType m Platform::String ^ CopyPasteManager::SanitizeOperand(Platform::String ^ operand) { - wchar_t unWantedChars[] = { L'\'', L'_', L'`', L'(', L')', L'-', L'+' }; + constexpr wchar_t unWantedChars[] = { L'\'', L'_', L'`', L'(', L')', L'-', L'+' }; - return ref new String(Utils::RemoveUnwantedCharsFromString(operand->Data(), unWantedChars, static_cast(size(unWantedChars))).c_str()); + return ref new String(Utils::RemoveUnwantedCharsFromString(operand->Data(), unWantedChars).c_str()); } IBox ^ CopyPasteManager::TryOperandToULL(String ^ operand, NumberBase numberBase) @@ -596,8 +596,8 @@ ULONG32 CopyPasteManager::ProgrammerOperandLength(Platform::String ^ operand, Nu // euro(€) - 8364 Platform::String ^ CopyPasteManager::RemoveUnwantedCharsFromString(Platform::String ^ input) { - wchar_t unWantedChars[] = { L' ', L',', L'"', 165, 164, 8373, 36, 8353, 8361, 8362, 8358, 8377, 163, 8364, 8234, 8235, 8236, 8237 }; - return ref new String(Utils::RemoveUnwantedCharsFromString(input->Data(), unWantedChars, 18).c_str()); + constexpr wchar_t unWantedChars[] = { L' ', L',', L'"', 165, 164, 8373, 36, 8353, 8361, 8362, 8358, 8377, 163, 8364, 8234, 8235, 8236, 8237 }; + return ref new String(Utils::RemoveUnwantedCharsFromString(input->Data(), unWantedChars).c_str()); } bool CopyPasteManager::IsErrorMessage(Platform::String ^ message) diff --git a/src/CalcViewModel/Common/Utils.cpp b/src/CalcViewModel/Common/Utils.cpp index 0fc2e66..368aa47 100644 --- a/src/CalcViewModel/Common/Utils.cpp +++ b/src/CalcViewModel/Common/Utils.cpp @@ -47,8 +47,8 @@ String ^ Utils::GetStringValue(String ^ input) double Utils::GetDoubleFromWstring(wstring input) { - wchar_t unWantedChars[] = { L' ', L',', 8234, 8235, 8236, 8237 }; - wstring ws = RemoveUnwantedCharsFromString(input, unWantedChars, 6); + constexpr wchar_t unWantedChars[] = { L' ', L',', 8234, 8235, 8236, 8237 }; + wstring ws = RemoveUnwantedCharsFromString(input, unWantedChars); return stod(ws); } @@ -80,16 +80,6 @@ bool Utils::IsLastCharacterTarget(_In_ wstring const& input, _In_ wchar_t target return !input.empty() && input.back() == target; } -// Return wstring after removing characters specified by unwantedChars array -wstring Utils::RemoveUnwantedCharsFromString(wstring input, wchar_t* unwantedChars, unsigned int size) -{ - for (unsigned int i = 0; i < size; ++i) - { - input.erase(std::remove(input.begin(), input.end(), unwantedChars[i]), input.end()); - } - return input; -} - void Utils::SerializeCommandsAndTokens( _In_ shared_ptr>> const& tokens, _In_ shared_ptr>> const& commands, diff --git a/src/CalcViewModel/Common/Utils.h b/src/CalcViewModel/Common/Utils.h index 84ce20d..2c75238 100644 --- a/src/CalcViewModel/Common/Utils.h +++ b/src/CalcViewModel/Common/Utils.h @@ -379,7 +379,18 @@ namespace Utils void IFTPlatformException(HRESULT hr); Platform::String ^ GetStringValue(Platform::String ^ input); bool IsLastCharacterTarget(std::wstring const& input, wchar_t target); - std::wstring RemoveUnwantedCharsFromString(std::wstring inputString, wchar_t* unwantedChars, unsigned int size); + + // Return wstring after removing characters specified by unwantedChars array + template + std::wstring RemoveUnwantedCharsFromString(std::wstring inputString, const wchar_t (&unwantedChars)[N]) + { + for (const wchar_t unwantedChar : unwantedChars) + { + inputString.erase(std::remove(inputString.begin(), inputString.end(), unwantedChar), inputString.end()); + } + return inputString; + } + double GetDoubleFromWstring(std::wstring input); int GetWindowId(); void RunOnUIThreadNonblocking(std::function&& function, _In_ Windows::UI::Core::CoreDispatcher ^ currentDispatcher);