Update of /cvsroot/cppunit/cppunit2/include/cpptl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31257/include/cpptl
Added Files:
stringtools.h
Log Message:
* added toString() to Value & Properties.
--- NEW FILE: stringtools.h ---
#ifndef CPPTL_STRINGTOOLS_H_INCLUDED
# define CPPTL_STRINGTOOLS_H_INCLUDED
# include <string>
# include <float.h> // toString( double )
namespace CppTL {
template<class IntType>
std::string integerToString( IntType v )
{
static const char *digits = "0123456789";
std::string s;
if ( v == 0 )
s += '0';
else
{
bool isSigned = v < 0;
if ( isSigned )
v *= -1;
while ( v != 0 )
{
s.insert( 0, digits[v % 10], 1 );
v /= 10;
}
if ( isSigned )
s.insert( 0, "-" );
}
return s;
}
inline std::string toString( char c )
{
return std::string( 1, c );
}
inline std::string toString( int v )
{
return integerToString( v );
}
inline std::string toString( unsigned int v )
{
return integerToString( v );
}
inline std::string toString( long v )
{
return integerToString( v );
}
inline std::string toString( unsigned long v )
{
return integerToString( v );
}
inline std::string toString( short v )
{
return integerToString( v );
}
inline std::string toString( unsigned short v )
{
return integerToString( v );
}
#ifndef CPPTL_NO_INT64
inline std::string toString( int64_t v )
{
return integerToString( v );
}
inline std::string toString( uint64_t v )
{
return integerToString( v );
}
#endif
inline std::string toString( float v )
{
#ifdef DBL_DIG
const int precision = FLT_DIG;
#else
const int precision = 6;
#endif // #ifdef DBL_DIG
char buffer[128];
sprintf(buffer, "%.*g", precision, v );
return buffer;
}
inline std::string toString( double v )
{
#ifdef DBL_DIG
const int precision = DBL_DIG;
#else
const int precision = 15;
#endif // #ifdef DBL_DIG
char buffer[128];
sprintf(buffer, "%.*g", precision, v );
return buffer;
}
inline std::string toString( long double v )
{
#ifdef DBL_DIG
const int precision = DBL_DIG;
#else
const int precision = 15;
#endif // #ifdef DBL_DIG
char buffer[128];
sprintf(buffer, "%.*g", precision, v );
return buffer;
}
inline std::string toString( bool v )
{
return v ? "true" : "false";
}
} // namespace CppTL
#endif // CPPTL_STRINGTOOLS_H_INCLUDED
|