UnicodeString& UnicodeString::Replace(size_t Pos, size_t Len, const wchar_t* Data, size_t DataLen) { // Pos & Len must be valid assert(Pos <= m_pData->GetLength()); assert(Len <= m_pData->GetLength()); assert(Pos + Len <= m_pData->GetLength()); // Data and *this must not intersect (but Data can be located entirely within *this) assert(!(Data < m_pData->GetData() && Data + DataLen > m_pData->GetData())); assert(!(Data < m_pData->GetData() + m_pData->GetLength() && Data + DataLen > m_pData->GetData() + m_pData->GetLength())); if (!Len && !DataLen) return *this; size_t NewLength = m_pData->GetLength() + DataLen - Len; if (m_pData->GetRef() == 1 && NewLength + 1 <= m_pData->GetSize()) { if (NewLength) { if (Data >= m_pData->GetData() && Data + DataLen <= m_pData->GetData() + m_pData->GetLength()) { // copy data from self UnicodeString TmpStr(Data, DataLen); wmemmove(m_pData->GetData() + Pos + DataLen, m_pData->GetData() + Pos + Len, m_pData->GetLength() - Pos - Len); wmemcpy(m_pData->GetData() + Pos, TmpStr.CPtr(), TmpStr.GetLength()); } else { wmemmove(m_pData->GetData() + Pos + DataLen, m_pData->GetData() + Pos + Len, m_pData->GetLength() - Pos - Len); wmemcpy(m_pData->GetData() + Pos, Data, DataLen); } } m_pData->SetLength(NewLength); } else { if (!NewLength) { m_pData->DecRef(); SetEUS(); return *this; } UnicodeStringData *NewData = new UnicodeStringData(NewLength + 1); wmemcpy(NewData->GetData(), m_pData->GetData(), Pos); wmemcpy(NewData->GetData() + Pos, Data, DataLen); wmemcpy(NewData->GetData() + Pos + DataLen, m_pData->GetData() + Pos + Len, m_pData->GetLength() - Pos - Len); NewData->SetLength(NewLength); m_pData->DecRef(); m_pData = NewData; } return *this; }
void UnicodeString::Inflate(size_t nSize) { if (m_pData->GetRef() == 1) { m_pData->Inflate(nSize); } else { UnicodeStringData *pNewData = new UnicodeStringData(nSize); size_t nNewLength = Min(m_pData->GetLength(),nSize-1); wmemcpy(pNewData->GetData(),m_pData->GetData(),nNewLength); pNewData->SetLength(nNewLength); m_pData->DecRef(); m_pData = pNewData; } }
bool ContainsAny(const wchar_t *Chars, size_t nStartPos=0) const { return wcspbrk(m_pData->GetData()+nStartPos,Chars) != nullptr; }
operator const wchar_t *() const { return m_pData->GetData(); }
bool Contains(wchar_t Ch, size_t nStartPos=0) const { return wcschr(m_pData->GetData()+nStartPos,Ch) != nullptr; }
UnicodeString& Unlink() {Inflate(m_pData->GetLength()+1); return *this;}
const wchar_t *CPtr() const { return m_pData->GetData(); }
wchar_t Last() const { return m_pData->GetData()[m_pData->GetLength()?m_pData->GetLength()-1:0]; }
bool IsEmpty() const { return !(m_pData->GetLength() && *m_pData->GetData()); }
wchar_t First() const { return m_pData->GetData()[0]; }
wchar_t At(size_t nIndex) const { return m_pData->GetData()[nIndex]; }
size_t GetSize() const { return m_pData->GetSize(); }
size_t GetLength() const { return m_pData->GetLength(); }
~UnicodeString() { /*if (m_pData) он не должен быть nullptr*/ m_pData->DecRef(); }