[Cppunit-cvs] cppunit2/include/cpptl stringtools.h,1.3,1.4
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-02-26 11:51:34
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13904/include/cpptl Modified Files: stringtools.h Log Message: * rewrote integerToString() so that it no longer use std::string internal (portability issue with suncc). Also avoid warning with gcc about 'always false condition'. Index: stringtools.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/stringtools.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** stringtools.h 23 Feb 2005 21:38:16 -0000 1.3 --- stringtools.h 26 Feb 2005 11:51:10 -0000 1.4 *************** *** 7,37 **** 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 ) { ! // Thoses cast are necessary to resolve an ambiguity between insert( char *, unsigned, char ) ! // and insert( unsigned, unsigned, char) with Sun CC. ! s.insert( (unsigned int)0, (unsigned int)1, digits[v % 10] ); v /= 10; } ! ! if ( isSigned ) ! s.insert( 0, "-" ); } ! return s; } inline std::string toString( char c ) { --- 7,96 ---- namespace CppTL { ! inline char digit( unsigned number ) { static const char *digits = "0123456789"; ! return digits[number]; ! } ! ! template<class IntType> ! struct SignedPolicy ! { ! void makePositive( IntType &value ) ! { ! isNegative_ = value < 0; ! if ( isNegative_ ) ! value *= -1; ! } ! ! bool needMinusSign() const ! { ! return isNegative_; ! } ! ! bool isNegative_; ! }; ! ! template<class IntType> ! struct UnsignedPolicy ! { ! void makePositive( IntType & ) ! { ! } ! ! bool needMinusSign() const ! { ! return false; ! } ! }; ! ! template<class IntType, class SignPolicy> ! std::string integerToString( IntType v, SignPolicy sign ) ! { ! char buffer[32]; ! char *current = &buffer[32]; ! *--current = 0; if ( v == 0 ) ! *--current = '0'; else { ! sign.makePositive( v ); while ( v != 0 ) { ! *--current = digit( unsigned(v % 10) ); v /= 10; } ! if ( sign.needMinusSign() ) ! *--current = '-'; } ! return current; } + + //template<class IntType> + //std::string integerToString( IntType v ) + //{ + // std::string s; + // if ( v == 0 ) + // s += '0'; + // else + // { + // bool isSigned = v < 0; + // if ( isSigned ) + // v *= -1; + + // while ( v != 0 ) + // { + // // Thoses cast are necessary to resolve an ambiguity between insert( char *, unsigned, char ) + // // and insert( unsigned, unsigned, char) with Sun CC. + // s.insert( (unsigned int)0, (unsigned int)1, digits[v % 10] ); + // v /= 10; + // } + + // if ( isSigned ) + // s.insert( 0, "-" ); + // } + // return s; + //} + inline std::string toString( char c ) { *************** *** 41,70 **** 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 ); } --- 100,129 ---- inline std::string toString( int v ) { ! return integerToString( v, SignedPolicy<int>() ); } inline std::string toString( unsigned int v ) { ! return integerToString( v, UnsignedPolicy<unsigned int>() ); } inline std::string toString( long v ) { ! return integerToString( v, SignedPolicy<long>() ); } inline std::string toString( unsigned long v ) { ! return integerToString( v, UnsignedPolicy<unsigned long>() ); } inline std::string toString( short v ) { ! return integerToString( v, SignedPolicy<short>() ); } inline std::string toString( unsigned short v ) { ! return integerToString( v, UnsignedPolicy<unsigned short>() ); } *************** *** 73,82 **** inline std::string toString( int64_t v ) { ! return integerToString( v ); } inline std::string toString( uint64_t v ) { ! return integerToString( v ); } #endif --- 132,141 ---- inline std::string toString( int64_t v ) { ! return integerToString( v, SignedPolicy<int64_t>() ); } inline std::string toString( uint64_t v ) { ! return integerToString( v, UnsignedPolicy<uint64_t>() ); } #endif *************** *** 84,92 **** 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 ); --- 143,151 ---- inline std::string toString( float v ) { ! #ifdef FLT_DIG const int precision = FLT_DIG; #else const int precision = 6; ! #endif // #ifdef FLT_DIG char buffer[128]; sprintf(buffer, "%.*g", precision, v ); |