Fix formatting issues with CurrencyConverter and some locales (#242)
The ViewModel wrongly assumed that non-breaking spaces were only used between the value and the symbol. It's not the case of all locales using non-breaking spaces as a thousand delimiter (French for example). When it was the case, the function only replaced the first thousand delimiter found and kept the extra space at the end of the string, generating 2 issues: Extra space at the end: #240 Bad formatting of the number: #232 Description of the changes: Replace currencyResult.find(L'\u00a0') by a regex only removing spaces at the end of the string. Fixes #240 and #232
This commit is contained in:
		
				
					committed by
					
						
						Howard Wolosky
					
				
			
			
				
	
			
			
			
						parent
						
							3d18dd38c2
						
					
				
				
					commit
					62b2fafdd0
				
			@@ -53,6 +53,8 @@ constexpr size_t SELECTED_TARGET_UNIT = 2;
 | 
			
		||||
 | 
			
		||||
// x millisecond delay before we consider conversion to be final
 | 
			
		||||
constexpr unsigned int CONVERSION_FINALIZED_DELAY_IN_MS = 1000;
 | 
			
		||||
const wregex regexTrimSpacesStart = wregex(L"^\\s+");
 | 
			
		||||
const wregex regexTrimSpacesEnd = wregex(L"\\s+$");
 | 
			
		||||
 | 
			
		||||
namespace CalculatorApp::ViewModel
 | 
			
		||||
{
 | 
			
		||||
@@ -346,10 +348,15 @@ String^ UnitConverterViewModel::ConvertToLocalizedString(const std::wstring& str
 | 
			
		||||
            if (pos != wstring::npos)
 | 
			
		||||
            {
 | 
			
		||||
                currencyResult.erase(pos, currencyCode.length());
 | 
			
		||||
                pos = currencyResult.find(L'\u00a0'); // non-breaking space
 | 
			
		||||
                if (pos != wstring::npos)
 | 
			
		||||
                std::wsmatch sm;
 | 
			
		||||
                if (regex_search(currencyResult, sm, regexTrimSpacesStart))
 | 
			
		||||
                {
 | 
			
		||||
                    currencyResult.erase(pos, 1);
 | 
			
		||||
                    currencyResult.erase(sm.prefix().length(), sm.length());
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (regex_search(currencyResult, sm, regexTrimSpacesEnd))
 | 
			
		||||
                {
 | 
			
		||||
                    currencyResult.erase(sm.prefix().length(), sm.length());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user