Example #1
0
/*
 * @implemented
 */
void CHString::TrimLeft() throw (CHeap_Exception)
{
    int NewBegin;
    int NewLength;
    WCHAR *CurrentChar;

    // We'll modify, so copy first
    CopyBeforeWrite();

    // Start at the begin of the string
    CurrentChar = m_pchData;
    while (*CurrentChar != 0)
    {
        // Browse string till we find something which is not a space
        if (!iswspace(*CurrentChar))
        {
            break;
        }

        CurrentChar++;
    }

    // Then, calculate new begin (easy) and new length
    // And move memory
    NewBegin = (CurrentChar - m_pchData);
    NewLength = GetData()->nDataLength - NewBegin;
    memmove(m_pchData, CurrentChar, NewLength * sizeof(WCHAR));
    GetData()->nDataLength = NewLength;
}
Example #2
0
int CString::Insert(int nIndex, LPCTSTR pstr)
{
	if (nIndex < 0)
		nIndex = 0;

	int nInsertLength = SafeStrlen(pstr);
	int nNewLength = GetData()->nDataLength;
	if (nInsertLength > 0)
	{
		CopyBeforeWrite();
		if (nIndex > nNewLength)
			nIndex = nNewLength;
		nNewLength += nInsertLength;

		if (GetData()->nAllocLength < nNewLength)
		{
			CStringData* pOldData = GetData();
			LPTSTR pstr = m_pchData;
			AllocBuffer(nNewLength);
			memcpy(m_pchData, pstr, (pOldData->nDataLength+1)*sizeof(TCHAR));
			CString::Release(pOldData);
		}

		// move existing bytes down
		memcpy(m_pchData + nIndex + nInsertLength,
			m_pchData + nIndex,
			(nNewLength-nIndex-nInsertLength+1)*sizeof(TCHAR));
		memcpy(m_pchData + nIndex,
			pstr, nInsertLength*sizeof(TCHAR));
		GetData()->nDataLength = nNewLength;
	}

	return nNewLength;
}
FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, FX_WCHAR ch)
{
    CopyBeforeWrite();
    if (nIndex < 0) {
        nIndex = 0;
    }
    FX_STRSIZE nNewLength = GetLength();
    if (nIndex > nNewLength) {
        nIndex = nNewLength;
    }
    nNewLength++;
    if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) {
        StringData* pOldData = m_pData;
        const FX_WCHAR* pstr = m_pData->m_String;
        m_pData = StringData::Create(nNewLength);
        if (!m_pData) {
            return 0;
        }
        if(pOldData != NULL) {
            FXSYS_memmove(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)*sizeof(FX_WCHAR));
            pOldData->Release();
        } else {
            m_pData->m_String[0] = 0;
        }
    }
    FXSYS_memmove(m_pData->m_String + nIndex + 1,
                    m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(FX_WCHAR));
    m_pData->m_String[nIndex] = ch;
    m_pData->m_nDataLength = nNewLength;
    return nNewLength;
}
Example #4
0
int CString::Insert(int nIndex, TCHAR ch)
{
	CopyBeforeWrite();

	if (nIndex < 0)
		nIndex = 0;

	int nNewLength = GetData()->nDataLength;
	if (nIndex > nNewLength)
		nIndex = nNewLength;
	nNewLength++;

	if (GetData()->nAllocLength < nNewLength)
	{
		CStringData* pOldData = GetData();
		LPTSTR pstr = m_pchData;
		AllocBuffer(nNewLength);
		memcpy(m_pchData, pstr, (pOldData->nDataLength+1)*sizeof(TCHAR));
		CString::Release(pOldData);
	}

	// move existing bytes down
	memcpy(m_pchData + nIndex + 1,
		m_pchData + nIndex, (nNewLength-nIndex)*sizeof(TCHAR));
	m_pchData[nIndex] = ch;
	GetData()->nDataLength = nNewLength;

	return nNewLength;
}
Example #5
0
/*
 * @implemented
 */
void CHString::MakeUpper() throw (CHeap_Exception)
{
    // We'll modify string, duplicate it first if needed
    CopyBeforeWrite();

    // Let's use appropriate helper
    _wcsupr(m_pchData);
}
Example #6
0
void CString::SetAt(int nIndex, TCHAR ch)
{
	ASSERT(nIndex >= 0);
	ASSERT(nIndex < GetData()->nDataLength);

	CopyBeforeWrite();
	m_pchData[nIndex] = ch;
}
Example #7
0
void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch) {
  if (m_pData == NULL) {
    return;
  }
  ASSERT(nIndex >= 0);
  ASSERT(nIndex < m_pData->m_nDataLength);
  CopyBeforeWrite();
  m_pData->m_String[nIndex] = ch;
}
Example #8
0
void CFX_WideString::MakeUpper() {
  if (m_pData == NULL) {
    return;
  }
  CopyBeforeWrite();
  if (GetLength() < 1) {
    return;
  }
  FXSYS_wcsupr(m_pData->m_String);
}
Example #9
0
void CString::ReleaseBuffer(int nNewLength)
{
	CopyBeforeWrite();  // just in case GetBuffer was not called

	if (nNewLength == -1)
		nNewLength = lstrlen(m_pchData); // zero terminated

	ASSERT(nNewLength <= GetData()->nAllocLength);
	GetData()->nDataLength = nNewLength;
	m_pchData[nNewLength] = '\0';
}
FX_STRSIZE CFX_WideString::Replace(const FX_WCHAR* lpszOld, const FX_WCHAR* lpszNew)
{
    if (GetLength() < 1) {
        return 0;
    }
    if (lpszOld == NULL) {
        return 0;
    }
    FX_STRSIZE nSourceLen = FXSYS_wcslen(lpszOld);
    if (nSourceLen == 0) {
        return 0;
    }
    FX_STRSIZE nReplacementLen = lpszNew ? FXSYS_wcslen(lpszNew) : 0;
    FX_STRSIZE nCount = 0;
    FX_WCHAR* lpszStart = m_pData->m_String;
    FX_WCHAR* lpszEnd = m_pData->m_String + m_pData->m_nDataLength;
    FX_WCHAR* lpszTarget;
    {
        while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) {
            nCount++;
            lpszStart = lpszTarget + nSourceLen;
        }
    }
    if (nCount > 0) {
        CopyBeforeWrite();
        FX_STRSIZE nOldLength = m_pData->m_nDataLength;
        FX_STRSIZE nNewLength =  nOldLength + (nReplacementLen - nSourceLen) * nCount;
        if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) {
            StringData* pOldData = m_pData;
            const FX_WCHAR* pstr = m_pData->m_String;
            m_pData = StringData::Create(nNewLength);
            if (!m_pData) {
                return 0;
            }
            FXSYS_memcpy(m_pData->m_String, pstr, pOldData->m_nDataLength * sizeof(FX_WCHAR));
            pOldData->Release();
        }
        lpszStart = m_pData->m_String;
        lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength);
        {
            while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) {
                FX_STRSIZE nBalance = nOldLength - (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen);
                FXSYS_memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen, nBalance * sizeof(FX_WCHAR));
                FXSYS_memcpy(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR));
                lpszStart = lpszTarget + nReplacementLen;
                lpszStart[nBalance] = 0;
                nOldLength += (nReplacementLen - nSourceLen);
            }
        }
        ASSERT(m_pData->m_String[nNewLength] == 0);
        m_pData->m_nDataLength = nNewLength;
    }
    return nCount;
}
Example #11
0
wxStringImpl& wxStringImpl::append(size_t n, wxStringCharType ch)
{
    size_type len = length();

    if ( !Alloc(len + n) || !CopyBeforeWrite() ) {
      wxFAIL_MSG( wxT("out of memory in wxStringImpl::append") );
      return *this;
    }
    GetStringData()->nDataLength = len + n;
    m_pchData[len + n] = '\0';
    for ( size_t i = 0; i < n; ++i )
        m_pchData[len + i] = ch;
    return *this;
}
Example #12
0
void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) {
  if (m_pData == NULL) {
    return;
  }
  CopyBeforeWrite();
  if (nNewLength == -1) {
    nNewLength = m_pData ? FXSYS_wcslen(m_pData->m_String) : 0;
  }
  if (nNewLength == 0) {
    Empty();
    return;
  }
  FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
  m_pData->m_nDataLength = nNewLength;
  m_pData->m_String[nNewLength] = 0;
}
Example #13
0
int CString::Delete(int nIndex, int nCount /* = 1 */)
{
	if (nIndex < 0)
		nIndex = 0;
	int nNewLength = GetData()->nDataLength;
	if (nCount > 0 && nIndex < nNewLength)
	{
		CopyBeforeWrite();
		int nBytesToCopy = nNewLength - (nIndex + nCount) + 1;

		memcpy(m_pchData + nIndex,
			m_pchData + nIndex + nCount, nBytesToCopy * sizeof(TCHAR));
		GetData()->nDataLength = nNewLength - nCount;
	}

	return nNewLength;
}
Example #14
0
/*
 * @implemented
 */
void CHString::ReleaseBuffer(int nNewLength) throw (CHeap_Exception)
{
    CHStringData* Data;

    // We'll modify buffer, so duplicate
    CopyBeforeWrite();

    // If no len provided, get one
    if (nNewLength == -1)
    {
        nNewLength = wcslen(m_pchData);
    }

    // Set appropriate size and null-terminate
    Data = GetData();
    Data->nDataLength = nNewLength;
    Data->data()[nNewLength] = 0;
}
FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount)
{
    if (GetLength() < 1) {
        return 0;
    }
    if (nIndex < 0) {
        nIndex = 0;
    }
    FX_STRSIZE nOldLength = m_pData->m_nDataLength;
    if (nCount > 0 && nIndex < nOldLength) {
        CopyBeforeWrite();
        int nBytesToCopy = nOldLength - (nIndex + nCount) + 1;
        FXSYS_memmove(m_pData->m_String + nIndex,
                        m_pData->m_String + nIndex + nCount, nBytesToCopy * sizeof(FX_WCHAR));
        m_pData->m_nDataLength = nOldLength - nCount;
    }
    return m_pData->m_nDataLength;
}
Example #16
0
/*
 * @implemented
 */
void CHString::TrimRight() throw (CHeap_Exception)
{
    WCHAR *CurrentChar;
    WCHAR *CanBeEaten;

    // We'll modify, so copy first
    CopyBeforeWrite();

    // Start at the begin of the string -- WHAT?!
    // Yes, this algorithm is the same that MS is
    // using for its TrimRight.
    // It is highly unefficient. It would have been
    // easier to start at nDataLength and to get back to
    // the begin. Note that it would have been safer as
    // well, in case the caller is using non-null-terminated
    // strings. But, well...
    CurrentChar = m_pchData;
    CanBeEaten = 0;
    while (*CurrentChar != 0)
    {
        // If not a space, reset what we can trim
        if (!iswspace(*CurrentChar))
        {
            CanBeEaten = 0;
        }
        // If it is one, and the first of the spaces serie
        // Keep its position
        else if (CanBeEaten == 0)
        {
            CanBeEaten = CurrentChar;
        }

        CurrentChar++;
    }

    // If nothing to trim, quit
    if (CanBeEaten == 0)
    {
        return;
    }

    // Otherwise, shorten the string
    GetData()->nDataLength = (CanBeEaten - m_pchData);
}
Example #17
0
wxStringImpl& wxStringImpl::insert(size_t nPos,
                                   const wxStringCharType *sz, size_t n)
{
    wxASSERT( nPos <= length() );

    if ( n == npos ) n = wxStrlen(sz);
    if ( n == 0 ) return *this;

    if ( !Alloc(length() + n) || !CopyBeforeWrite() ) {
        wxFAIL_MSG( wxT("out of memory in wxStringImpl::insert") );
        return *this;
    }

    memmove(m_pchData + nPos + n, m_pchData + nPos,
            (length() - nPos) * sizeof(wxStringCharType));
    memcpy(m_pchData + nPos, sz, n * sizeof(wxStringCharType));
    GetStringData()->nDataLength = length() + n;
    m_pchData[length()] = '\0';

    return *this;
}
Example #18
0
void CFX_WideString::TrimRight(const FX_WCHAR* lpszTargetList) {
  FXSYS_assert(lpszTargetList != NULL);
  if (m_pData == NULL || *lpszTargetList == 0) {
    return;
  }
  CopyBeforeWrite();
  FX_STRSIZE len = GetLength();
  if (len < 1) {
    return;
  }
  FX_STRSIZE pos = len;
  while (pos) {
    if (FXSYS_wcschr(lpszTargetList, m_pData->m_String[pos - 1]) == NULL) {
      break;
    }
    pos--;
  }
  if (pos < len) {
    m_pData->m_String[pos] = 0;
    m_pData->m_nDataLength = pos;
  }
}
Example #19
0
FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove) {
  if (m_pData == NULL) {
    return 0;
  }
  CopyBeforeWrite();
  if (GetLength() < 1) {
    return 0;
  }
  FX_WCHAR* pstrSource = m_pData->m_String;
  FX_WCHAR* pstrDest = m_pData->m_String;
  FX_WCHAR* pstrEnd = m_pData->m_String + m_pData->m_nDataLength;
  while (pstrSource < pstrEnd) {
    if (*pstrSource != chRemove) {
      *pstrDest = *pstrSource;
      pstrDest++;
    }
    pstrSource++;
  }
  *pstrDest = 0;
  FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
  m_pData->m_nDataLength -= nCount;
  return nCount;
}
void CFX_WideString::TrimLeft(const FX_WCHAR* lpszTargets)
{
    FXSYS_assert(lpszTargets != NULL);
    if (m_pData == NULL || *lpszTargets == 0) {
        return;
    }
    CopyBeforeWrite();
    if (GetLength() < 1) {
        return;
    }
    const FX_WCHAR* lpsz = m_pData->m_String;
    while (*lpsz != 0) {
        if (FXSYS_wcschr(lpszTargets, *lpsz) == NULL) {
            break;
        }
        lpsz ++;
    }
    if (lpsz != m_pData->m_String) {
        int nDataLength = m_pData->m_nDataLength - (FX_STRSIZE)(lpsz - m_pData->m_String);
        FXSYS_memmove(m_pData->m_String, lpsz, (nDataLength + 1)*sizeof(FX_WCHAR));
        m_pData->m_nDataLength = nDataLength;
    }
}
Example #21
0
int CString::Remove(TCHAR chRemove)
{
	CopyBeforeWrite();

	LPTSTR pstrSource = m_pchData;
	LPTSTR pstrDest = m_pchData;
	LPTSTR pstrEnd = m_pchData + GetData()->nDataLength;

	while (pstrSource < pstrEnd)
	{
		if (*pstrSource != chRemove)
		{
			*pstrDest = *pstrSource;
			pstrDest = _tcsinc(pstrDest);
		}
		pstrSource = _tcsinc(pstrSource);
	}
	*pstrDest = '\0';
	int nCount = pstrSource - pstrDest;
	GetData()->nDataLength -= nCount;

	return nCount;
}
Example #22
0
int CString::Replace(TCHAR chOld, TCHAR chNew)
{
	int nCount = 0;

	// short-circuit the nop case
	if (chOld != chNew)
	{
		// otherwise modify each character that matches in the string
		CopyBeforeWrite();
		LPTSTR psz = m_pchData;
		LPTSTR pszEnd = psz + GetData()->nDataLength;
		while (psz < pszEnd)
		{
			// replace instances of the specified character only
			if (*psz == chOld)
			{
				*psz = chNew;
				nCount++;
			}
			psz = _tcsinc(psz);
		}
	}
	return nCount;
}
Example #23
0
void CString::MakeReverse()
{
	CopyBeforeWrite();
	_tcsrev(m_pchData);
}
Example #24
0
int CString::Replace(LPCTSTR lpszOld, LPCTSTR lpszNew)
{
	// can't have empty or NULL lpszOld

	int nSourceLen = SafeStrlen(lpszOld);
	if (nSourceLen == 0)
		return 0;
	int nReplacementLen = SafeStrlen(lpszNew);

	// loop once to figure out the size of the result string
	int nCount = 0;
	LPTSTR lpszStart = m_pchData;
	LPTSTR lpszEnd = m_pchData + GetData()->nDataLength;
	LPTSTR lpszTarget;
	while (lpszStart < lpszEnd)
	{
		while ((lpszTarget = _tcsstr(lpszStart, lpszOld)) != NULL)
		{
			nCount++;
			lpszStart = lpszTarget + nSourceLen;
		}
		lpszStart += lstrlen(lpszStart) + 1;
	}

	// if any changes were made, make them
	if (nCount > 0)
	{
		CopyBeforeWrite();

		// if the buffer is too small, just
		//   allocate a new buffer (slow but sure)
		int nOldLength = GetData()->nDataLength;
		int nNewLength =  nOldLength + (nReplacementLen-nSourceLen)*nCount;
		if (GetData()->nAllocLength < nNewLength || GetData()->nRefs > 1)
		{
			CStringData* pOldData = GetData();
			LPTSTR pstr = m_pchData;
			AllocBuffer(nNewLength);
			memcpy(m_pchData, pstr, pOldData->nDataLength*sizeof(TCHAR));
			CString::Release(pOldData);
		}
		// else, we just do it in-place
		lpszStart = m_pchData;
		lpszEnd = m_pchData + GetData()->nDataLength;

		// loop again to actually do the work
		while (lpszStart < lpszEnd)
		{
			while ( (lpszTarget = _tcsstr(lpszStart, lpszOld)) != NULL)
			{
				int nBalance = nOldLength - (lpszTarget - m_pchData + nSourceLen);
				memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
					nBalance * sizeof(TCHAR));
				memcpy(lpszTarget, lpszNew, nReplacementLen*sizeof(TCHAR));
				lpszStart = lpszTarget + nReplacementLen;
				lpszStart[nBalance] = '\0';
				nOldLength += (nReplacementLen - nSourceLen);
			}
			lpszStart += lstrlen(lpszStart) + 1;
		}
		ASSERT(m_pchData[nNewLength] == '\0');
		GetData()->nDataLength = nNewLength;
	}

	return nCount;
}
Example #25
0
void CString::MakeLower()
{
	CopyBeforeWrite();
	::CharLower(m_pchData);
}
Example #26
0
void CString::AnsiToOem()
{
	CopyBeforeWrite();
	::AnsiToOem(m_pchData, m_pchData);
}
Example #27
0
void CString::OemToAnsi()
{
	CopyBeforeWrite();
	::OemToAnsi(m_pchData, m_pchData);
}
Example #28
0
wxStringImpl::iterator wxStringImpl::begin()
{
    if ( !empty() )
        CopyBeforeWrite();
    return m_pchData;
}
Example #29
0
wxStringImpl::iterator wxStringImpl::end()
{
    if (length() > 0)
        CopyBeforeWrite();
    return m_pchData + length();
}
Example #30
0
wxStringImpl::iterator wxStringImpl::begin()
{
    if (length() > 0)
        CopyBeforeWrite();
    return m_pchData;
}