Replace redundant FString code with templates
This commit is contained in:
parent
bd3b2b31bb
commit
3ad4652883
|
@ -1,5 +1,6 @@
|
||||||
2018-12-28 Markus Gans <guru.mail@muenster.de>
|
2018-12-28 Markus Gans <guru.mail@muenster.de>
|
||||||
* Text scrolling in FTextView was broken since February 17th!
|
* Text scrolling in FTextView was broken since February 17th!
|
||||||
|
* Replace redundant FString code with templates
|
||||||
|
|
||||||
2018-12-28 Markus Gans <guru.mail@muenster.de>
|
2018-12-28 Markus Gans <guru.mail@muenster.de>
|
||||||
* Add the assignment operator (=) for FButton to set the button text
|
* Add the assignment operator (=) for FButton to set the button text
|
||||||
|
|
|
@ -896,7 +896,7 @@ FString& FString::setString (const FString& s)
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
FString& FString::setNumber (long num)
|
FString& FString::setNumber (sInt64 num)
|
||||||
{
|
{
|
||||||
wchar_t* s;
|
wchar_t* s;
|
||||||
bool neg;
|
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* s;
|
||||||
wchar_t buf[30];
|
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* s;
|
||||||
wchar_t format[20]; // = "%.<precision>Lg"
|
wchar_t format[20]; // = "%.<precision>Lg"
|
||||||
|
@ -980,11 +980,11 @@ FString& FString::setNumber (lDouble num, int precision)
|
||||||
*s++ = L'g';
|
*s++ = L'g';
|
||||||
*s = L'\0';
|
*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;
|
int n;
|
||||||
wchar_t* s;
|
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;
|
int n;
|
||||||
wchar_t* s;
|
wchar_t* s;
|
||||||
|
|
|
@ -54,6 +54,7 @@
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "final/fc.h"
|
#include "final/fc.h"
|
||||||
|
@ -228,22 +229,16 @@ class FString
|
||||||
FStringList split (const FString&);
|
FStringList split (const FString&);
|
||||||
FString& setString (const FString&);
|
FString& setString (const FString&);
|
||||||
|
|
||||||
FString& setNumber (sInt16);
|
template <typename NumT>
|
||||||
FString& setNumber (uInt16);
|
FString& setNumber (NumT, int = getPrecision<NumT>());
|
||||||
FString& setNumber (int);
|
FString& setNumber (sInt64);
|
||||||
FString& setNumber (uInt);
|
FString& setNumber (uInt64);
|
||||||
FString& setNumber (long);
|
FString& setNumber (lDouble, int = getPrecision<lDouble>());
|
||||||
FString& setNumber (uLong);
|
|
||||||
FString& setNumber (float, int = FLT_DIG);
|
|
||||||
FString& setNumber (double, int = DBL_DIG);
|
|
||||||
FString& setNumber (lDouble, int = LDBL_DIG);
|
|
||||||
|
|
||||||
FString& setFormatedNumber (sInt16, char = nl_langinfo(THOUSEP)[0]);
|
template <typename NumT>
|
||||||
FString& setFormatedNumber (uInt16, char = nl_langinfo(THOUSEP)[0]);
|
FString& setFormatedNumber (NumT, char = nl_langinfo(THOUSEP)[0]);
|
||||||
FString& setFormatedNumber (int, char = nl_langinfo(THOUSEP)[0]);
|
FString& setFormatedNumber (sInt64, char = nl_langinfo(THOUSEP)[0]);
|
||||||
FString& setFormatedNumber (uInt, char = nl_langinfo(THOUSEP)[0]);
|
FString& setFormatedNumber (uInt64, char = nl_langinfo(THOUSEP)[0]);
|
||||||
FString& setFormatedNumber (long, char = nl_langinfo(THOUSEP)[0]);
|
|
||||||
FString& setFormatedNumber (uLong, char = nl_langinfo(THOUSEP)[0]);
|
|
||||||
|
|
||||||
const FString& insert (const FString&, int);
|
const FString& insert (const FString&, int);
|
||||||
const FString& insert (const FString&, std::size_t);
|
const FString& insert (const FString&, std::size_t);
|
||||||
|
@ -397,44 +392,27 @@ inline wchar_t FString::back() const
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
inline FString& FString::setNumber (sInt16 num)
|
template <typename NumT>
|
||||||
{ return setNumber (long(num)); }
|
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)
|
template <typename NumT>
|
||||||
{ return setNumber (uLong(num)); }
|
inline FString& FString::setFormatedNumber (NumT num, char separator)
|
||||||
|
{
|
||||||
|
if ( isNegative(num) )
|
||||||
|
return setFormatedNumber (sInt64(num), separator);
|
||||||
|
|
||||||
//----------------------------------------------------------------------
|
return setFormatedNumber (uInt64(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); }
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace finalcut
|
} // namespace finalcut
|
||||||
|
|
|
@ -93,6 +93,15 @@ inline bool isNegative (const T& x)
|
||||||
return is_negative<T, std::numeric_limits<T>::is_signed>()(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
|
namespace fc
|
||||||
{
|
{
|
||||||
#pragma pack(push)
|
#pragma pack(push)
|
||||||
|
|
|
@ -1059,6 +1059,12 @@ void FStringTest::formatTest()
|
||||||
|
|
||||||
fnum2.setFormatedNumber(uLong(9223372036854775807), '\0');
|
fnum2.setFormatedNumber(uLong(9223372036854775807), '\0');
|
||||||
CPPUNIT_ASSERT ( fnum2 == "9 223 372 036 854 775 807" );
|
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
|
#else
|
||||||
// 32-bit architecture
|
// 32-bit architecture
|
||||||
fnum1.setFormatedNumber(0xffffffff, '\'');
|
fnum1.setFormatedNumber(0xffffffff, '\'');
|
||||||
|
@ -1072,7 +1078,25 @@ void FStringTest::formatTest()
|
||||||
|
|
||||||
fnum2.setFormatedNumber(uLong(2147483647), '\0');
|
fnum2.setFormatedNumber(uLong(2147483647), '\0');
|
||||||
CPPUNIT_ASSERT ( fnum2 == "2 147 483 647" );
|
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
|
#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()
|
void FStringTest::convertFromNumberTest()
|
||||||
{
|
{
|
||||||
constexpr sInt16 n1 = -1234;
|
constexpr sInt8 n1 = -12;
|
||||||
constexpr uInt16 n2 = 1234;
|
constexpr uInt8 n2 = 12;
|
||||||
constexpr int n3 = -12345;
|
constexpr sInt16 n3 = -1234;
|
||||||
constexpr uInt n4 = 12345;
|
constexpr uInt16 n4 = 1234;
|
||||||
constexpr long n5 = -12345678;
|
constexpr int n5 = -12345;
|
||||||
constexpr uLong n6 = 12345678;
|
constexpr uInt n6 = 12345;
|
||||||
constexpr float n7 = 1234.56f;
|
constexpr sInt32 n7 = -12345;
|
||||||
constexpr double n8 = 1234.5678;
|
constexpr uInt32 n8 = 12345;
|
||||||
constexpr lDouble n9 = 12345.67890L;
|
constexpr long n9 = -12345678;
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n1) == "-1234" );
|
constexpr uLong n10 = 12345678;
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n2) == "1234" );
|
constexpr sInt64 n11 = -12345678;
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n3) == "-12345" );
|
constexpr uInt64 n12 = 12345678;
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n4) == "12345" );
|
constexpr float n13 = 1234.56f;
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n5) == "-12345678" );
|
constexpr double n14 = 1234.5678;
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n6) == "12345678" );
|
constexpr lDouble n15 = 12345.67890L;
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n7) == "1234.56" );
|
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n8) == "1234.5678" );
|
CPPUNIT_ASSERT ( finalcut::getPrecision<float>() == FLT_DIG );
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n9) == "12345.6789" );
|
CPPUNIT_ASSERT ( finalcut::getPrecision<double>() == DBL_DIG );
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n7, 0) == "1e+03" );
|
CPPUNIT_ASSERT ( finalcut::getPrecision<lDouble>() == LDBL_DIG );
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n8, 0) == "1e+03" );
|
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n9, 0) == "1e+04" );
|
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n1) == "-12" );
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n7, 100)
|
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" );
|
== "1234.56005859375" );
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n8, 100)
|
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n14, 100)
|
||||||
== "1234.567800000000033833202905952930450439453125" );
|
== "1234.567800000000033833202905952930450439453125" );
|
||||||
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n9, 100)
|
CPPUNIT_ASSERT ( finalcut::FString().setNumber(n15, 100)
|
||||||
== "12345.67889999999999961488583721802569925785064697265625" );
|
== "12345.67889999999999961488583721802569925785064697265625" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue