stringutils.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 2,672 行 · 第 1/5 页

CPP
2,672
字号
//StringUtils.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/FoundationKit/FoundationKit.h"#include "vcf/FoundationKit/FoundationKitPrivate.h"#include "vcf/FoundationKit/DateTime.h"#if defined(VCF_OSX) || defined(VCF_MINGW)     #include <cxxabi.h>  //add this so we can demangle the GCC typeinfo names#endif#ifdef VCF_POSIX    #include <cxxabi.h>  //add this so we can demangle the GCC typeinfo names	#include <uuid/uuid.h>#endif#define TO_STRING_TXT_SIZE		50using namespace VCF;String StringUtils::weekdays[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };String StringUtils::abbrevWeekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };String StringUtils::months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };String StringUtils::abbrevMonths[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };/*void StringUtils::traceWithArgs( String text, ... ){	text = StringUtils::convertFormatString( text );//#ifdef _DEBUG	va_list argList;	va_start( argList, text );     // Initialize variable arguments.	VCFChar* buf = new VCFChar[MAX_TRACE_STRING];	memset( buf, 0, MAX_TRACE_STRING*sizeof(VCFChar) );#if defined(VCF_GCC) || defined(VCF_CW)    #ifdef VCF_OSX    CFMutableStringRef fmt = CFStringCreateMutable( NULL, 0 );	CFStringAppendCharacters( fmt, text.c_str(), text.size() );    CFStringRef res = CFStringCreateWithFormatAndArguments( NULL, NULL, fmt, argList );    int length = minVal<uint32>( MAX_TRACE_STRING-1, CFStringGetLength( res ) );    CFRange range = {0, length };    CFStringGetCharacters( res, range, buf );    CFRelease( res );    CFRelease( fmt );    #else      vswprintf( buf, MAX_TRACE_STRING, text.c_str(), argList );    #endif#else	_vsnwprintf( buf, MAX_TRACE_STRING, text.c_str(), argList );#endif	va_end( argList );              // Reset variable arguments.	//StringUtils::trace( String("WARNING: Using deprecated function!!! - StringUtils::traceWithArgs(...)\n") ); // MP-	StringUtils::trace( String(buf) );	delete [] buf;//#endif}*/void StringUtils::traceWithArgs( const Format& formatter ){	StringUtils::trace( formatter );}void StringUtils::trace( const String& text ){#ifdef _DEBUG#ifdef WIN32	VCFWin32::Win32Utils::trace( text );#else  #ifdef VCF_OSX    CFTextString tmp;    tmp = text;    CFShow( tmp );  #else    wprintf( text.c_str() );  #endif#endif#endif}String StringUtils::trimLeft( const String& text, const char& c ){	String result = text;	unsigned int n=0;	for (; n<result.length(); ++n) {		if (result[n] != c) {			break;		}	}	result.erase(0,n);	return result;}String StringUtils::trimRight( const String& text, const char& c ){	String result = text;	for (int n=result.length()-1; 0<=n; --n) {		if (result[n] != c) {			break;		}		result.erase(n,1);	}	return result;}String StringUtils::trim( const String& text, const char& c ){	String result;	result = trimLeft(text, c);	result = trimRight(result, c);	return result;}void StringUtils::trimWhiteSpacesLeft( String& text ){	for (unsigned int n=0; n<text.length(); ++n) {		if (text[0] == ' ' || text[0] == '\t'|| text[0] == '\r'|| text[0] == '\n' ) {			text.erase(0,1);		} else {			break;		}	}}void StringUtils::trimWhiteSpacesRight( String& text ){	for (int n=text.length()-1; 0<=n; --n) {		if (text[n] == ' ' || text[n] == '\t' || text[n] == '\r' || text[n] == '\n' ) {			//text[n]=0;			// error: this doesn't update text.size()			text.erase(n,1);		} else {			break;		}	}}void StringUtils::trimWhiteSpaces( String& text ){	trimWhiteSpacesLeft(text);	trimWhiteSpacesRight(text);}String StringUtils::eraseLeftOfChar( const String& s, const VCFChar& ch, const bool& included/*=false*/, const int& count/*=1*/ ){	String str = s;	String::size_type pos;	int cnt = count;	if ( cnt < 0 ) {		pos = s.rfind( ch );		while ( ++cnt != 0 ) {			pos = s.find( ch, pos );		}	} else {		pos = s.find( ch );		while ( --cnt != 0 ) {			pos = s.find( ch, pos );		}	}	if ( String::npos != pos ) {		if ( included ) {			str.erase( 0, pos + 1 );		} else {			str.erase( 0, pos );		}	}	return str;}String StringUtils::eraseRightOfChar( const String& s, const VCFChar& ch, const bool& included/*=false*/, const int& count/*=1*/ ){	String str = s;	String::size_type pos;	int cnt = count;	if ( cnt < 0 ) {		pos = s.rfind( ch );		while ( ++cnt != 0 ) {			pos = s.find( ch, pos );		}	} else {		pos = s.find( ch );		while ( --cnt != 0 ) {			pos = s.find( ch, pos );		}	}	if ( String::npos != pos ) {		if ( included ) {			str.erase( pos, str.size() - pos );		} else {			str.erase( pos + 1, str.size() - pos - 1 );		}	}	return str;}String StringUtils::lowerCase( const String& text ){	String result;#if defined(VCF_MSC) || defined(VCF_BCC)	VCFChar* copyText = new VCFChar[text.size()+1];	memset(copyText, 0, (text.size()+1)*sizeof(VCFChar) );	text.copy( copyText, text.size() );	_wcslwr( copyText ); // not in ANSI standard library	result = copyText;	delete [] copyText;	#elif defined(VCF_CW_W32) || defined(VCF_GCC)	VCFChar* copyText = new VCFChar[text.size()+1];	memset(copyText, 0, (text.size()+1)*sizeof(VCFChar) );	text.copy( copyText, text.size() );	for (int n=0; n<text.size(); n++)	{		copyText[n] = std::towlower(copyText[n]);	}		result = copyText;	delete [] copyText;#elif VCF_OSX	CFTextString tmp;	tmp = text;	tmp.lowerCase();	result = tmp;#endif	return result;}String StringUtils::upperCase( const VCF::String& text ){	String result;#if defined(VCF_MSC) || defined(VCF_BCC)	VCFChar* copyText = new VCFChar[text.size()+1];	memset(copyText, 0, (text.size()+1)*sizeof(VCFChar) );	text.copy( copyText, text.size() );	_wcsupr( copyText );	result = copyText;	delete [] copyText;#elif defined(VCF_CW_W32) || defined(VCF_GCC)	VCFChar* copyText = new VCFChar[text.size()+1];	memset(copyText, 0, (text.size()+1)*sizeof(VCFChar) );	text.copy( copyText, text.size() );	for (int n=0; n<text.size(); n++)	{		copyText[n] = std::towupper(copyText[n]);	}		result = copyText;	delete [] copyText;#elif VCF_OSX	CFTextString tmp;	tmp = text;	tmp.upperCase();	result = tmp;#endif	return result;}int StringUtils::noCaseCompare( const VCF::String& str1, const VCF::String& str2 ){	int result = 0;#if defined(VCF_OSX)	CFTextString tmp1(str1);		CFTextString tmp2(str2);	CFComparisonResult cmpRes = CFStringCompare( tmp1, tmp2, kCFCompareCaseInsensitive );	switch ( cmpRes ) {		case kCFCompareLessThan : {			result = -1;		}		break;				case kCFCompareEqualTo : {			result = 0;		}		break;				case kCFCompareGreaterThan : {			result = 1;		}		break;	}#elif defined(WIN32)	int cmpRes = CSTR_EQUAL;	if ( System::isUnicodeEnabled() ) {		cmpRes = ::CompareStringW( GetThreadLocale(), NORM_IGNORECASE, str1.c_str(), str1.size(), str2.c_str(), str2.size() );			}	else {		AnsiString tmp1 = str1;		AnsiString tmp2 = str2;		cmpRes = ::CompareStringA( GetThreadLocale(), NORM_IGNORECASE, tmp1.c_str(), tmp1.size(), tmp2.c_str(), tmp2.size() );	}	switch ( cmpRes ) {		case CSTR_LESS_THAN : {			result = -1;		}		break;				case CSTR_EQUAL : {			result = 0;		}		break;				case CSTR_GREATER_THAN : {			result = 1;		}		break;	}#else	String s1 = StringUtils::upperCase(str1);	String s2 = StringUtils::upperCase(str2);		if ( s1 < s2 ) {		result = -1;	}	else if ( s1 > s2 ) {		result = 1;	}#endif	return result;}VCF::String StringUtils::toStringFromHexNumber( const uchar& value ){	VCF::String result;#ifdef VCF_OSX	CFTextString cfTmp;	cfTmp.format( CFSTR("%02X"), value );	result = cfTmp;#else	VCFChar hexBytes[50];	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)	swprintf( hexBytes, sizeof(hexBytes)-1, L"%02X", value  );#else	swprintf( hexBytes, L"%02X", value );	#endif	result = hexBytes;#endif	return result;}VCF::String StringUtils::toString( const int& value ){#ifdef VCF_OSX	CFTextString cfTmp;	CFStringRef s = CFStringCreateWithFormat( NULL, NULL, CFSTR( STR_INT_CONVERSION ), value );	cfTmp = s;	CFRelease( s );	return String( cfTmp );#else	VCFChar tmp[TO_STRING_TXT_SIZE];	memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) );	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)		swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, W_STR_INT_CONVERSION, value  );	#else		swprintf( tmp, W_STR_INT_CONVERSION, value );	#endif	return String( tmp );#endif}VCF::String StringUtils::toString( const VCF::uint32& value ){#ifdef VCF_OSX	CFTextString cfTmp;	CFStringRef s = CFStringCreateWithFormat( NULL, NULL, CFSTR( STR_UINT_CONVERSION ), value );	cfTmp = s;	CFRelease( s );	return String( cfTmp );#else	VCFChar tmp[TO_STRING_TXT_SIZE];	memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) );	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)		swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, W_STR_UINT_CONVERSION, value  );	#else		swprintf( tmp, W_STR_UINT_CONVERSION, value );	#endif	return String( tmp );#endif}VCF::String StringUtils::toString( const long& value ){#ifdef VCF_OSX	CFTextString cfTmp;	CFStringRef s = CFStringCreateWithFormat( NULL, NULL, CFSTR( STR_LONG_CONVERSION ), value );	cfTmp = s;	CFRelease( s );	return String( cfTmp );#else	VCFChar tmp[TO_STRING_TXT_SIZE];	memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) );	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)		swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, W_STR_LONG_CONVERSION, value  );	#else		swprintf( tmp, W_STR_LONG_CONVERSION, value );	#endif	return String( tmp );#endif}VCF::String StringUtils::toString( const VCF::ulong32& value ){#ifdef VCF_OSX	CFTextString cfTmp;	CFStringRef s = CFStringCreateWithFormat( NULL, NULL, CFSTR( STR_ULONG_CONVERSION ), value );	cfTmp = s;	CFRelease( s );	return String( cfTmp );#else	VCFChar tmp[TO_STRING_TXT_SIZE];	memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) );	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)		swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, W_STR_ULONG_CONVERSION, value  );	#else		swprintf( tmp, W_STR_ULONG_CONVERSION, value );	#endif	return String( tmp );#endif}VCF::String StringUtils::toString( const VCF::long64& value ){#ifdef VCF_OSX	CFTextString cfTmp;	CFStringRef s;	// if ( 0 != valHi )	// s = CFStringCreateWithFormat( NULL, NULL, CFSTR( "%ld%ld" ), value.hi(), value.lo() );	// would be a wrong implementation	s = CFStringCreateWithFormat( NULL, NULL, CFSTR( STR_ULONG_CONVERSION ), value.lo() );	cfTmp = s;	CFRelease( s );	return String( cfTmp );#else	VCFChar tmp[TO_STRING_TXT_SIZE];	memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) );	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)		// if ( 0 != valHi )		// swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, L"%lu%lu", value.hi(), value.lo() );		// would be a wrong implementation		swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, W_STR_ULONG_CONVERSION, (int)value.lo() );	#else		swprintf( tmp, L"%I64d", (__int64)value );	#endif	return String( tmp );#endif}VCF::String StringUtils::toString( const VCF::ulong64& value ){#ifdef VCF_OSX	CFTextString cfTmp;	CFStringRef s;	// if ( 0 != valHi )	// s = CFStringCreateWithFormat( NULL, NULL, CFSTR( "%lu%lu" ), value.hi(), value.lo() );	// would be a wrong implementation	s = CFStringCreateWithFormat( NULL, NULL, CFSTR( STR_ULONG_CONVERSION ), value.lo() );	cfTmp = s;	CFRelease( s );	return String( cfTmp );#else	VCFChar tmp[TO_STRING_TXT_SIZE];	memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) );	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)		// if ( 0 != valHi )		// swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, L"%lu%lu", value.hi(), value.lo() );		// would be a wrong implementation		swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, W_STR_ULONG_CONVERSION, (int)value.lo() );	#else		swprintf( tmp, L"%I64u", (unsigned __int64)value );	#endif	return String( tmp );#endif}VCF::String StringUtils::toString( const float& value ){#ifdef VCF_OSX	CFTextString cfTmp;	CFStringRef s = CFStringCreateWithFormat( NULL, NULL, CFSTR( "%.5f" ), value );	cfTmp = s;	CFRelease( s );	return String( cfTmp );#else	VCFChar tmp[TO_STRING_TXT_SIZE];	memset( tmp, 0, TO_STRING_TXT_SIZE * sizeof(VCFChar) );	#if defined(VCF_POSIX) || defined(VCF_CW_W32) || defined(VCF_DMC)		swprintf( tmp, sizeof(tmp)/sizeof(VCFChar)-1, L"%.5f", value  );	#else		swprintf( tmp, L"%.5f", value );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?