X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0d30c79b4450b5e5960d4f945b18c0a2287f7045..a66bf817a6ba833ad47cefc9fa8948ef8045a59d:/src/common/numformatter.cpp diff --git a/src/common/numformatter.cpp b/src/common/numformatter.cpp index 39f4f5743e..12de60e842 100644 --- a/src/common/numformatter.cpp +++ b/src/common/numformatter.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: numformatter.cpp +// Name: src/common/numformatter.cpp // Purpose: wxNumberFormatter // Author: Fulvio Senore, Vadim Zeitlin // Created: 2010-11-06 @@ -21,6 +21,83 @@ #include "wx/numformatter.h" #include "wx/intl.h" +#include // for setlocale and LC_ALL + +// ---------------------------------------------------------------------------- +// local helpers +// ---------------------------------------------------------------------------- + +namespace +{ + +// Contains information about the locale which was used to initialize our +// cached values of the decimal and thousands separators. Notice that it isn't +// enough to store just wxLocale because the user code may call setlocale() +// directly and storing just C locale string is not enough because we can use +// the OS API directly instead of the CRT ones on some platforms. So just store +// both. +class LocaleId +{ +public: + LocaleId() + { +#if wxUSE_INTL + m_wxloc = NULL; +#endif // wxUSE_INTL + m_cloc = NULL; + } + + ~LocaleId() + { + Free(); + } + +#if wxUSE_INTL + // Return true if this is the first time this function is called for this + // object or if the program locale has changed since the last time it was + // called. Otherwise just return false indicating that updating locale- + // dependent information is not necessary. + bool NotInitializedOrHasChanged() + { + wxLocale * const wxloc = wxGetLocale(); + const char * const cloc = setlocale(LC_ALL, NULL); + if ( m_wxloc || m_cloc ) + { + if ( m_wxloc == wxloc && strcmp(m_cloc, cloc) == 0 ) + return false; + + Free(); + } + //else: Not initialized yet. + + m_wxloc = wxloc; + m_cloc = wxCRT_StrdupA(cloc); + + return true; + } +#endif // wxUSE_INTL + +private: + void Free() + { +#if wxUSE_INTL + free(m_cloc); +#endif // wxUSE_INTL + } + +#if wxUSE_INTL + // Non-owned pointer to wxLocale which was used. + wxLocale *m_wxloc; +#endif // wxUSE_INTL + + // Owned pointer to the C locale string. + char *m_cloc; + + wxDECLARE_NO_COPY_CLASS(LocaleId); +}; + +} // anonymous namespace + // ============================================================================ // wxNumberFormatter implementation // ============================================================================ @@ -31,6 +108,7 @@ wxChar wxNumberFormatter::GetDecimalSeparator() { +#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. @@ -38,9 +116,9 @@ wxChar wxNumberFormatter::GetDecimalSeparator() // Remember the locale which was current when we initialized, we must redo // the initialization if the locale changed. - static wxLocale *s_localeUsedForInit = NULL; + static LocaleId s_localeUsedForInit; - if ( !s_decimalSeparator || (s_localeUsedForInit != wxGetLocale()) ) + if ( s_localeUsedForInit.NotInitializedOrHasChanged() ) { const wxString s = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); @@ -58,20 +136,21 @@ wxChar wxNumberFormatter::GetDecimalSeparator() s_decimalSeparator = s[0]; } - - s_localeUsedForInit = wxGetLocale(); } return s_decimalSeparator; +#else // !wxUSE_INTL + return wxT('.'); +#endif // wxUSE_INTL/!wxUSE_INTL } bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep) { +#if wxUSE_INTL static wxChar s_thousandsSeparator = 0; - static bool s_initialized = false; - static wxLocale *s_localeUsedForInit = NULL; + static LocaleId s_localeUsedForInit; - if ( !s_initialized || (s_localeUsedForInit != wxGetLocale()) ) + if ( s_localeUsedForInit.NotInitializedOrHasChanged() ) { const wxString s = wxLocale::GetInfo(wxLOCALE_THOUSANDS_SEP, wxLOCALE_CAT_NUMBER); @@ -84,9 +163,6 @@ bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep) } //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. - - s_initialized = true; - s_localeUsedForInit = wxGetLocale(); } if ( !s_thousandsSeparator ) @@ -96,6 +172,10 @@ bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep) *sep = s_thousandsSeparator; return true; +#else // !wxUSE_INTL + wxUnusedVar(sep); + return false; +#endif // wxUSE_INTL/!wxUSE_INTL } // ---------------------------------------------------------------------------- @@ -130,8 +210,7 @@ wxString wxNumberFormatter::ToString(wxLongLong_t val, int style) wxString wxNumberFormatter::ToString(double val, int precision, int style) { - const wxString fmt = wxString::Format("%%.%df", precision); - wxString s = wxString::Format(fmt, val); + wxString s = wxString::FromDouble(val,precision); if ( style & Style_WithThousandsSep ) AddThousandsSeparators(s); @@ -155,6 +234,10 @@ void wxNumberFormatter::AddThousandsSeparators(wxString& s) pos = s.length(); } + // End grouping at the beginning of the digits -- there could be at a sign + // before their start. + const size_t start = s.find_first_of("0123456789"); + // We currently group digits by 3 independently of the locale. This is not // the right thing to do and we should use lconv::grouping (under POSIX) // and GetLocaleInfo(LOCALE_SGROUPING) (under MSW) to get information about @@ -162,7 +245,7 @@ void wxNumberFormatter::AddThousandsSeparators(wxString& s) // wxLocale level first and then used here in the future (TODO). const size_t GROUP_LEN = 3; - while ( pos > GROUP_LEN ) + while ( pos > start + GROUP_LEN ) { pos -= GROUP_LEN; s.insert(pos, thousandsSep); @@ -172,7 +255,8 @@ void wxNumberFormatter::AddThousandsSeparators(wxString& s) void wxNumberFormatter::RemoveTrailingZeroes(wxString& s) { const size_t posDecSep = s.find(GetDecimalSeparator()); - wxCHECK_RET( posDecSep != wxString::npos, "No decimal separator" ); + wxCHECK_RET( posDecSep != wxString::npos, + wxString::Format("No decimal separator in \"%s\"", s) ); wxCHECK_RET( posDecSep, "Can't start with decimal separator" ); // Find the last character to keep.