Replace redundant FString code with templates

This commit is contained in:
Markus Gans 2018-12-29 22:10:24 +01:00
parent bd3b2b31bb
commit 3ad4652883
5 changed files with 109 additions and 80 deletions

View File

@ -1,5 +1,6 @@
2018-12-28 Markus Gans <guru.mail@muenster.de>
* Text scrolling in FTextView was broken since February 17th!
* Replace redundant FString code with templates
2018-12-28 Markus Gans <guru.mail@muenster.de>
* Add the assignment operator (=) for FButton to set the button text

View File

@ -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]; // = "%.<precision>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;

View File

@ -54,6 +54,7 @@
#include <new>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
#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 <typename NumT>
FString& setNumber (NumT, int = getPrecision<NumT>());
FString& setNumber (sInt64);
FString& setNumber (uInt64);
FString& setNumber (lDouble, int = getPrecision<lDouble>());
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 <typename NumT>
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 <typename NumT>
inline FString& FString::setNumber (NumT num, int precision)
{
if ( std::is_floating_point<NumT>::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 <typename NumT>
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

View File

@ -93,6 +93,15 @@ inline bool isNegative (const T& x)
return is_negative<T, std::numeric_limits<T>::is_signed>()(x);
}
template <typename T>
struct getPrecision
{
operator int ()
{
return std::numeric_limits<T>::digits10;
}
};
namespace fc
{
#pragma pack(push)

View File

@ -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<float>() == FLT_DIG );
CPPUNIT_ASSERT ( finalcut::getPrecision<double>() == DBL_DIG );
CPPUNIT_ASSERT ( finalcut::getPrecision<lDouble>() == 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" );
}