Add support for long long to wxNumberFormatter.
It seems to make sense to allow using it for formatting and parsing long long values as well as it can be done trivially using almost the same code as for long. It would be nice to support long double in a similar way but we don't wrap C99 strtold() right now so it wouldn't be as simple, leave it for later. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66712 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -30,7 +30,10 @@ public:
|
|||||||
// precision can also be specified.
|
// precision can also be specified.
|
||||||
static wxString ToString(long val,
|
static wxString ToString(long val,
|
||||||
int style = Style_WithThousandsSep);
|
int style = Style_WithThousandsSep);
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
static wxString ToString(wxLongLong_t val,
|
||||||
|
int style = Style_WithThousandsSep);
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
static wxString ToString(double val,
|
static wxString ToString(double val,
|
||||||
int precision,
|
int precision,
|
||||||
int style = Style_WithThousandsSep);
|
int style = Style_WithThousandsSep);
|
||||||
@@ -40,6 +43,9 @@ public:
|
|||||||
// Return true on success and stores the result in the provided location
|
// Return true on success and stores the result in the provided location
|
||||||
// which must be a valid non-NULL pointer.
|
// which must be a valid non-NULL pointer.
|
||||||
static bool FromString(wxString s, long *val);
|
static bool FromString(wxString s, long *val);
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
static bool FromString(wxString s, wxLongLong_t *val);
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
static bool FromString(wxString s, double *val);
|
static bool FromString(wxString s, double *val);
|
||||||
|
|
||||||
|
|
||||||
@@ -53,6 +59,9 @@ public:
|
|||||||
static bool GetThousandsSeparatorIfUsed(wxChar *sep);
|
static bool GetThousandsSeparatorIfUsed(wxChar *sep);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// Post-process the string representing an integer.
|
||||||
|
static wxString PostProcessIntString(wxString s, int style);
|
||||||
|
|
||||||
// Add the thousands separators to a string representing a number without
|
// Add the thousands separators to a string representing a number without
|
||||||
// the separators. This is used by ToString(Style_WithThousandsSep).
|
// the separators. This is used by ToString(Style_WithThousandsSep).
|
||||||
static void AddThousandsSeparators(wxString& s);
|
static void AddThousandsSeparators(wxString& s);
|
||||||
|
@@ -94,10 +94,8 @@ bool wxNumberFormatter::GetThousandsSeparatorIfUsed(wxChar *sep)
|
|||||||
// Conversion to string and helpers
|
// Conversion to string and helpers
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
wxString wxNumberFormatter::ToString(long val, int style)
|
wxString wxNumberFormatter::PostProcessIntString(wxString s, int style)
|
||||||
{
|
{
|
||||||
wxString s = wxString::Format("%ld", val);
|
|
||||||
|
|
||||||
if ( style & Style_WithThousandsSep )
|
if ( style & Style_WithThousandsSep )
|
||||||
AddThousandsSeparators(s);
|
AddThousandsSeparators(s);
|
||||||
|
|
||||||
@@ -107,6 +105,21 @@ wxString wxNumberFormatter::ToString(long val, int style)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wxString wxNumberFormatter::ToString(long val, int style)
|
||||||
|
{
|
||||||
|
return PostProcessIntString(wxString::Format("%ld", val), style);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
|
wxString wxNumberFormatter::ToString(wxLongLong_t val, int style)
|
||||||
|
{
|
||||||
|
return PostProcessIntString(wxString::Format("%" wxLongLongFmtSpec "d", val),
|
||||||
|
style);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
wxString wxNumberFormatter::ToString(double val, int precision, int style)
|
wxString wxNumberFormatter::ToString(double val, int precision, int style)
|
||||||
{
|
{
|
||||||
const wxString fmt = wxString::Format("%%.%df", precision);
|
const wxString fmt = wxString::Format("%%.%df", precision);
|
||||||
@@ -183,6 +196,16 @@ bool wxNumberFormatter::FromString(wxString s, long *val)
|
|||||||
return s.ToLong(val);
|
return s.ToLong(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
|
bool wxNumberFormatter::FromString(wxString s, wxLongLong_t *val)
|
||||||
|
{
|
||||||
|
RemoveThousandsSeparators(s);
|
||||||
|
return s.ToLongLong(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
bool wxNumberFormatter::FromString(wxString s, double *val)
|
bool wxNumberFormatter::FromString(wxString s, double *val)
|
||||||
{
|
{
|
||||||
RemoveThousandsSeparators(s);
|
RemoveThousandsSeparators(s);
|
||||||
|
@@ -53,16 +53,28 @@ public:
|
|||||||
private:
|
private:
|
||||||
CPPUNIT_TEST_SUITE( NumFormatterTestCase );
|
CPPUNIT_TEST_SUITE( NumFormatterTestCase );
|
||||||
CPPUNIT_TEST( LongToString );
|
CPPUNIT_TEST( LongToString );
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
CPPUNIT_TEST( LongLongToString );
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
CPPUNIT_TEST( DoubleToString );
|
CPPUNIT_TEST( DoubleToString );
|
||||||
CPPUNIT_TEST( NoTrailingZeroes );
|
CPPUNIT_TEST( NoTrailingZeroes );
|
||||||
CPPUNIT_TEST( LongFromString );
|
CPPUNIT_TEST( LongFromString );
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
CPPUNIT_TEST( LongLongFromString );
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
CPPUNIT_TEST( DoubleFromString );
|
CPPUNIT_TEST( DoubleFromString );
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
void LongToString();
|
void LongToString();
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
void LongLongToString();
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
void DoubleToString();
|
void DoubleToString();
|
||||||
void NoTrailingZeroes();
|
void NoTrailingZeroes();
|
||||||
void LongFromString();
|
void LongFromString();
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
void LongLongFromString();
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
void DoubleFromString();
|
void DoubleFromString();
|
||||||
|
|
||||||
wxLocale *m_locale;
|
wxLocale *m_locale;
|
||||||
@@ -85,17 +97,37 @@ void NumFormatterTestCase::LongToString()
|
|||||||
if ( !m_locale )
|
if ( !m_locale )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString( 1));
|
CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString( 1L));
|
||||||
CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString( 12));
|
CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString( 12L));
|
||||||
CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString( 123));
|
CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString( 123L));
|
||||||
CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString( 1234));
|
CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString( 1234L));
|
||||||
CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString( 12345));
|
CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString( 12345L));
|
||||||
CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString( 123456));
|
CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString( 123456L));
|
||||||
CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString( 1234567));
|
CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString( 1234567L));
|
||||||
CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString( 12345678));
|
CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString( 12345678L));
|
||||||
CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString( 123456789));
|
CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString( 123456789L));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
|
void NumFormatterTestCase::LongLongToString()
|
||||||
|
{
|
||||||
|
if ( !m_locale )
|
||||||
|
return;
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "1", wxNumberFormatter::ToString(wxLL( 1)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "12", wxNumberFormatter::ToString(wxLL( 12)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "123", wxNumberFormatter::ToString(wxLL( 123)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "1,234", wxNumberFormatter::ToString(wxLL( 1234)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "12,345", wxNumberFormatter::ToString(wxLL( 12345)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "123,456", wxNumberFormatter::ToString(wxLL( 123456)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "1,234,567", wxNumberFormatter::ToString(wxLL( 1234567)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL( "12,345,678", wxNumberFormatter::ToString(wxLL( 12345678)));
|
||||||
|
CPPUNIT_ASSERT_EQUAL("123,456,789", wxNumberFormatter::ToString(wxLL( 123456789)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
void NumFormatterTestCase::DoubleToString()
|
void NumFormatterTestCase::DoubleToString()
|
||||||
{
|
{
|
||||||
if ( !m_locale )
|
if ( !m_locale )
|
||||||
@@ -196,6 +228,44 @@ void NumFormatterTestCase::LongFromString()
|
|||||||
CPPUNIT_ASSERT_EQUAL( 1234567, l );
|
CPPUNIT_ASSERT_EQUAL( 1234567, l );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
|
void NumFormatterTestCase::LongLongFromString()
|
||||||
|
{
|
||||||
|
if ( !m_locale )
|
||||||
|
return;
|
||||||
|
|
||||||
|
WX_ASSERT_FAILS_WITH_ASSERT
|
||||||
|
(
|
||||||
|
wxNumberFormatter::FromString("123", static_cast<wxLongLong_t *>(0))
|
||||||
|
);
|
||||||
|
|
||||||
|
wxLongLong_t l;
|
||||||
|
CPPUNIT_ASSERT( !wxNumberFormatter::FromString("", &l) );
|
||||||
|
CPPUNIT_ASSERT( !wxNumberFormatter::FromString("foo", &l) );
|
||||||
|
CPPUNIT_ASSERT( !wxNumberFormatter::FromString("1.234", &l) );
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT( wxNumberFormatter::FromString("123", &l) );
|
||||||
|
CPPUNIT_ASSERT_EQUAL( 123, l );
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT( wxNumberFormatter::FromString("1234", &l) );
|
||||||
|
CPPUNIT_ASSERT_EQUAL( 1234, l );
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234", &l) );
|
||||||
|
CPPUNIT_ASSERT_EQUAL( 1234, l );
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT( wxNumberFormatter::FromString("12,345", &l) );
|
||||||
|
CPPUNIT_ASSERT_EQUAL( 12345, l );
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT( wxNumberFormatter::FromString("123,456", &l) );
|
||||||
|
CPPUNIT_ASSERT_EQUAL( 123456, l );
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT( wxNumberFormatter::FromString("1,234,567", &l) );
|
||||||
|
CPPUNIT_ASSERT_EQUAL( 1234567, l );
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
|
||||||
|
|
||||||
void NumFormatterTestCase::DoubleFromString()
|
void NumFormatterTestCase::DoubleFromString()
|
||||||
{
|
{
|
||||||
if ( !m_locale )
|
if ( !m_locale )
|
||||||
|
Reference in New Issue
Block a user