2015-05-06 21:31:40 +00:00
|
|
|
#ifndef __ATLSIMPSTR_H__
|
|
|
|
#define __ATLSIMPSTR_H__
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-09-24 20:59:09 +00:00
|
|
|
#include <atlcore.h>
|
2016-08-29 12:29:45 +00:00
|
|
|
#include <atlexcept.h>
|
2015-05-06 21:31:40 +00:00
|
|
|
|
|
|
|
namespace ATL
|
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
struct CStringData;
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-24 20:59:09 +00:00
|
|
|
// Pure virtual interface
|
|
|
|
class IAtlStringMgr
|
2015-09-23 20:05:31 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-24 20:59:09 +00:00
|
|
|
virtual ~IAtlStringMgr() {}
|
|
|
|
|
|
|
|
virtual _Ret_maybenull_ _Post_writable_byte_size_(sizeof(CStringData) + nAllocLength*nCharSize)
|
2015-09-23 20:05:31 +00:00
|
|
|
CStringData* Allocate(
|
|
|
|
_In_ int nAllocLength,
|
|
|
|
_In_ int nCharSize
|
2015-09-24 20:59:09 +00:00
|
|
|
) = 0;
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-24 20:59:09 +00:00
|
|
|
virtual void Free(
|
2015-09-23 20:05:31 +00:00
|
|
|
_Inout_ CStringData* pData
|
2015-09-24 20:59:09 +00:00
|
|
|
) = 0;
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
virtual _Ret_maybenull_ _Post_writable_byte_size_(sizeof(CStringData) + nAllocLength*nCharSize)
|
|
|
|
CStringData* Reallocate(
|
|
|
|
_Inout_ CStringData* pData,
|
|
|
|
_In_ int nAllocLength,
|
|
|
|
_In_ int nCharSize
|
2015-09-24 20:59:09 +00:00
|
|
|
) = 0;
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-24 20:59:09 +00:00
|
|
|
virtual CStringData* GetNilString(void) = 0;
|
|
|
|
virtual IAtlStringMgr* Clone(void) = 0;
|
2015-09-23 20:05:31 +00:00
|
|
|
};
|
2015-05-06 21:31:40 +00:00
|
|
|
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
struct CStringData
|
|
|
|
{
|
|
|
|
IAtlStringMgr* pStringMgr;
|
|
|
|
int nAllocLength;
|
|
|
|
int nDataLength;
|
|
|
|
long nRefs;
|
|
|
|
|
|
|
|
void* data() throw()
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
return (this + 1);
|
|
|
|
}
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
void AddRef() throw()
|
|
|
|
{
|
|
|
|
ATLASSERT(nRefs > 0);
|
|
|
|
_InterlockedIncrement(&nRefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Release() throw()
|
|
|
|
{
|
|
|
|
ATLASSERT(nRefs != 0);
|
|
|
|
|
|
|
|
if (_InterlockedDecrement(&nRefs) <= 0)
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
pStringMgr->Free(this);
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLocked() const throw()
|
|
|
|
{
|
|
|
|
return (nRefs < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsShared() const throw()
|
|
|
|
{
|
|
|
|
return (nRefs > 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CNilStringData :
|
|
|
|
public CStringData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CNilStringData() throw()
|
|
|
|
{
|
|
|
|
pStringMgr = NULL;
|
|
|
|
nRefs = 2;
|
|
|
|
nDataLength = 0;
|
|
|
|
nAllocLength = 0;
|
|
|
|
achNil[0] = 0;
|
|
|
|
achNil[1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetManager(_In_ IAtlStringMgr* pMgr) throw()
|
|
|
|
{
|
|
|
|
ATLASSERT(pStringMgr == NULL);
|
|
|
|
pStringMgr = pMgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
wchar_t achNil[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
template< typename BaseType = char >
|
2015-10-12 20:33:33 +00:00
|
|
|
class ChTraitsBase
|
2015-09-23 20:05:31 +00:00
|
|
|
{
|
2015-10-25 22:55:18 +00:00
|
|
|
public:
|
|
|
|
typedef char XCHAR;
|
|
|
|
typedef LPSTR PXSTR;
|
|
|
|
typedef LPCSTR PCXSTR;
|
|
|
|
typedef wchar_t YCHAR;
|
|
|
|
typedef LPWSTR PYSTR;
|
|
|
|
typedef LPCWSTR PCYSTR;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class ChTraitsBase<wchar_t>
|
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
public:
|
|
|
|
typedef wchar_t XCHAR;
|
|
|
|
typedef LPWSTR PXSTR;
|
|
|
|
typedef LPCWSTR PCXSTR;
|
|
|
|
typedef char YCHAR;
|
|
|
|
typedef LPSTR PYSTR;
|
|
|
|
typedef LPCSTR PCYSTR;
|
|
|
|
};
|
|
|
|
|
|
|
|
template< typename BaseType, bool t_bMFCDLL = false>
|
|
|
|
class CSimpleStringT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename ChTraitsBase<BaseType>::XCHAR XCHAR;
|
|
|
|
typedef typename ChTraitsBase<BaseType>::PXSTR PXSTR;
|
|
|
|
typedef typename ChTraitsBase<BaseType>::PCXSTR PCXSTR;
|
|
|
|
typedef typename ChTraitsBase<BaseType>::YCHAR YCHAR;
|
|
|
|
typedef typename ChTraitsBase<BaseType>::PYSTR PYSTR;
|
|
|
|
typedef typename ChTraitsBase<BaseType>::PCYSTR PCYSTR;
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
private:
|
|
|
|
PXSTR m_pszData;
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
public:
|
|
|
|
explicit CSimpleStringT(_Inout_ IAtlStringMgr* pStringMgr)
|
|
|
|
{
|
|
|
|
CStringData* pData = pStringMgr->GetNilString();
|
|
|
|
Attach(pData);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSimpleStringT(_In_ const CSimpleStringT& strSrc)
|
|
|
|
{
|
|
|
|
CStringData* pSrcData = strSrc.GetData();
|
|
|
|
CStringData* pNewData = CloneData(pSrcData);
|
|
|
|
Attach(pNewData);
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
CSimpleStringT(
|
|
|
|
_In_z_ PCXSTR pszSrc,
|
|
|
|
_Inout_ IAtlStringMgr* pStringMgr)
|
|
|
|
{
|
|
|
|
int nLength = StringLength(pszSrc);
|
|
|
|
CStringData* pData = pStringMgr->Allocate(nLength, sizeof(XCHAR));
|
|
|
|
if (pData == NULL)
|
2017-08-15 09:02:17 +00:00
|
|
|
ThrowMemoryException();
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
Attach(pData);
|
|
|
|
SetLength(nLength);
|
|
|
|
CopyChars(m_pszData, nLength, pszSrc, nLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
CSimpleStringT(
|
|
|
|
_In_count_(nLength) const XCHAR* pchSrc,
|
|
|
|
_In_ int nLength,
|
|
|
|
_Inout_ IAtlStringMgr* pStringMgr)
|
|
|
|
{
|
|
|
|
if (pchSrc == NULL && nLength != 0)
|
2017-08-15 09:02:17 +00:00
|
|
|
ThrowInvalidArgException();
|
2015-10-25 22:55:18 +00:00
|
|
|
|
|
|
|
CStringData* pData = pStringMgr->Allocate(nLength, sizeof(XCHAR));
|
|
|
|
if (pData == NULL)
|
|
|
|
{
|
2017-08-15 09:02:17 +00:00
|
|
|
ThrowMemoryException();
|
2015-10-25 22:55:18 +00:00
|
|
|
}
|
|
|
|
Attach(pData);
|
|
|
|
SetLength(nLength);
|
|
|
|
CopyChars(m_pszData, nLength, pchSrc, nLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CSimpleStringT() throw()
|
|
|
|
{
|
|
|
|
CStringData* pData = GetData();
|
|
|
|
pData->Release();
|
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
|
|
|
|
CSimpleStringT& operator=(_In_opt_z_ PCXSTR pszSrc)
|
|
|
|
{
|
|
|
|
SetString(pszSrc);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
[ATL][ATL_APITEST] Implement / Improve CString, based upon the code that was already there. CORE-11579 #resolve
Add code + tests for:
Conversion of A->W and W->A, equality operators, MakeLower, MakeUpper, Find, FindOneOf, ReverseFind, Compare, Mid, Left, Right, Format, Replace, Trim, TrimLeft, TrimRight.
svn path=/trunk/; revision=72061
2016-07-30 19:07:43 +00:00
|
|
|
CSimpleStringT& operator=(_In_ const CSimpleStringT& strSrc)
|
|
|
|
{
|
|
|
|
CStringData* pData = GetData();
|
|
|
|
CStringData* pNewData = strSrc.GetData();
|
|
|
|
|
|
|
|
if (pNewData != pData)
|
|
|
|
{
|
|
|
|
if (!pData->IsLocked() && (pNewData->pStringMgr == pData->pStringMgr))
|
|
|
|
{
|
|
|
|
pNewData = CloneData(pNewData);
|
|
|
|
pData->Release();
|
|
|
|
Attach(pNewData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetString(strSrc.GetString(), strSrc.GetLength());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
CSimpleStringT& operator+=(_In_ const CSimpleStringT& strSrc)
|
|
|
|
{
|
|
|
|
Append(strSrc);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSimpleStringT& operator+=(_In_z_ PCXSTR pszSrc)
|
|
|
|
{
|
|
|
|
Append(pszSrc);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
|
2016-09-24 18:01:49 +00:00
|
|
|
CSimpleStringT& operator+=(XCHAR ch)
|
|
|
|
{
|
|
|
|
Append(&ch, 1);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
operator PCXSTR() const throw()
|
|
|
|
{
|
|
|
|
return m_pszData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Empty() throw()
|
|
|
|
{
|
|
|
|
CStringData* pOldData = GetData();
|
|
|
|
IAtlStringMgr* pStringMgr = pOldData->pStringMgr;
|
|
|
|
if (pOldData->nDataLength == 0) return;
|
|
|
|
|
|
|
|
if (pOldData->IsLocked())
|
|
|
|
{
|
|
|
|
SetLength(0);
|
|
|
|
}
|
|
|
|
else
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
pOldData->Release();
|
|
|
|
CStringData* pNewData = pStringMgr->GetNilString();
|
|
|
|
Attach(pNewData);
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
void Append(
|
|
|
|
_In_count_(nLength) PCXSTR pszSrc,
|
|
|
|
_In_ int nLength)
|
|
|
|
{
|
|
|
|
UINT_PTR nOffset = pszSrc - GetString();
|
|
|
|
|
|
|
|
int nOldLength = GetLength();
|
|
|
|
if (nOldLength < 0)
|
|
|
|
nOldLength = 0;
|
|
|
|
|
|
|
|
ATLASSERT(nLength >= 0);
|
|
|
|
|
|
|
|
#if 0 // FIXME: See comment for StringLengthN below.
|
|
|
|
nLength = StringLengthN(pszSrc, nLength);
|
|
|
|
if (!(INT_MAX - nLength >= nOldLength))
|
|
|
|
throw;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int nNewLength = nOldLength + nLength;
|
|
|
|
PXSTR pszBuffer = GetBuffer(nNewLength);
|
|
|
|
if (nOffset <= (UINT_PTR)nOldLength)
|
|
|
|
{
|
|
|
|
pszSrc = pszBuffer + nOffset;
|
|
|
|
}
|
|
|
|
CopyChars(pszBuffer + nOldLength, nLength, pszSrc, nLength);
|
|
|
|
ReleaseBufferSetLength(nNewLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Append(_In_z_ PCXSTR pszSrc)
|
|
|
|
{
|
|
|
|
Append(pszSrc, StringLength(pszSrc));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Append(_In_ const CSimpleStringT& strSrc)
|
|
|
|
{
|
|
|
|
Append(strSrc.GetString(), strSrc.GetLength());
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
void SetString(_In_opt_z_ PCXSTR pszSrc)
|
|
|
|
{
|
|
|
|
SetString(pszSrc, StringLength(pszSrc));
|
|
|
|
}
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
void SetString(_In_reads_opt_(nLength) PCXSTR pszSrc,
|
|
|
|
_In_ int nLength)
|
|
|
|
{
|
|
|
|
if (nLength == 0)
|
|
|
|
{
|
|
|
|
Empty();
|
|
|
|
}
|
|
|
|
else
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
UINT nOldLength = GetLength();
|
|
|
|
UINT_PTR nOffset = pszSrc - GetString();
|
2015-05-06 21:31:40 +00:00
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
PXSTR pszBuffer = GetBuffer(nLength);
|
|
|
|
if (nOffset <= nOldLength)
|
|
|
|
{
|
|
|
|
CopyCharsOverlapped(pszBuffer, GetAllocLength(),
|
|
|
|
pszBuffer + nOffset, nLength);
|
|
|
|
}
|
|
|
|
else
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
CopyChars(pszBuffer, GetAllocLength(), pszSrc, nLength);
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
ReleaseBufferSetLength(nLength);
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PXSTR GetBuffer()
|
|
|
|
{
|
|
|
|
CStringData* pData = GetData();
|
|
|
|
if (pData->IsShared())
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
// We should fork here
|
|
|
|
Fork(pData->nDataLength);
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
return m_pszData;
|
|
|
|
}
|
|
|
|
|
2015-09-24 20:59:09 +00:00
|
|
|
_Ret_notnull_ _Post_writable_size_(nMinBufferLength + 1) PXSTR GetBuffer(_In_ int nMinBufferLength)
|
|
|
|
{
|
|
|
|
return PrepareWrite(nMinBufferLength);
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
int GetAllocLength() const throw()
|
|
|
|
{
|
|
|
|
return GetData()->nAllocLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetLength() const throw()
|
|
|
|
{
|
|
|
|
return GetData()->nDataLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
PCXSTR GetString() const throw()
|
|
|
|
{
|
|
|
|
return m_pszData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseBufferSetLength(_In_ int nNewLength)
|
|
|
|
{
|
|
|
|
ATLASSERT(nNewLength >= 0);
|
|
|
|
SetLength(nNewLength);
|
|
|
|
}
|
|
|
|
|
2016-09-24 18:01:49 +00:00
|
|
|
void ReleaseBuffer(_In_ int nNewLength = -1)
|
|
|
|
{
|
|
|
|
if (nNewLength < 0)
|
|
|
|
nNewLength = StringLength(m_pszData);
|
|
|
|
ReleaseBufferSetLength(nNewLength);
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
bool IsEmpty() const throw()
|
|
|
|
{
|
|
|
|
return (GetLength() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CStringData* GetData() const throw()
|
|
|
|
{
|
2015-10-25 22:55:18 +00:00
|
|
|
return (reinterpret_cast<CStringData*>(m_pszData) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
IAtlStringMgr* GetManager() const throw()
|
|
|
|
{
|
|
|
|
IAtlStringMgr* pStringMgr = GetData()->pStringMgr;
|
|
|
|
return (pStringMgr ? pStringMgr->Clone() : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
friend CSimpleStringT operator+(
|
|
|
|
_In_ const CSimpleStringT& str1,
|
|
|
|
_In_ const CSimpleStringT& str2)
|
|
|
|
{
|
|
|
|
CSimpleStringT s(str1.GetManager());
|
|
|
|
Concatenate(s, str1, str1.GetLength(), str2, str2.GetLength());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend CSimpleStringT operator+(
|
|
|
|
_In_ const CSimpleStringT& str1,
|
|
|
|
_In_z_ PCXSTR psz2)
|
|
|
|
{
|
|
|
|
CSimpleStringT s(str1.GetManager());
|
|
|
|
Concatenate(s, str1, str1.GetLength(), psz2, StringLength(psz2));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend CSimpleStringT operator+(
|
|
|
|
_In_z_ PCXSTR psz1,
|
|
|
|
_In_ const CSimpleStringT& str2)
|
|
|
|
{
|
|
|
|
CSimpleStringT s(str2.GetManager());
|
|
|
|
Concatenate(s, psz1, StringLength(psz1), str2, str2.GetLength());
|
|
|
|
return s;
|
2015-09-23 20:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __cdecl CopyChars(
|
|
|
|
_Out_writes_to_(nDestLen, nChars) XCHAR* pchDest,
|
|
|
|
_In_ size_t nDestLen,
|
|
|
|
_In_reads_opt_(nChars) const XCHAR* pchSrc,
|
|
|
|
_In_ int nChars) throw()
|
|
|
|
{
|
|
|
|
memcpy(pchDest, pchSrc, nChars * sizeof(XCHAR));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __cdecl CopyCharsOverlapped(
|
|
|
|
_Out_writes_to_(nDestLen, nDestLen) XCHAR* pchDest,
|
|
|
|
_In_ size_t nDestLen,
|
|
|
|
_In_reads_(nChars) const XCHAR* pchSrc,
|
|
|
|
_In_ int nChars) throw()
|
|
|
|
{
|
|
|
|
memmove(pchDest, pchSrc, nChars * sizeof(XCHAR));
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
static int __cdecl StringLength(_In_opt_z_ const char* psz) throw()
|
|
|
|
{
|
|
|
|
if (psz == NULL) return 0;
|
|
|
|
return (int)strlen(psz);
|
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
static int __cdecl StringLength(_In_opt_z_ const wchar_t* psz) throw()
|
|
|
|
{
|
|
|
|
if (psz == NULL) return 0;
|
|
|
|
return (int)wcslen(psz);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0 // For whatever reason we do not link with strnlen / wcsnlen. Please investigate!
|
|
|
|
// strnlen / wcsnlen are available in MSVCRT starting Vista+.
|
|
|
|
static int __cdecl StringLengthN(
|
|
|
|
_In_opt_z_count_(sizeInXChar) const char* psz,
|
|
|
|
_In_ size_t sizeInXChar) throw()
|
|
|
|
{
|
|
|
|
if (psz == NULL) return 0;
|
|
|
|
return (int)strnlen(psz, sizeInXChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __cdecl StringLengthN(
|
|
|
|
_In_opt_z_count_(sizeInXChar) const wchar_t* psz,
|
|
|
|
_In_ size_t sizeInXChar) throw()
|
|
|
|
{
|
|
|
|
if (psz == NULL) return 0;
|
|
|
|
return (int)wcsnlen(psz, sizeInXChar);
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-23 20:05:31 +00:00
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
protected:
|
|
|
|
static void __cdecl Concatenate(
|
|
|
|
_Inout_ CSimpleStringT& strResult,
|
|
|
|
_In_count_(nLength1) PCXSTR psz1,
|
|
|
|
_In_ int nLength1,
|
|
|
|
_In_count_(nLength2) PCXSTR psz2,
|
|
|
|
_In_ int nLength2)
|
|
|
|
{
|
|
|
|
int nNewLength = nLength1 + nLength2;
|
|
|
|
PXSTR pszBuffer = strResult.GetBuffer(nNewLength);
|
|
|
|
CopyChars(pszBuffer, nLength1, psz1, nLength1);
|
|
|
|
CopyChars(pszBuffer + nLength1, nLength2, psz2, nLength2);
|
|
|
|
strResult.ReleaseBufferSetLength(nNewLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-09-23 20:05:31 +00:00
|
|
|
void Attach(_Inout_ CStringData* pData) throw()
|
|
|
|
{
|
|
|
|
m_pszData = static_cast<PXSTR>(pData->data());
|
|
|
|
}
|
2015-10-25 22:55:18 +00:00
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
__declspec(noinline) void Fork(_In_ int nLength)
|
|
|
|
{
|
|
|
|
CStringData* pOldData = GetData();
|
|
|
|
int nOldLength = pOldData->nDataLength;
|
|
|
|
CStringData* pNewData = pOldData->pStringMgr->Clone()->Allocate(nLength, sizeof(XCHAR));
|
|
|
|
if (pNewData == NULL)
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2016-08-29 12:29:45 +00:00
|
|
|
ThrowMemoryException();
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
int nCharsToCopy = ((nOldLength < nLength) ? nOldLength : nLength) + 1;
|
|
|
|
CopyChars(PXSTR(pNewData->data()), nCharsToCopy,
|
|
|
|
PCXSTR(pOldData->data()), nCharsToCopy);
|
|
|
|
pNewData->nDataLength = nOldLength;
|
|
|
|
pOldData->Release();
|
|
|
|
Attach(pNewData);
|
|
|
|
}
|
|
|
|
|
|
|
|
PXSTR PrepareWrite(_In_ int nLength)
|
|
|
|
{
|
|
|
|
CStringData* pOldData = GetData();
|
|
|
|
int nShared = 1 - pOldData->nRefs;
|
|
|
|
int nTooShort = pOldData->nAllocLength - nLength;
|
|
|
|
if ((nShared | nTooShort) < 0)
|
2015-05-06 21:31:40 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
PrepareWrite2(nLength);
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
return m_pszData;
|
|
|
|
}
|
|
|
|
void PrepareWrite2(_In_ int nLength)
|
|
|
|
{
|
|
|
|
CStringData* pOldData = GetData();
|
|
|
|
if (pOldData->nDataLength > nLength)
|
|
|
|
{
|
|
|
|
nLength = pOldData->nDataLength;
|
|
|
|
}
|
|
|
|
if (pOldData->IsShared())
|
|
|
|
{
|
|
|
|
Fork(nLength);
|
[ATL][ATL_APITEST] Implement / Improve CString, based upon the code that was already there. CORE-11579 #resolve
Add code + tests for:
Conversion of A->W and W->A, equality operators, MakeLower, MakeUpper, Find, FindOneOf, ReverseFind, Compare, Mid, Left, Right, Format, Replace, Trim, TrimLeft, TrimRight.
svn path=/trunk/; revision=72061
2016-07-30 19:07:43 +00:00
|
|
|
//ATLASSERT(FALSE);
|
2015-09-23 20:05:31 +00:00
|
|
|
}
|
|
|
|
else if (pOldData->nAllocLength < nLength)
|
|
|
|
{
|
|
|
|
int nNewLength = pOldData->nAllocLength;
|
|
|
|
if (nNewLength > 1024 * 1024 * 1024)
|
|
|
|
{
|
|
|
|
nNewLength += 1024 * 1024;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nNewLength = nNewLength + nNewLength / 2;
|
|
|
|
}
|
|
|
|
if (nNewLength < nLength)
|
|
|
|
{
|
|
|
|
nNewLength = nLength;
|
|
|
|
}
|
|
|
|
Reallocate(nNewLength);
|
|
|
|
}
|
|
|
|
}
|
2015-05-06 21:56:24 +00:00
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
void Reallocate(_In_ int nLength)
|
|
|
|
{
|
|
|
|
CStringData* pOldData = GetData();
|
|
|
|
ATLASSERT(pOldData->nAllocLength < nLength);
|
|
|
|
IAtlStringMgr* pStringMgr = pOldData->pStringMgr;
|
|
|
|
if (pOldData->nAllocLength >= nLength || nLength <= 0)
|
2015-05-06 21:56:24 +00:00
|
|
|
{
|
2015-09-23 20:05:31 +00:00
|
|
|
return;
|
2015-05-06 21:56:24 +00:00
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
CStringData* pNewData = pStringMgr->Reallocate(pOldData, nLength, sizeof(XCHAR));
|
2015-10-25 22:55:18 +00:00
|
|
|
if (pNewData == NULL)
|
|
|
|
{
|
2016-08-29 12:29:45 +00:00
|
|
|
ThrowMemoryException();
|
2015-10-25 22:55:18 +00:00
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
|
|
|
|
Attach(pNewData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLength(_In_ int nLength)
|
|
|
|
{
|
|
|
|
ATLASSERT(nLength >= 0);
|
|
|
|
ATLASSERT(nLength <= GetData()->nAllocLength);
|
|
|
|
|
|
|
|
if (nLength < 0 || nLength > GetData()->nAllocLength)
|
2016-08-29 12:29:45 +00:00
|
|
|
{
|
|
|
|
AtlThrow(E_INVALIDARG);
|
|
|
|
}
|
2015-09-23 20:05:31 +00:00
|
|
|
|
|
|
|
GetData()->nDataLength = nLength;
|
|
|
|
m_pszData[nLength] = 0;
|
|
|
|
}
|
2015-05-06 21:56:24 +00:00
|
|
|
|
2015-09-24 18:06:02 +00:00
|
|
|
static CStringData* __cdecl CloneData(_Inout_ CStringData* pData)
|
|
|
|
{
|
|
|
|
CStringData* pNewData = NULL;
|
|
|
|
|
|
|
|
IAtlStringMgr* pNewStringMgr = pData->pStringMgr->Clone();
|
|
|
|
if (!pData->IsLocked() && (pNewStringMgr == pData->pStringMgr))
|
|
|
|
{
|
|
|
|
pNewData = pData;
|
|
|
|
pNewData->AddRef();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pNewData = pNewStringMgr->Allocate(pData->nDataLength, sizeof(XCHAR));
|
2015-10-25 22:55:18 +00:00
|
|
|
if (pNewData == NULL)
|
|
|
|
{
|
2016-08-29 12:29:45 +00:00
|
|
|
ThrowMemoryException();
|
2015-10-25 22:55:18 +00:00
|
|
|
}
|
2015-09-24 20:59:09 +00:00
|
|
|
|
2015-09-24 18:06:02 +00:00
|
|
|
pNewData->nDataLength = pData->nDataLength;
|
|
|
|
CopyChars(PXSTR(pNewData->data()), pData->nDataLength + 1,
|
|
|
|
PCXSTR(pData->data()), pData->nDataLength + 1);
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:55:18 +00:00
|
|
|
return pNewData;
|
2015-09-24 18:06:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 12:29:45 +00:00
|
|
|
|
|
|
|
static void ThrowMemoryException()
|
|
|
|
{
|
|
|
|
AtlThrow(E_OUTOFMEMORY);
|
|
|
|
}
|
|
|
|
|
2017-08-15 09:02:17 +00:00
|
|
|
static void ThrowInvalidArgException()
|
|
|
|
{
|
|
|
|
AtlThrow(E_INVALIDARG);
|
|
|
|
}
|
|
|
|
|
2015-09-23 20:05:31 +00:00
|
|
|
};
|
2016-09-24 18:01:49 +00:00
|
|
|
|
|
|
|
#ifdef UNICODE
|
|
|
|
typedef CSimpleStringT<WCHAR> CSimpleString;
|
|
|
|
#else
|
|
|
|
typedef CSimpleStringT<CHAR> CSimpleString;
|
|
|
|
#endif
|
2015-05-06 21:31:40 +00:00
|
|
|
}
|
|
|
|
|
2016-09-24 18:01:49 +00:00
|
|
|
#endif
|