TString<T> TString<T>::SubStr(int nIndex, int nCount) const { _ASSERT(nIndex >= 0 && nCount >= 0); TString<T> Result; if (nIndex < GetLen()) { My_strncpy(Result.GetBuffer(nCount), &pBuf[nIndex], nCount); Result.ReleaseBuffer(); } else { Result.GetBuffer(1)[0] = '\0'; Result.ReleaseBuffer(0); } return Result; }
TString<T>& TString<T>::Replace(const T *szFind, const T *szReplaceBy) { if (!pBuf) { return *this; } T *pCurPos = pBuf; int FindLen = My_lstrlen(szFind); T *p; TString<T> Result; Result.GetBuffer(1)[0] = '\0'; Result.ReleaseBuffer(0); // set the string to ""; we can't do it in a usual way (using a constructor or an assignment) because we don't know whether "" needs to be unicode or ansi while (p = ( T* )My_strstr(pCurPos, szFind)) { Result.DiffCat(pCurPos, p); Result += szReplaceBy; pCurPos = p + FindLen; } Result += pCurPos; *this = Result; return *this; }
TString<T>& TString<T>::Replace(int nIndex, int nCount, const T *szReplaceBy) { if (!pBuf || !szReplaceBy || nIndex < 0 || nCount < 0) { return *this; } TString<T> Result; Result.GetBuffer(1)[0] = '\0'; Result.ReleaseBuffer(0); // set the string to ""; we can't do it in a usual way (using a constructor or an assignment) because we don't know whether "" needs to be unicode or ansi if (nIndex > GetLen()) { nIndex = GetLen(); } if (nIndex + nCount > GetLen()) { nCount = GetLen() - nIndex; } Result.DiffCat(pBuf, pBuf + nIndex); Result += szReplaceBy; Result += pBuf + nIndex + nCount; *this = Result; return *this; }