[Cppunit-cvs] cppunit2/include/cpptl stringtools.h,1.4,1.5
Brought to you by:
blep
From: Baptiste L. <bl...@us...> - 2005-02-27 10:13:27
|
Update of /cvsroot/cppunit/cppunit2/include/cpptl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1890/include/cpptl Modified Files: stringtools.h Log Message: * use ConstString instead of std::string * moved quoteString() from cpput/assertstring to cpptl/stringtools.h. Index: stringtools.h =================================================================== RCS file: /cvsroot/cppunit/cppunit2/include/cpptl/stringtools.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** stringtools.h 26 Feb 2005 11:51:10 -0000 1.4 --- stringtools.h 27 Feb 2005 10:13:18 -0000 1.5 *************** *** 2,6 **** # define CPPTL_STRINGTOOLS_H_INCLUDED ! # include <string> # include <float.h> // toString( double ) --- 2,6 ---- # define CPPTL_STRINGTOOLS_H_INCLUDED ! # include <cpptl/conststring.h> # include <float.h> // toString( double ) *************** *** 13,16 **** --- 13,22 ---- } + inline char hexaDigit( unsigned number ) + { + static const char *digit = "0123456789abcdef"; + return digit[number]; + } + template<class IntType> struct SignedPolicy *************** *** 45,49 **** template<class IntType, class SignPolicy> ! std::string integerToString( IntType v, SignPolicy sign ) { char buffer[32]; --- 51,55 ---- template<class IntType, class SignPolicy> ! CppTL::ConstString integerToString( IntType v, SignPolicy sign ) { char buffer[32]; *************** *** 67,74 **** //template<class IntType> ! //std::string integerToString( IntType v ) //{ ! // std::string s; // if ( v == 0 ) // s += '0'; --- 73,100 ---- + template<class UnsignedIntegerType> + CppTL::ConstString + integerToHexaString( UnsignedIntegerType value ) + { + const int bufferLength = sizeof(UnsignedIntegerType) * 2 + 1; + char buffer[ bufferLength ]; + char *current = &buffer[ bufferLength ]; + char *end = current; + *--current = 0; + do + { + *--current += hexaDigit( size_type(value & 15) ); + value /= 16; + } + while ( value != 0 ); + + return CppTL::ConstString( current, end ); + } + + //template<class IntType> ! //CppTL::ConstString integerToString( IntType v ) //{ ! // CppTL::ConstString s; // if ( v == 0 ) // s += '0'; *************** *** 93,127 **** //} ! inline std::string toString( char c ) { ! return std::string( 1, c ); } ! 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>() ); --- 119,153 ---- //} ! inline CppTL::ConstString toString( char c ) { ! return CppTL::ConstString( &c, 1 ); } ! inline CppTL::ConstString toString( int v ) { return integerToString( v, SignedPolicy<int>() ); } ! inline CppTL::ConstString toString( unsigned int v ) { return integerToString( v, UnsignedPolicy<unsigned int>() ); } ! inline CppTL::ConstString toString( long v ) { return integerToString( v, SignedPolicy<long>() ); } ! inline CppTL::ConstString toString( unsigned long v ) { return integerToString( v, UnsignedPolicy<unsigned long>() ); } ! inline CppTL::ConstString toString( short v ) { return integerToString( v, SignedPolicy<short>() ); } ! inline CppTL::ConstString toString( unsigned short v ) { return integerToString( v, UnsignedPolicy<unsigned short>() ); *************** *** 130,139 **** #ifndef CPPTL_NO_INT64 ! 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>() ); --- 156,165 ---- #ifndef CPPTL_NO_INT64 ! inline CppTL::ConstString toString( int64_t v ) { return integerToString( v, SignedPolicy<int64_t>() ); } ! inline CppTL::ConstString toString( uint64_t v ) { return integerToString( v, UnsignedPolicy<uint64_t>() ); *************** *** 141,145 **** #endif ! inline std::string toString( float v ) { #ifdef FLT_DIG --- 167,171 ---- #endif ! inline CppTL::ConstString toString( float v ) { #ifdef FLT_DIG *************** *** 153,157 **** } ! inline std::string toString( double v ) { #ifdef DBL_DIG --- 179,183 ---- } ! inline CppTL::ConstString toString( double v ) { #ifdef DBL_DIG *************** *** 165,169 **** } ! inline std::string toString( long double v ) { #ifdef DBL_DIG --- 191,195 ---- } ! inline CppTL::ConstString toString( long double v ) { #ifdef DBL_DIG *************** *** 177,185 **** } ! inline std::string toString( bool v ) { return v ? "true" : "false"; } } // namespace CppTL --- 203,271 ---- } ! inline CppTL::ConstString toString( bool v ) { return v ? "true" : "false"; } + + inline CppTL::ConstString toHexaString( unsigned int v ) + { + return integerToHexaString( v ); + } + + + #ifndef CPPTL_NO_INT64 + inline CppTL::ConstString toHexaString( uint64_t v ) + { + return integerToHexaString( v ); + } + #endif + + + inline CppTL::ConstString + escapeControl( char c ) + { + if ( c == '\n' ) + return "\\n"; + else if ( c == '\t' ) + return "\\t"; + else if ( c == '\r' ) + return "\\r"; + else if ( c == '\v' ) + return "\\v"; + return "\\x" + toHexaString( unsigned(c) ); + } + + + inline CppTL::ConstString + escape( char c ) + { + if ( c >=0 && c < 32 ) + return escapeControl( c ); + else if ( c == '\\' ) + return "\\\\"; + return CppTL::ConstString( &c, 1 ); + } + + + // need to expose some of those utility functions... + inline CppTL::ConstString + escape( const CppTL::ConstString &text ) + { + CppTL::StringBuffer escaped( size_type(text.length() * 1.2) + 64 ); + CppTL::ConstString::const_iterator it = text.begin(); + while ( it != text.end() ) + escaped += escape( *it++ ); + return escaped; + } + + + inline CppTL::ConstString + quoteString( const CppTL::ConstString &str ) + { + return "\"" + escape(str) + "\""; + } + + } // namespace CppTL |