Using wstring instead of wstringstream where appropriate (#881)

This commit is contained in:
Scott Freeman 2019-12-19 04:50:31 -05:00 committed by Rudy Huyn
parent be4e437f4d
commit a21b4a2d1a
6 changed files with 57 additions and 53 deletions

View file

@ -269,34 +269,47 @@ bool CalcInput::IsEmpty()
wstring CalcInput::ToString(uint32_t radix) wstring CalcInput::ToString(uint32_t radix)
{ {
// In theory both the base and exponent could be C_NUM_MAX_DIGITS long. // In theory both the base and exponent could be C_NUM_MAX_DIGITS long.
wstringstream resStream;
if ((m_base.value.size() > MAX_STRLEN) || (m_hasExponent && m_exponent.value.size() > MAX_STRLEN)) if ((m_base.value.size() > MAX_STRLEN) || (m_hasExponent && m_exponent.value.size() > MAX_STRLEN))
{ {
return wstring(); return wstring();
} }
wstring result;
if (m_base.IsNegative()) if (m_base.IsNegative())
{ {
resStream << L'-'; result = L'-';
} }
resStream << (m_base.IsEmpty() ? L"0" : m_base.value); if (m_base.IsEmpty())
{
result += L'0';
}
else
{
result += m_base.value;
}
if (m_hasExponent) if (m_hasExponent)
{ {
// Add a decimal point if it is not already there // Add a decimal point if it is not already there
if (!m_hasDecimal) if (!m_hasDecimal)
{ {
resStream << m_decSymbol; result += m_decSymbol;
} }
resStream << ((radix == 10) ? L'e' : L'^'); result += ((radix == 10) ? L'e' : L'^');
resStream << (m_exponent.IsNegative() ? L'-' : L'+'); result += (m_exponent.IsNegative() ? L'-' : L'+');
resStream << (m_exponent.IsEmpty() ? L"0" : m_exponent.value);
}
auto result = resStream.str(); if (m_exponent.IsEmpty())
{
result += L'0';
}
else
{
result += m_exponent.value;
}
}
// Base and Exp can each be up to C_NUM_MAX_DIGITS in length, plus 4 characters for sign, dec, exp, and expSign. // Base and Exp can each be up to C_NUM_MAX_DIGITS in length, plus 4 characters for sign, dec, exp, and expSign.
if (result.size() > C_NUM_MAX_DIGITS * 2 + 4) if (result.size() > C_NUM_MAX_DIGITS * 2 + 4)

View file

@ -312,7 +312,7 @@ wstring CCalcEngine::GroupDigits(wstring_view delimiter, vector<uint32_t> const&
ritr = displayString.rbegin(); ritr = displayString.rbegin();
} }
wstringstream groupedStream{}; wstring result;
uint32_t groupingSize = 0; uint32_t groupingSize = 0;
auto groupItr = grouping.begin(); auto groupItr = grouping.begin();
@ -323,7 +323,7 @@ wstring CCalcEngine::GroupDigits(wstring_view delimiter, vector<uint32_t> const&
auto reverse_end = displayString.rend() - (isNumNegative ? 1 : 0); auto reverse_end = displayString.rend() - (isNumNegative ? 1 : 0);
while (ritr != reverse_end) while (ritr != reverse_end)
{ {
groupedStream << *ritr++; result += *ritr++;
groupingSize++; groupingSize++;
// If a group is complete, add a separator // If a group is complete, add a separator
@ -332,7 +332,7 @@ wstring CCalcEngine::GroupDigits(wstring_view delimiter, vector<uint32_t> const&
// - we are at the end of the digit string // - we are at the end of the digit string
if (currGrouping != 0 && (groupingSize % currGrouping) == 0 && ritr != reverse_end) if (currGrouping != 0 && (groupingSize % currGrouping) == 0 && ritr != reverse_end)
{ {
groupedStream << wstring{ delimiter }; result += delimiter;
groupingSize = 0; // reset for a new group groupingSize = 0; // reset for a new group
// Shift the grouping to next values if they exist // Shift the grouping to next values if they exist
@ -364,11 +364,10 @@ wstring CCalcEngine::GroupDigits(wstring_view delimiter, vector<uint32_t> const&
// now copy the negative sign if it is there // now copy the negative sign if it is there
if (isNumNegative) if (isNumNegative)
{ {
groupedStream << displayString[0]; result += displayString[0];
} }
auto groupedString = groupedStream.str(); reverse(result.begin(), result.end());
wstring result(groupedString.rbegin(), groupedString.rend());
// Add the right (fractional or exponential) part of the number to the final string. // Add the right (fractional or exponential) part of the number to the final string.
if (hasDecimal) if (hasDecimal)
{ {

View file

@ -1204,78 +1204,73 @@ wstring NumberToString(_Inout_ PNUMBER& pnum, int format, uint32_t radix, int32_
} }
// Begin building the result string // Begin building the result string
wstringstream resultStream{}; wstring result;
// Make sure negative zeros aren't allowed. // Make sure negative zeros aren't allowed.
if ((pnum->sign == -1) && (length > 0)) if ((pnum->sign == -1) && (length > 0))
{ {
resultStream << L'-'; result = L'-';
} }
if (exponent <= 0 && !useSciForm) if (exponent <= 0 && !useSciForm)
{ {
resultStream << L'0'; result += L'0';
resultStream << g_decimalSeparator; result += g_decimalSeparator;
// Used up a digit unaccounted for. // Used up a digit unaccounted for.
} }
while (exponent < 0) while (exponent < 0)
{ {
resultStream << L'0'; result += L'0';
exponent++; exponent++;
} }
while (length > 0) while (length > 0)
{ {
exponent--; exponent--;
resultStream << DIGITS[*pmant--]; result += DIGITS[*pmant--];
length--; length--;
// Be more regular in using a decimal point. // Be more regular in using a decimal point.
if (exponent == 0) if (exponent == 0)
{ {
resultStream << g_decimalSeparator; result += g_decimalSeparator;
} }
} }
while (exponent > 0) while (exponent > 0)
{ {
resultStream << L'0'; result += L'0';
exponent--; exponent--;
// Be more regular in using a decimal point. // Be more regular in using a decimal point.
if (exponent == 0) if (exponent == 0)
{ {
resultStream << g_decimalSeparator; result += g_decimalSeparator;
} }
} }
if (useSciForm) if (useSciForm)
{ {
resultStream << (radix == 10 ? L'e' : L'^'); result += (radix == 10 ? L'e' : L'^');
resultStream << (eout < 0 ? L'-' : L'+'); result += (eout < 0 ? L'-' : L'+');
eout = abs(eout); eout = abs(eout);
wstringstream exponentStream{}; wstring expString{};
do do
{ {
exponentStream << DIGITS[eout % radix]; expString += DIGITS[eout % radix];
eout /= radix; eout /= radix;
} while (eout > 0); } while (eout > 0);
auto expString = exponentStream.str(); result.insert(result.end(), expString.crbegin(), expString.crend());
for (auto ritr = expString.rbegin(); ritr != expString.rend(); ritr++)
{
resultStream << *ritr;
}
} }
// Remove trailing decimal // Remove trailing decimal
auto resultString = resultStream.str(); if (!result.empty() && result.back() == g_decimalSeparator)
if (!resultString.empty() && resultString.back() == g_decimalSeparator)
{ {
resultString.pop_back(); result.pop_back();
} }
return resultString; return result;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View file

@ -599,16 +599,15 @@ String ^ LocalizationService::GetNarratorReadableToken(String ^ rawToken)
String ^ LocalizationService::GetNarratorReadableString(String ^ rawString) String ^ LocalizationService::GetNarratorReadableString(String ^ rawString)
{ {
wstringstream readableString{}; wstring readableString{};
readableString << L"";
wstring asWstring = rawString->Data(); wstring asWstring = rawString->Data();
for (const auto& c : asWstring) for (const auto& c : asWstring)
{ {
readableString << LocalizationService::GetNarratorReadableToken(L"" + c)->Data(); readableString += LocalizationService::GetNarratorReadableToken(ref new String(&c, 1))->Data();
} }
return ref new String(readableString.str().c_str()); return ref new String(readableString.c_str());
} }
void LocalizationService::Sort(std::vector<Platform::String ^>& source) void LocalizationService::Sort(std::vector<Platform::String ^>& source)

View file

@ -31,13 +31,12 @@ String
_In_ String ^ fallbackExpression) _In_ String ^ fallbackExpression)
{ {
// updating accessibility names for expression and result // updating accessibility names for expression and result
wstringstream accExpression{}; wstring accExpression{};
accExpression << L"";
for (const auto& tokenItem : *spTokens) for (const auto& tokenItem : *spTokens)
{ {
accExpression << LocalizationService::GetNarratorReadableToken(StringReference(tokenItem.first.c_str()))->Data(); accExpression += LocalizationService::GetNarratorReadableToken(StringReference(tokenItem.first.c_str()))->Data();
} }
return ref new String(accExpression.str().c_str()); return ref new String(accExpression.c_str());
} }

View file

@ -177,21 +177,20 @@ String ^ StandardCalculatorViewModel::CalculateNarratorDisplayValue(_In_ wstring
String ^ StandardCalculatorViewModel::GetNarratorStringReadRawNumbers(_In_ String ^ localizedDisplayValue) String ^ StandardCalculatorViewModel::GetNarratorStringReadRawNumbers(_In_ String ^ localizedDisplayValue)
{ {
wstringstream wss; wstring ws;
auto& locSettings = LocalizationSettings::GetInstance(); const auto& locSettings = LocalizationSettings::GetInstance();
// Insert a space after each digit in the string, to force Narrator to read them as separate numbers. // Insert a space after each digit in the string, to force Narrator to read them as separate numbers.
wstring wstrValue(localizedDisplayValue->Data()); for (const wchar_t& c : localizedDisplayValue)
for (wchar_t& c : wstrValue)
{ {
wss << c; ws += c;
if (locSettings.IsLocalizedHexDigit(c)) if (locSettings.IsLocalizedHexDigit(c))
{ {
wss << L' '; ws += L' ';
} }
} }
return ref new String(wss.str().c_str()); return ref new String(ws.c_str());
} }
void StandardCalculatorViewModel::SetPrimaryDisplay(_In_ String ^ displayStringValue, _In_ bool isError) void StandardCalculatorViewModel::SetPrimaryDisplay(_In_ String ^ displayStringValue, _In_ bool isError)