Skip to content

Commit

Permalink
Code cleanup: Use number formatting utilities from the standard libra…
Browse files Browse the repository at this point in the history
…ry (microsoft#1743)

* Use general format in Graphing Calculator share

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

* cleanup
  • Loading branch information
mcooley authored May 30, 2022
1 parent d25cde5 commit 8fab2cb
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 94 deletions.
1 change: 0 additions & 1 deletion src/CalcManager/CEngine/scicomm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/CalcManager/NumberFormattingUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/CalcManager/NumberFormattingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/CalcManager/UnitConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions src/CalcViewModel/Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/CalcViewModel/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 13 additions & 14 deletions src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
76 changes: 24 additions & 52 deletions src/CalcViewModel/GraphingCalculator/GraphingSettingsViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions src/CalculatorUnitTests/CalculatorManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit 8fab2cb

Please sign in to comment.