dwstring.cpp.svn-base
来自「ffshow源码」· SVN-BASE 代码 · 共 2,010 行 · 第 1/4 页
SVN-BASE
2,010 行
size_t len2 = aLen2; size_t len = std::min(len1, len2); int r = strncmp(buf1, buf2, len); if (r == 0) { if (len1 < len2) r = -1; else if (len1 > len2) { r = 1; } } return r;}template<class tchar> DwString<tchar>& DwString<tchar>::ConvertToLowerCase(){ if (mRep->mRefCount > 1) { _copy(); } tchar* buf = mRep->mBuffer + mStart; for (size_t i=0; i < mLength; ++i) { buf[i] = (tchar) tolower(buf[i]); } return *this;}template<class tchar> DwString<tchar>& DwString<tchar>::ConvertToUpperCase(){ if (mRep->mRefCount > 1) { _copy(); } tchar* buf = mRep->mBuffer + mStart; for (size_t i=0; i < mLength; ++i) { buf[i] = (tchar) toupper(buf[i]); } return *this;}template<class tchar> DwString<tchar>& DwString<tchar>::Trim(){ const tchar* buf = mRep->mBuffer + mStart; size_t i = 0; while (mLength > 0) { if (isspace(buf[i])) { ++mStart; --mLength; ++i; } else { break; } } buf = mRep->mBuffer + mStart; i = mLength - 1; while (mLength > 0) { if (isspace(buf[i])) { --mLength; --i; } else { break; } } if (mLength == 0) { assign(_L("")); } return *this; }/*void DwString<tchar>::WriteTo(std::ostream& aStrm) const{ const char* buf = mRep->mBuffer + mStart; for (size_t i=0; i < mLength; ++i) { aStrm << buf[i]; }}*/template<class tchar> void DwString<tchar>::TakeBuffer(tchar* aBuf, size_t aSize, size_t aStart, size_t aLen){ assert(aBuf != 0); DwStringRep<tchar>* rep = new DwStringRep<tchar>(aBuf, aSize); assert(rep != 0); if (rep) { delete_rep_safely(mRep); mRep = rep; mStart = aStart; mLength = aLen; }}template<class tchar> void DwString<tchar>::ReleaseBuffer(tchar** aBuf, size_t* aSize, size_t* aStart, size_t* aLen){ assert(aBuf != 0); assert(aSize != 0); assert(aStart != 0); assert(aLen != 0); if (mRep->mRefCount == 1) { *aBuf = mRep->mBuffer; *aSize = mRep->mSize; } else { size_t size = mRep->mSize; tchar* buf = new tchar [size]; assert(buf != 0); if (buf != 0) { mem_copy(mRep->mBuffer, size, buf); *aBuf = buf; *aSize = size; } else { // If not throwing an exception, recover as best we can *aBuf = 0; *aSize = 0; *aStart = mStart = 0; *aLen = mLength = 0; return; } } *aStart = mStart; *aLen = mLength; mRep = new_rep_reference(sEmptyRep); mStart = 0; mLength = 0;}template<class tchar> void DwString<tchar>::CopyTo(DwString<tchar>* aStr) const{ assert(aStr != 0); if (!aStr) return; size_t len = mLength; size_t size = len + 1; tchar* buf = mem_alloc<tchar>(&size); assert(buf != 0); if (buf != 0) { mem_copy(mRep->mBuffer+mStart, len, buf); buf[len] = 0; DwStringRep<tchar>* rep = new DwStringRep<tchar>(buf, size); assert(rep != 0); if (rep != 0) { aStr->mRep = rep; delete_rep_safely(aStr->mRep); aStr->mStart = 0; aStr->mLength = len; } }}template<class tchar> void DwString<tchar>::_copy(){ if (mRep->mRefCount > 1) { size_t size = mLength + 1; tchar* newBuf = mem_alloc<tchar>(&size); assert(newBuf != 0); if (newBuf != 0) { tchar* to = newBuf; const tchar* from = mRep->mBuffer + mStart; mem_copy(from, mLength, to); to[mLength] = 0; DwStringRep<tchar>* rep = new DwStringRep<tchar>(newBuf, size); assert(rep != 0); if (rep != 0) { delete_rep_safely(mRep); mRep = rep; mStart = 0; } else /* if (rep == 0) */ { mem_free(newBuf); mLength = 0; } } else /* if (newBuf == 0) */ { mLength = 0; } }}template<class tchar> void DwString<tchar>::_replace(size_t aPos1, size_t aLen1, const tchar* aBuf, size_t aLen2){ assert(aPos1 <= mLength); assert(aBuf != 0); size_t pos1 = std::min(aPos1, mLength); size_t len1 = std::min(aLen1, mLength - pos1); assert(mStart + mLength - len1 < ((size_t)-1) - aLen2); size_t len2 = std::min(aLen2, ((size_t)-1) - (mStart + mLength - len1)); size_t i; tchar* to; const tchar* from; size_t newLen = (mLength - len1) + len2; // Is new string empty? if (newLen == 0 || aBuf == 0) { if (mRep != sEmptyRep) { delete_rep_safely(mRep); mRep = new_rep_reference(sEmptyRep); mStart = 0; mLength = 0; } } // Is buffer shared? Is buffer too small? else if (mRep->mRefCount > 1 || newLen >= mRep->mSize) { size_t size = newLen + 1; tchar* newBuf = mem_alloc<tchar>(&size); assert(newBuf != 0); if (newBuf != 0) { to = newBuf; from = mRep->mBuffer + mStart; for (i=0; i < pos1; ++i) *to++ = *from++; from = aBuf; for (i=0; i < len2; ++i) *to++ = *from++; from = mRep->mBuffer + mStart + pos1 + len1; for (i=0; i < mLength - pos1 - len1; ++i) *to++ = *from++; *to = 0; DwStringRep<tchar>* rep = new DwStringRep<tchar>(newBuf, size); assert(rep != 0); if (rep != 0) { delete_rep_safely(mRep); mRep = rep; mStart = 0; mLength = newLen; } } } // Is the replacement smaller than the replaced? else if (len2 < len1) { to = mRep->mBuffer + mStart + pos1; from = aBuf; for (i=0; i < len2; ++i) *to++ = *from++; from = mRep->mBuffer + mStart + pos1 + len1; for (i=0; i < mLength - pos1 - len1; ++i) *to++ = *from++; *to = 0; mLength = newLen; } // Is there enough room at end of buffer? else if (mStart + newLen < mRep->mSize) { to = mRep->mBuffer + mStart + newLen; from = mRep->mBuffer + mStart + mLength - 1; *to-- = 0; for (i=0; i < mLength-pos1-len1; ++i) *to-- = *from--; from = aBuf + (len2 - 1); for (i=0; i < len2; ++i) *to-- = *from--; mLength = newLen; } // Is there enough room at beginning of buffer? else if (len2 - len1 <= mStart) { to = mRep->mBuffer + mStart - (len2 - len1); from = mRep->mBuffer + mStart; for (i=0; i < pos1; ++i) *to++ = *from++; from = aBuf; for (i=0; i < len2; ++i) *to++ = *from++; mStart -= len2 - len1; mLength = newLen; } // There's enough room, but we must move characters. else { to = mRep->mBuffer + newLen; from = mRep->mBuffer + mStart + mLength - 1; *to-- = 0; for (i=0; i < mLength-pos1-len1; ++i) *to-- = *from--; to = mRep->mBuffer; from = mRep->mBuffer + mStart; for (i=0; i < pos1; ++i) *to++ = *from++; from = aBuf; for (i=0; i < len2; ++i) *to++ = *from++; mStart = 0; mLength = newLen; }}template<class tchar> void DwString<tchar>::_replace(size_t aPos1, size_t aLen1, size_t aLen2, tchar aChar){ assert(aPos1 <= mLength); size_t pos1 = std::min(aPos1, mLength); size_t len1 = std::min(aLen1, mLength - pos1); assert(mStart + mLength - len1 < ((size_t)-1) - aLen2); size_t len2 = std::min(aLen2, ((size_t)-1) - (mStart + mLength - len1)); size_t i; tchar* to; const tchar* from; size_t newLen = mLength - len1 + len2; // Is new string empty? if (newLen == 0) { if (mRep != sEmptyRep) { delete_rep_safely(mRep); mRep = new_rep_reference(sEmptyRep); mStart = 0; mLength = 0; } } // Is buffer shared? Is buffer too small? else if (mRep->mRefCount > 1 || newLen >= mRep->mSize) { size_t size = newLen + 1; tchar* newBuf = mem_alloc<tchar>(&size); assert(newBuf != 0); if (newBuf != 0) { to = newBuf; from = mRep->mBuffer + mStart; for (i=0; i < pos1; ++i) *to++ = *from++; for (i=0; i < len2; ++i) *to++ = aChar; from = mRep->mBuffer + mStart + pos1 + len1; for (i=0; i < mLength - pos1 - len1; ++i) *to++ = *from++; *to = 0; DwStringRep<tchar>* rep = new DwStringRep<tchar>(newBuf, size); assert(rep != 0); if (rep != 0) { delete_rep_safely(mRep); mRep = rep; mStart = 0; mLength = newLen; } } } // Is the replacement smaller than the replaced? else if (len2 < len1) { to = mRep->mBuffer + mStart + pos1; for (i=0; i < len2; ++i) *to++ = aChar; from = mRep->mBuffer + mStart + pos1 + len1; for (i=0; i < mLength - pos1 - len1; ++i) *to++ = *from++; *to = 0; mLength = newLen; } // Is there enough room at end of buffer? else if (mStart + newLen < mRep->mSize) { to = mRep->mBuffer + mStart + newLen; from = mRep->mBuffer + mStart + mLength - 1; *to-- = 0; for (i=0; i < mLength-pos1-len1; ++i) *to-- = *from--; for (i=0; i < len2; ++i) *to-- = aChar; mLength = newLen; } // Is there enough room at beginning of buffer? else if (len2 - len1 <= mStart) { to = mRep->mBuffer + mStart - (len2 - len1); from = mRep->mBuffer + mStart; for (i=0; i < pos1; ++i) *to++ = *from++; for (i=0; i < len2; ++i) *to++ = aChar; mStart -= len2 - len1; mLength = newLen; } // There's enough room, but we must move characters. else { to = mRep->mBuffer + newLen; from = mRep->mBuffer + mStart + mLength - 1; *to-- = 0; for (i=0; i < mLength-pos1-len1; ++i) *to-- = *from--; to = mRep->mBuffer; from = mRep->mBuffer + mStart; for (i=0; i < pos1; ++i) *to++ = *from++; for (i=0; i < len2; ++i) *to++ = aChar; mStart = 0; mLength = newLen; }}/*#if defined (DW_DEBUG_VERSION)void DwString<tchar>::PrintDebugInfo(std::ostream& aStrm) const{ aStrm << "----------------- Debug info for DwString<tchar> class ----------------\n"; aStrm << "Id: " << ClassName() << ", " << ObjectId() << "\n"; aStrm << "Rep: " << (void*) mRep << "\n"; aStrm << "Buffer: " << (void*) mRep->mBuffer << "\n"; aStrm << "Buffer size: " << mRep->mSize << "\n"; aStrm << "Start: " << mStart << "\n"; aStrm << "Length: " << mLength << "\n"; aStrm << "Contents: "; for (size_t i=0; i < mLength && i < 64; ++i) { aStrm << mRep->mBuffer[mStart+i]; } aStrm << endl;}#elsevoid DwString<tchar>::PrintDebugInfo(std::ostream& ) const {}#endif // defined (DW_DEBUG_VERSION)*/template<class tchar> DwString<tchar> operator + (const DwString<tchar>& aStr1, const DwString<tchar>& aStr2){ DwString<tchar> str(aStr1); str.append(aStr2); return str;}template<class tchar> DwString<tchar> operator + (const tchar* aCstr, const DwString<tchar>& aStr2){ DwString<tchar> str(aCstr); str.append(aStr2); return str;}template<class tchar> DwString<tchar> operator + (tchar aChar, const DwString<tchar>& aStr2){ DwString<tchar> str(1, aChar); str.append(aStr2); return str;}template<class tchar> DwString<tchar> operator + (const DwString<tchar>& aStr1, const tchar* aCstr){ DwString<tchar> str(aStr1); str.append(aCstr); return str;}template<class tchar> DwString<tchar> operator + (const DwString<tchar>& aStr1, tchar aChar){ DwString<tchar> str(aStr1); str.append(1, aChar); return str;}template<class tchar> bool operator == (const DwString<tchar>& aStr1, const DwString<tchar>& aStr2){ const tchar* s1 = aStr1.data(); size_t len1 = aStr1.length(); const tchar* s2 = aStr2.data(); size_t len2 = aStr2.length(); int r = dw_strcmp(s1, len1, s2, len2); r = (r == 0) ? 1 : 0; return !!r;}template<class tchar> bool operator == (const DwString<tchar>& aStr1, const tchar* aCstr){ assert(aCstr != 0); const tchar* s1 = aStr1.data(); size_t len1 = aStr1.length(); const tchar* s2 = aCstr; size_t len2 = (aCstr) ? strlen(aCstr) : 0; int r = dw_strcmp(s1, len1, s2, len2); r = (r == 0) ? 1 : 0; return !!r;}template<class tchar> bool operator == (const tchar* aCstr, const DwString<tchar>& aStr2){ assert(aCstr != 0); const tchar* s1 = aCstr; size_t len1 = (aCstr) ? strlen(aCstr) : 0; const tchar* s2 = aStr2.data(); size_t len2 = aStr2.length(); int r = dw_strcmp(s1, len1, s2, len2); r = (r == 0) ? 1 : 0; return !!r;}template<class tchar> bool operator != (const DwString<tchar>& aStr1, const DwString<tchar>& aStr2){ const tchar* s1 = aStr1.data(); size_t len1 = aStr1.length(); const tchar* s2 = aStr2.data(); size_t len2 = aStr2.length(); int r = dw_strcmp(s1, len1, s2, len2); r = (r == 0) ? 0 : 1; return !!r;}template<class tchar> bool operator != (const DwString<tchar>& aStr1, const tchar* aCstr){ assert(aCstr != 0); const tchar* s1 = aStr1.data(); size_t len1 = aStr1.length(); const tchar* s2 = aCstr; size_t len2 = (aCstr) ? strlen(aCstr) : 0; int r = dw_strcmp(s1, len1, s2, len2); r = (r == 0) ? 0 : 1; return !!r;}template<class tchar> bool operator != (const tchar* aCstr, const DwString<tchar>& aStr2){ assert(aCstr != 0); const tchar* s1 = aCstr; size_t len1 = (aCstr) ? strlen(aCstr) : 0; const tchar* s2 = aStr2.data(); size_t len2 = aStr2.length(); int r = dw_strcmp(s1, len1, s2, len2); r = (r == 0) ? 0 : 1; return !!r;}template<class tchar> bool operator < (const DwString<tchar>& aStr1, const DwString<tchar>& aStr2){ const tchar* s1 = aStr1.data();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?