From 3ad46528839417e467a58ae48d8ade61ee0d0a3b Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Sat, 29 Dec 2018 22:10:24 +0100 Subject: [PATCH] Replace redundant FString code with templates --- ChangeLog | 1 + src/fstring.cpp | 12 ++--- src/include/final/fstring.h | 78 ++++++++++++-------------------- src/include/final/ftypes.h | 9 ++++ test/fstring-test.cpp | 89 +++++++++++++++++++++++++++---------- 5 files changed, 109 insertions(+), 80 deletions(-) diff --git a/ChangeLog b/ChangeLog index ed364116..9c6e7ba0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 2018-12-28 Markus Gans * Text scrolling in FTextView was broken since February 17th! + * Replace redundant FString code with templates 2018-12-28 Markus Gans * Add the assignment operator (=) for FButton to set the button text diff --git a/src/fstring.cpp b/src/fstring.cpp index 615a1354..d0db046f 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -896,7 +896,7 @@ FString& FString::setString (const FString& s) } //---------------------------------------------------------------------- -FString& FString::setNumber (long num) +FString& FString::setNumber (sInt64 num) { wchar_t* s; bool neg; @@ -931,7 +931,7 @@ FString& FString::setNumber (long num) } //---------------------------------------------------------------------- -FString& FString::setNumber (uLong num) +FString& FString::setNumber (uInt64 num) { wchar_t* s; wchar_t buf[30]; @@ -951,7 +951,7 @@ FString& FString::setNumber (uLong num) } //---------------------------------------------------------------------- -FString& FString::setNumber (lDouble num, int precision) +FString& FString::setNumber (lDouble f_num, int precision) { wchar_t* s; wchar_t format[20]; // = "%.Lg" @@ -980,11 +980,11 @@ FString& FString::setNumber (lDouble num, int precision) *s++ = L'g'; *s = L'\0'; - return sprintf(format, num); + return sprintf(format, f_num); } //---------------------------------------------------------------------- -FString& FString::setFormatedNumber (long num, char separator) +FString& FString::setFormatedNumber (sInt64 num, char separator) { int n; wchar_t* s; @@ -1027,7 +1027,7 @@ FString& FString::setFormatedNumber (long num, char separator) } //---------------------------------------------------------------------- -FString& FString::setFormatedNumber (uLong num, char separator) +FString& FString::setFormatedNumber (uInt64 num, char separator) { int n; wchar_t* s; diff --git a/src/include/final/fstring.h b/src/include/final/fstring.h index 5d51911a..4d59f74a 100644 --- a/src/include/final/fstring.h +++ b/src/include/final/fstring.h @@ -54,6 +54,7 @@ #include #include #include +#include #include #include "final/fc.h" @@ -228,22 +229,16 @@ class FString FStringList split (const FString&); FString& setString (const FString&); - FString& setNumber (sInt16); - FString& setNumber (uInt16); - FString& setNumber (int); - FString& setNumber (uInt); - FString& setNumber (long); - FString& setNumber (uLong); - FString& setNumber (float, int = FLT_DIG); - FString& setNumber (double, int = DBL_DIG); - FString& setNumber (lDouble, int = LDBL_DIG); + template + FString& setNumber (NumT, int = getPrecision()); + FString& setNumber (sInt64); + FString& setNumber (uInt64); + FString& setNumber (lDouble, int = getPrecision()); - FString& setFormatedNumber (sInt16, char = nl_langinfo(THOUSEP)[0]); - FString& setFormatedNumber (uInt16, char = nl_langinfo(THOUSEP)[0]); - FString& setFormatedNumber (int, char = nl_langinfo(THOUSEP)[0]); - FString& setFormatedNumber (uInt, char = nl_langinfo(THOUSEP)[0]); - FString& setFormatedNumber (long, char = nl_langinfo(THOUSEP)[0]); - FString& setFormatedNumber (uLong, char = nl_langinfo(THOUSEP)[0]); + template + FString& setFormatedNumber (NumT, char = nl_langinfo(THOUSEP)[0]); + FString& setFormatedNumber (sInt64, char = nl_langinfo(THOUSEP)[0]); + FString& setFormatedNumber (uInt64, char = nl_langinfo(THOUSEP)[0]); const FString& insert (const FString&, int); const FString& insert (const FString&, std::size_t); @@ -397,44 +392,27 @@ inline wchar_t FString::back() const } //---------------------------------------------------------------------- -inline FString& FString::setNumber (sInt16 num) -{ return setNumber (long(num)); } +template +inline FString& FString::setNumber (NumT num, int precision) +{ + if ( std::is_floating_point::value ) + return setNumber (lDouble(num), precision); + + if ( isNegative(num) ) + return setNumber (sInt64(num)); + + return setNumber (uInt64(num)); +} //---------------------------------------------------------------------- -inline FString& FString::setNumber (uInt16 num) -{ return setNumber (uLong(num)); } +template +inline FString& FString::setFormatedNumber (NumT num, char separator) +{ + if ( isNegative(num) ) + return setFormatedNumber (sInt64(num), separator); -//---------------------------------------------------------------------- -inline FString& FString::setNumber (int num) -{ return setNumber (long(num)); } - -//---------------------------------------------------------------------- -inline FString& FString::setNumber (uInt num) -{ return setNumber (uLong(num)); } - -//---------------------------------------------------------------------- -inline FString& FString::setNumber (float num, int precision) -{ return setNumber (lDouble(num), precision); } - -//---------------------------------------------------------------------- -inline FString& FString::setNumber (double num, int precision) -{ return setNumber (lDouble(num), precision); } - -//---------------------------------------------------------------------- -inline FString& FString::setFormatedNumber (sInt16 num, char separator) -{ return setFormatedNumber (long(num), separator); } - -//---------------------------------------------------------------------- -inline FString& FString::setFormatedNumber (uInt16 num, char separator) -{ return setFormatedNumber (uLong(num), separator); } - -//---------------------------------------------------------------------- -inline FString& FString::setFormatedNumber (int num, char separator) -{ return setFormatedNumber (long(num), separator); } - -//---------------------------------------------------------------------- -inline FString& FString::setFormatedNumber (uInt num, char separator) -{ return setFormatedNumber (uLong(num), separator); } + return setFormatedNumber (uInt64(num), separator); +} } // namespace finalcut diff --git a/src/include/final/ftypes.h b/src/include/final/ftypes.h index 661f4aca..ec30f938 100644 --- a/src/include/final/ftypes.h +++ b/src/include/final/ftypes.h @@ -93,6 +93,15 @@ inline bool isNegative (const T& x) return is_negative::is_signed>()(x); } +template +struct getPrecision +{ + operator int () + { + return std::numeric_limits::digits10; + } +}; + namespace fc { #pragma pack(push) diff --git a/test/fstring-test.cpp b/test/fstring-test.cpp index 9bb87eec..2a8b60ef 100644 --- a/test/fstring-test.cpp +++ b/test/fstring-test.cpp @@ -1059,6 +1059,12 @@ void FStringTest::formatTest() fnum2.setFormatedNumber(uLong(9223372036854775807), '\0'); CPPUNIT_ASSERT ( fnum2 == "9 223 372 036 854 775 807" ); + + fnum2.setFormatedNumber(sInt64(9223372036854775807), '\0'); + CPPUNIT_ASSERT ( fnum2 == "9 223 372 036 854 775 807" ); + + fnum2.setFormatedNumber(uInt64(9223372036854775807), '\0'); + CPPUNIT_ASSERT ( fnum2 == "9 223 372 036 854 775 807" ); #else // 32-bit architecture fnum1.setFormatedNumber(0xffffffff, '\''); @@ -1072,7 +1078,25 @@ void FStringTest::formatTest() fnum2.setFormatedNumber(uLong(2147483647), '\0'); CPPUNIT_ASSERT ( fnum2 == "2 147 483 647" ); + + fnum2.setFormatedNumber(sInt32(2147483647), '\0'); + CPPUNIT_ASSERT ( fnum2 == "2 147 483 647" ); + + fnum2.setFormatedNumber(uInt32(2147483647), '\0'); + CPPUNIT_ASSERT ( fnum2 == "2 147 483 647" ); #endif + + fnum1.setFormatedNumber(sInt16(-2048), '_'); + CPPUNIT_ASSERT ( fnum1 == "-2_048" ); + + fnum2.setFormatedNumber(uInt16(65535)); + CPPUNIT_ASSERT ( fnum2 == "65 535" ); + + fnum1.setFormatedNumber(sInt8(-123), '*'); + CPPUNIT_ASSERT ( fnum1 == "-123" ); + + fnum2.setFormatedNumber(uInt8(255)); + CPPUNIT_ASSERT ( fnum2 == "255" ); } //---------------------------------------------------------------------- @@ -1118,32 +1142,49 @@ void FStringTest::convertToNumberTest() //---------------------------------------------------------------------- void FStringTest::convertFromNumberTest() { - constexpr sInt16 n1 = -1234; - constexpr uInt16 n2 = 1234; - constexpr int n3 = -12345; - constexpr uInt n4 = 12345; - constexpr long n5 = -12345678; - constexpr uLong n6 = 12345678; - constexpr float n7 = 1234.56f; - constexpr double n8 = 1234.5678; - constexpr lDouble n9 = 12345.67890L; - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n1) == "-1234" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n2) == "1234" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n3) == "-12345" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n4) == "12345" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n5) == "-12345678" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n6) == "12345678" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n7) == "1234.56" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n8) == "1234.5678" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n9) == "12345.6789" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n7, 0) == "1e+03" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n8, 0) == "1e+03" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n9, 0) == "1e+04" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n7, 100) + constexpr sInt8 n1 = -12; + constexpr uInt8 n2 = 12; + constexpr sInt16 n3 = -1234; + constexpr uInt16 n4 = 1234; + constexpr int n5 = -12345; + constexpr uInt n6 = 12345; + constexpr sInt32 n7 = -12345; + constexpr uInt32 n8 = 12345; + constexpr long n9 = -12345678; + constexpr uLong n10 = 12345678; + constexpr sInt64 n11 = -12345678; + constexpr uInt64 n12 = 12345678; + constexpr float n13 = 1234.56f; + constexpr double n14 = 1234.5678; + constexpr lDouble n15 = 12345.67890L; + + CPPUNIT_ASSERT ( finalcut::getPrecision() == FLT_DIG ); + CPPUNIT_ASSERT ( finalcut::getPrecision() == DBL_DIG ); + CPPUNIT_ASSERT ( finalcut::getPrecision() == LDBL_DIG ); + + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n1) == "-12" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n2) == "12" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n3) == "-1234" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n4) == "1234" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n5) == "-12345" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n6) == "12345" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n7) == "-12345" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n8) == "12345" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n9) == "-12345678" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n10) == "12345678" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n11) == "-12345678" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n12) == "12345678" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n13) == "1234.56" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n14) == "1234.5678" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n15) == "12345.6789" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n13, 0) == "1e+03" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n14, 0) == "1e+03" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n15, 0) == "1e+04" ); + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n13, 100) == "1234.56005859375" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n8, 100) + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n14, 100) == "1234.567800000000033833202905952930450439453125" ); - CPPUNIT_ASSERT ( finalcut::FString().setNumber(n9, 100) + CPPUNIT_ASSERT ( finalcut::FString().setNumber(n15, 100) == "12345.67889999999999961488583721802569925785064697265625" ); }