#include "BwString.h" #include <string>
void BW_Util::BwString::init() { currLen = 0; maxLen = 3; growSize = 3; pw = (wchar_t*)calloc(maxLen, sizeof(wchar_t)); }
BW_Util::BwString::BwString(void) { init(); }
BW_Util::BwString::BwString( const char * pStr ) { init(); (*this) = pStr; }
BW_Util::BwString::BwString( const wchar_t * pStr ) { init(); (*this) = pStr; }
BW_Util::BwString::BwString( const wchar_t * pStr1, const wchar_t * pStr2 ) { init(); (*this) = pStr1; Append(pStr2); }
BW_Util::BwString::BwString( const char * pStr1, const char * pStr2 ) { init(); (*this) = pStr1; Append(pStr2); }
BW_Util::BwString::BwString( const wchar_t * pStr1, const char * pStr2 ) { init(); (*this) = pStr1; Append(pStr2); }
BW_Util::BwString::BwString( const char * pStr1, const wchar_t * pStr2 ) { init(); (*this) = pStr1; Append(pStr2); }
BW_Util::BwString::~BwString(void) { if(pw) { free(pw); pw = 0; } }
int BW_Util::BwString::Length() { return currLen; }
wchar_t* BW_Util::BwString::operator=(const char* pStr ) { memset(pw, '\0', maxLen); currLen = 0; Append(pStr); return pw; }
wchar_t* BW_Util::BwString::operator=( const wchar_t* pStr ) { memset(pw, '\0', maxLen); currLen = 0; Append(pStr); return pw; }
BW_Util::BwString& BW_Util::BwString::operator=( BwString& str ) { //此时调用了重载符:char* EcpString::operator=(const char* pStr )
(*this) = str.pw; return str; }
BW_Util::BwString::operator wchar_t*() { return pw; }
wchar_t* BW_Util::BwString::operator+=( const char* pStr ) { Append(pStr); return (*this); }
wchar_t* BW_Util::BwString::operator+=( const wchar_t* pStr ) { Append(pStr); return (*this); }
BW_Util::BwString& BW_Util::BwString::operator+=( BwString& str ) { Append(str.pw); return (*this); }
template <class T> wchar_t* BW_Util::BwString::Append( const T* pVal ) { int len = CharsCount(pVal); if(len + currLen >= maxLen ) { while( len + currLen >= maxLen) { maxLen += growSize; } wchar_t * pwTemp = (wchar_t*)calloc(maxLen, sizeof(wchar_t)); memcpy(pwTemp, pw, currLen * sizeof(wchar_t)); free(pw); pw = pwTemp; } for(int i=currLen, j = 0; i<len + currLen; i++, j++) { pw[i] = pVal[j]; }
currLen += len;
return pw; }
bool BW_Util::BwString::operator<( BwString& str ) { for(int i=0; i<currLen; i++) { if(pw[i] < str.pw[i]) return true; } return false; }
bool BW_Util::BwString::operator>( BwString& str ) { for(int i=0; i<currLen; i++) { if(pw[i] > str.pw[i]) return true; } return false; }
template <class T> int BW_Util::BwString::CharsCount( const T* pStr ) { int i=0; for(; pStr[i]; i++) {} return i; }
BW_Util::BwString BW_Util::operator+( BwString& str, const char* pStr ) { //这里直接调用构造函数,而不是实例一个对象后在返回这个对象,是为了避免调用拷贝构造函数
return BwString(str.pw, pStr); }
BW_Util::BwString BW_Util::operator+( BwString& str, const wchar_t* pStr ) { return BwString(str.pw, pStr); }
BW_Util::BwString BW_Util::operator+( BwString& str1, BwString& str2 ) { return BwString(str1.pw, str2.pw); }
|