From 0d30c79b4450b5e5960d4f945b18c0a2287f7045 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 19 Jan 2011 10:48:10 +0000 Subject: [PATCH] Update cached values in wxNumberFormatter when locale changes. Caching the decimal and thousands separators in wxNumberFormatter is a useful performance optimization, however it can give wrong results if the locale changed since the cached values were initialized. So remember the locale used for the initialization and redo it if it changed. This should still be almost as fast as the previous version but now also correct (still not MT-safe though). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66713 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/numformatter.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/common/numformatter.cpp b/src/common/numformatter.cpp index ff658fb737..39f4f5743e 100644 --- a/src/common/numformatter.cpp +++ b/src/common/numformatter.cpp @@ -36,7 +36,11 @@ wxChar wxNumberFormatter::GetDecimalSeparator() // concurrently from more than one thread so it's not a real problem. static wxChar s_decimalSeparator = 0; - if ( !s_decimalSeparator ) + // Remember the locale which was current when we initialized, we must redo + // the initialization if the locale changed. + static wxLocale *s_localeUsedForInit = NULL; + + if ( !s_decimalSeparator || (s_localeUsedForInit != wxGetLocale()) ) { const wxString s = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); @@ -54,6 +58,8 @@ wxChar wxNumberFormatter::GetDecimalSeparator() s_decimalSeparator = s[0]; } + + s_localeUsedForInit = wxGetLocale(); } return s_decimalSeparator; @@ -63,8 +69,9 @@ bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep) { static wxChar s_thousandsSeparator = 0; static bool s_initialized = false; + static wxLocale *s_localeUsedForInit = NULL; - if ( !s_initialized ) + if ( !s_initialized || (s_localeUsedForInit != wxGetLocale()) ) { const wxString s = wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP, wxLOCALE_CAT_NUMBER); @@ -79,6 +86,7 @@ bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep) // be empty if grouping is not used, so just leave it as 0. s_initialized = true; + s_localeUsedForInit = wxGetLocale(); } if ( !s_thousandsSeparator ) -- 2.45.2