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; } }