Code cleanup: Use number formatting utilities from the standard library (#1743)

* Use general format in Graphing Calculator share

* Use stringstream (general format) to format graph settings min/max

* cleanup
This commit is contained in:
Matt Cooley 2022-05-29 23:48:15 -07:00 committed by GitHub
parent d25cde553f
commit 8fab2cb060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 52 additions and 94 deletions

View File

@ -18,7 +18,6 @@
#include <sstream> #include <sstream>
#include "Header Files/CalcEngine.h" #include "Header Files/CalcEngine.h"
#include "Header Files/CalcUtils.h" #include "Header Files/CalcUtils.h"
#include "NumberFormattingUtils.h"
using namespace std; using namespace std;
using namespace CalcEngine; using namespace CalcEngine;

View File

@ -3,7 +3,7 @@
using namespace std; using namespace std;
namespace CalcManager::NumberFormattingUtils namespace UnitConversionManager::NumberFormattingUtils
{ {
/// <summary> /// <summary>
/// Trims out any trailing zeros or decimals in the given input string /// Trims out any trailing zeros or decimals in the given input string

View File

@ -6,7 +6,7 @@
#include <string> #include <string>
#include "sal_cross_platform.h" #include "sal_cross_platform.h"
namespace CalcManager::NumberFormattingUtils namespace UnitConversionManager::NumberFormattingUtils
{ {
void TrimTrailingZeros(_Inout_ std::wstring& input); void TrimTrailingZeros(_Inout_ std::wstring& input);
unsigned int GetNumberDigits(std::wstring value); unsigned int GetNumberDigits(std::wstring value);

View File

@ -11,7 +11,7 @@
using namespace std; using namespace std;
using namespace UnitConversionManager; using namespace UnitConversionManager;
using namespace CalcManager::NumberFormattingUtils; using namespace UnitConversionManager::NumberFormattingUtils;
static constexpr uint32_t EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT = 3U; static constexpr uint32_t EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT = 3U;
static constexpr uint32_t EXPECTEDSERIALIZEDUNITTOKENCOUNT = 6U; static constexpr uint32_t EXPECTEDSERIALIZEDUNITTOKENCOUNT = 6U;

View File

@ -13,7 +13,6 @@
#include "Common/AppResourceProvider.h" #include "Common/AppResourceProvider.h"
#include "Common/ExpressionCommandSerializer.h" #include "Common/ExpressionCommandSerializer.h"
#include "Common/ExpressionCommandDeserializer.h" #include "Common/ExpressionCommandDeserializer.h"
#include "CalcManager/NumberFormattingUtils.h"
using namespace CalculatorApp; using namespace CalculatorApp;
using namespace CalculatorApp::ViewModel::Common; using namespace CalculatorApp::ViewModel::Common;
@ -239,13 +238,6 @@ String^ CalculatorApp::ViewModel::Common::Utilities::EscapeHtmlSpecialCharacters
return replaceCharacters ? replacementString : originalString; return replaceCharacters ? replacementString : originalString;
} }
Platform::String^ CalculatorApp::ViewModel::Common::Utilities::TrimTrailingZeros(Platform::String^ input)
{
std::wstring tmp(input->Data());
CalcManager::NumberFormattingUtils::TrimTrailingZeros(tmp);
return ref new Platform::String(tmp.c_str());
}
bool CalculatorApp::ViewModel::Common::Utilities::AreColorsEqual(Windows::UI::Color color1, Windows::UI::Color color2) bool CalculatorApp::ViewModel::Common::Utilities::AreColorsEqual(Windows::UI::Color color1, Windows::UI::Color color2)
{ {
return Utils::AreColorsEqual(color1, color2); return Utils::AreColorsEqual(color1, color2);

View File

@ -713,7 +713,6 @@ namespace CalculatorApp
{ {
public: public:
static Platform::String ^ EscapeHtmlSpecialCharacters(Platform::String ^ originalString); static Platform::String ^ EscapeHtmlSpecialCharacters(Platform::String ^ originalString);
static Platform::String^ TrimTrailingZeros(Platform::String^ input);
static bool AreColorsEqual(Windows::UI::Color color1, Windows::UI::Color color2); static bool AreColorsEqual(Windows::UI::Color color1, Windows::UI::Color color2);
static Windows::UI::Xaml::Media::SolidColorBrush ^ GetContrastColor(Windows::UI::Color backgroundColor); static Windows::UI::Xaml::Media::SolidColorBrush ^ GetContrastColor(Windows::UI::Color backgroundColor);
static int GetWindowId(); static int GetWindowId();

View File

@ -3,11 +3,9 @@
#include "pch.h" #include "pch.h"
#include "GraphingSettingsViewModel.h" #include "GraphingSettingsViewModel.h"
#include <CalcManager\NumberFormattingUtils.h>
using namespace CalculatorApp::ViewModel; using namespace CalculatorApp::ViewModel;
using namespace CalculatorApp::ViewModel::Common; using namespace CalculatorApp::ViewModel::Common;
using namespace CalcManager::NumberFormattingUtils;
using namespace GraphControl; using namespace GraphControl;
using namespace std; using namespace std;
using namespace Platform; using namespace Platform;
@ -55,21 +53,22 @@ void GraphingSettingsViewModel::InitRanges()
m_XMaxValue = xMax; m_XMaxValue = xMax;
m_YMinValue = yMin; m_YMinValue = yMin;
m_YMaxValue = yMax; m_YMaxValue = yMax;
auto valueStr = to_wstring(m_XMinValue);
TrimTrailingZeros(valueStr);
XMin = ref new String(valueStr.c_str());
valueStr = to_wstring(m_XMaxValue); std::wostringstream xMinStr;
TrimTrailingZeros(valueStr); xMinStr << m_XMinValue;
XMax = ref new String(valueStr.c_str()); XMin = ref new String(xMinStr.str().c_str());
valueStr = to_wstring(m_YMinValue); std::wostringstream xMaxStr;
TrimTrailingZeros(valueStr); xMaxStr << m_XMaxValue;
YMin = ref new String(valueStr.c_str()); XMax = ref new String(xMaxStr.str().c_str());
valueStr = to_wstring(m_YMaxValue); std::wostringstream yMinStr;
TrimTrailingZeros(valueStr); yMinStr << m_YMinValue;
YMax = ref new String(valueStr.c_str()); YMin = ref new String(yMinStr.str().c_str());
std::wostringstream yMaxStr;
yMaxStr << m_YMaxValue;
YMax = ref new String(yMaxStr.str().c_str());
m_dontUpdateDisplayRange = false; m_dontUpdateDisplayRange = false;
} }

View File

@ -51,21 +51,14 @@ namespace CalculatorApp::ViewModel
m_XIsMinLastChanged = true; m_XIsMinLastChanged = true;
if (m_Graph != nullptr) if (m_Graph != nullptr)
{ {
try std::wistringstream input(value->Data());
double number;
if (input >> number && input.eof())
{ {
size_t sz; m_Graph->XAxisMin = m_XMinValue = number;
auto number = std::stod(value->Data(), &sz); XMinError = false;
if (value->Length() == sz)
{
m_Graph->XAxisMin = m_XMinValue = number;
XMinError = false;
}
else
{
XMinError = true;
}
} }
catch (...) else
{ {
XMinError = true; XMinError = true;
} }
@ -92,21 +85,14 @@ namespace CalculatorApp::ViewModel
m_XIsMinLastChanged = false; m_XIsMinLastChanged = false;
if (m_Graph != nullptr) if (m_Graph != nullptr)
{ {
try std::wistringstream input(value->Data());
double number;
if (input >> number && input.eof())
{ {
size_t sz; m_Graph->XAxisMax = m_XMaxValue = number;
auto number = std::stod(value->Data(), &sz); XMaxError = false;
if (value->Length() == sz)
{
m_Graph->XAxisMax = m_XMaxValue = number;
XMaxError = false;
}
else
{
XMaxError = true;
}
} }
catch (...) else
{ {
XMaxError = true; XMaxError = true;
} }
@ -133,21 +119,14 @@ namespace CalculatorApp::ViewModel
m_YIsMinLastChanged = true; m_YIsMinLastChanged = true;
if (m_Graph != nullptr) if (m_Graph != nullptr)
{ {
try std::wistringstream input(value->Data());
double number;
if (input >> number && input.eof())
{ {
size_t sz; m_Graph->YAxisMin = m_YMinValue = number;
auto number = std::stod(value->Data(), &sz); YMinError = false;
if (value->Length() == sz)
{
m_Graph->YAxisMin = m_YMinValue = number;
YMinError = false;
}
else
{
YMinError = true;
}
} }
catch (...) else
{ {
YMinError = true; YMinError = true;
} }
@ -174,21 +153,14 @@ namespace CalculatorApp::ViewModel
m_YIsMinLastChanged = false; m_YIsMinLastChanged = false;
if (m_Graph != nullptr) if (m_Graph != nullptr)
{ {
try std::wistringstream input(value->Data());
double number;
if (input >> number && input.eof())
{ {
size_t sz; m_Graph->YAxisMax = m_YMaxValue = number;
auto number = std::stod(value->Data(), &sz); YMaxError = false;
if (value->Length() == sz)
{
m_Graph->YAxisMax = m_YMaxValue = number;
YMaxError = false;
}
else
{
YMaxError = true;
}
} }
catch (...) else
{ {
YMaxError = true; YMaxError = true;
} }

View File

@ -12,9 +12,7 @@
using CalculatorApp.Controls; using CalculatorApp.Controls;
using CalculatorApp.Utils; using CalculatorApp.Utils;
using CalculatorApp.ViewModel; using CalculatorApp.ViewModel;
//using CalcManager.NumberFormattingUtils;
using GraphControl; using GraphControl;
//using Utils;
using Windows.ApplicationModel.DataTransfer; using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.Resources; using Windows.ApplicationModel.Resources;
using Windows.Foundation; using Windows.Foundation;
@ -467,8 +465,7 @@ private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs
var value = variables[i].Value; var value = variables[i].Value;
rawHtml += name + "="; rawHtml += name + "=";
var formattedValue = value.ToString("R"); var formattedValue = value.ToString();
formattedValue = Utilities.TrimTrailingZeros(formattedValue);
rawHtml += formattedValue; rawHtml += formattedValue;
if (variables.Count - 1 != i) if (variables.Count - 1 != i)

View File

@ -12,7 +12,7 @@
using namespace CalculatorApp; using namespace CalculatorApp;
using namespace CalculatorApp::ViewModel::Common; using namespace CalculatorApp::ViewModel::Common;
using namespace CalculationManager; using namespace CalculationManager;
using namespace CalcManager::NumberFormattingUtils; using namespace UnitConversionManager::NumberFormattingUtils;
using namespace Platform; using namespace Platform;
using namespace std; using namespace std;
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
@ -191,11 +191,11 @@ namespace CalculatorManagerTest
TEST_METHOD(CalculatorManagerTestMaxDigitsReached_LeadingDecimal); TEST_METHOD(CalculatorManagerTestMaxDigitsReached_LeadingDecimal);
TEST_METHOD(CalculatorManagerTestMaxDigitsReached_TrailingDecimal); TEST_METHOD(CalculatorManagerTestMaxDigitsReached_TrailingDecimal);
TEST_METHOD(CalculatorManagerNumberFormattingUtils_TrimTrailingZeros); TEST_METHOD(UnitConversionManagerNumberFormattingUtils_TrimTrailingZeros);
TEST_METHOD(CalculatorManagerNumberFormattingUtils_GetNumberDigits); TEST_METHOD(UnitConversionManagerNumberFormattingUtils_GetNumberDigits);
TEST_METHOD(CalculatorManagerNumberFormattingUtils_GetNumberDigitsWholeNumberPart); TEST_METHOD(UnitConversionManagerNumberFormattingUtils_GetNumberDigitsWholeNumberPart);
TEST_METHOD(CalculatorManagerNumberFormattingUtils_RoundSignificantDigits); TEST_METHOD(UnitConversionManagerNumberFormattingUtils_RoundSignificantDigits);
TEST_METHOD(CalculatorManagerNumberFormattingUtils_ToScientificNumber); TEST_METHOD(UnitConversionManagerNumberFormattingUtils_ToScientificNumber);
TEST_METHOD(CalculatorManagerTestBinaryOperatorReceived); TEST_METHOD(CalculatorManagerTestBinaryOperatorReceived);
TEST_METHOD(CalculatorManagerTestBinaryOperatorReceived_Multiple); TEST_METHOD(CalculatorManagerTestBinaryOperatorReceived_Multiple);
@ -917,7 +917,7 @@ namespace CalculatorManagerTest
TestMaxDigitsReachedScenario(L"123,456,789,101,112.13"); TestMaxDigitsReachedScenario(L"123,456,789,101,112.13");
} }
void CalculatorManagerTest::CalculatorManagerNumberFormattingUtils_TrimTrailingZeros() void CalculatorManagerTest::UnitConversionManagerNumberFormattingUtils_TrimTrailingZeros()
{ {
wstring number = L"2.1032100000000"; wstring number = L"2.1032100000000";
TrimTrailingZeros(number); TrimTrailingZeros(number);
@ -942,7 +942,7 @@ namespace CalculatorManagerTest
VERIFY_ARE_EQUAL(number, L"322423"); VERIFY_ARE_EQUAL(number, L"322423");
} }
void CalculatorManagerTest::CalculatorManagerNumberFormattingUtils_GetNumberDigits() void CalculatorManagerTest::UnitConversionManagerNumberFormattingUtils_GetNumberDigits()
{ {
wstring number = L"2.10321"; wstring number = L"2.10321";
unsigned int digitsCount = GetNumberDigits(number); unsigned int digitsCount = GetNumberDigits(number);
@ -961,7 +961,7 @@ namespace CalculatorManagerTest
VERIFY_ARE_EQUAL(digitsCount, 8); VERIFY_ARE_EQUAL(digitsCount, 8);
} }
void CalculatorManagerTest::CalculatorManagerNumberFormattingUtils_GetNumberDigitsWholeNumberPart() void CalculatorManagerTest::UnitConversionManagerNumberFormattingUtils_GetNumberDigitsWholeNumberPart()
{ {
unsigned int digitsCount = GetNumberDigitsWholeNumberPart(2.10321); unsigned int digitsCount = GetNumberDigitsWholeNumberPart(2.10321);
VERIFY_ARE_EQUAL(digitsCount, 1); VERIFY_ARE_EQUAL(digitsCount, 1);
@ -981,7 +981,7 @@ namespace CalculatorManagerTest
VERIFY_ARE_EQUAL(digitsCount, 1); VERIFY_ARE_EQUAL(digitsCount, 1);
} }
void CalculatorManagerTest::CalculatorManagerNumberFormattingUtils_RoundSignificantDigits() void CalculatorManagerTest::UnitConversionManagerNumberFormattingUtils_RoundSignificantDigits()
{ {
wstring result = RoundSignificantDigits(12.342343242, 3); wstring result = RoundSignificantDigits(12.342343242, 3);
VERIFY_ARE_EQUAL(result, L"12.342"); VERIFY_ARE_EQUAL(result, L"12.342");
@ -997,7 +997,7 @@ namespace CalculatorManagerTest
VERIFY_ARE_EQUAL(result, L"0.3423000"); VERIFY_ARE_EQUAL(result, L"0.3423000");
} }
void CalculatorManagerTest::CalculatorManagerNumberFormattingUtils_ToScientificNumber() void CalculatorManagerTest::UnitConversionManagerNumberFormattingUtils_ToScientificNumber()
{ {
wstring result = ToScientificNumber(3423); wstring result = ToScientificNumber(3423);
VERIFY_ARE_EQUAL(result, L"3.423000e+03"); VERIFY_ARE_EQUAL(result, L"3.423000e+03");