Don't cache locale-specific data in wxNumberFormatter

This cache was never updated and so could be wrong and not using it at
all is the simplest (although also the slowest) fix for this.

Notably, it could result in failures in ValNum::Interactive unit test
because the thousands separator was set to nothing during a previous
test execution, instead of the expected comma.

This commit is best viewed ignoring whitespace-only changes.
This commit is contained in:
Vadim Zeitlin 2022-03-23 18:46:55 +01:00
parent 4a9da949ce
commit 8bc606ba73

View File

@ -29,64 +29,36 @@
wxChar wxNumberFormatter::GetDecimalSeparator() wxChar wxNumberFormatter::GetDecimalSeparator()
{ {
#if wxUSE_INTL #if wxUSE_INTL
// Notice that while using static variable here is not MT-safe, the worst
// that can happen is that we redo the initialization if we're called
// concurrently from more than one thread so it's not a real problem.
static wxChar s_decimalSeparator = 0;
if ( !s_decimalSeparator )
{
const wxString const wxString
s = wxUILocale::GetCurrent().GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); s = wxUILocale::GetCurrent().GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
if ( s.length() == 1 ) if ( s.length() == 1 )
{ {
s_decimalSeparator = s[0]; return s[0];
}
else
{
// We really must have something for decimal separator, so fall
// back to the C locale default.
s_decimalSeparator = '.';
}
} }
return s_decimalSeparator; // We really must have something for decimal separator, so fall
#else // !wxUSE_INTL // back to the C locale default.
#endif // wxUSE_INTL
return wxT('.'); return wxT('.');
#endif // wxUSE_INTL/!wxUSE_INTL
} }
bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep) bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep)
{ {
#if wxUSE_INTL #if wxUSE_INTL
static wxChar s_thousandsSeparator = 0;
static bool s_thousandsSeparatorInitialized = false;
if ( !s_thousandsSeparatorInitialized )
{
s_thousandsSeparatorInitialized = true;
const wxString const wxString
s = wxUILocale::GetCurrent().GetInfo(wxLOCALE_THOUSANDS_SEP, wxLOCALE_CAT_NUMBER); s = wxUILocale::GetCurrent().GetInfo(wxLOCALE_THOUSANDS_SEP, wxLOCALE_CAT_NUMBER);
if ( s.length() == 1 ) if ( s.length() == 1 )
{ {
s_thousandsSeparator = s[0];
}
//else: Unlike above it's perfectly fine for the thousands separator to
// be empty if grouping is not used, so just leave it as 0.
}
if ( !s_thousandsSeparator )
return false;
if ( sep ) if ( sep )
*sep = s_thousandsSeparator; *sep = s[0];
return true; return true;
#else // !wxUSE_INTL }
#endif // wxUSE_INTL
wxUnusedVar(sep); wxUnusedVar(sep);
return false; return false;
#endif // wxUSE_INTL/!wxUSE_INTL
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------