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 "Header Files/CalcEngine.h"
#include "Header Files/CalcUtils.h"
#include "NumberFormattingUtils.h"
using namespace std;
using namespace CalcEngine;

View File

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

View File

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

View File

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

View File

@ -13,7 +13,6 @@
#include "Common/AppResourceProvider.h"
#include "Common/ExpressionCommandSerializer.h"
#include "Common/ExpressionCommandDeserializer.h"
#include "CalcManager/NumberFormattingUtils.h"
using namespace CalculatorApp;
using namespace CalculatorApp::ViewModel::Common;
@ -239,13 +238,6 @@ String^ CalculatorApp::ViewModel::Common::Utilities::EscapeHtmlSpecialCharacters
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)
{
return Utils::AreColorsEqual(color1, color2);

View File

@ -713,7 +713,6 @@ namespace CalculatorApp
{
public:
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 Windows::UI::Xaml::Media::SolidColorBrush ^ GetContrastColor(Windows::UI::Color backgroundColor);
static int GetWindowId();

View File

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

View File

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

View File

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

View File

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