⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vostring.cpp

📁 仿CString类
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-------------------------------------------------------------------
// VOString implementation file
//-------------------------------------------------------------------
//
// Copyright ?000- 02 Virtual Office Systems Incorporated
// All Rights Reserved
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This code can be compiled, modified and distributed freely, providing
// that this copyright information remains intact in the distribution.
//
// This code may be compiled in original or modified form in any private
// or commercial application.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage, in any form, caused
// by this code. Use it at your own risk.
//-------------------------------------------------------------------

#include "stdafx.h"
#include "VOString.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CVOString::CVOString(LPCTSTR pcszValue)
		: m_pBuffer(NULL),
		m_dwLength(0),
		m_dwBufferSize(0)
{
	*this = pcszValue;
}

// Copy Constructor
CVOString::CVOString(const CVOString &rSrc)
		: m_pBuffer(NULL),
		m_dwLength(0),
		m_dwBufferSize(0)
{
	*this = rSrc.m_pBuffer;
}

CVOString::~CVOString()
{
	if (m_pBuffer)
	{
		delete [] m_pBuffer;
	}
}

void CVOString::operator = (const CVOString& rSrc)
{
	if ( &rSrc != this )
	{
		*this = rSrc.m_pBuffer;
	}
}

void CVOString::operator = (LPCTSTR pcszValue)
{
	if (pcszValue == NULL)
	{
		m_dwLength = 0;

		if (m_pBuffer)
		{
			delete [] m_pBuffer;
			m_pBuffer = NULL;
		}
	}
	else
	{
		m_dwLength = _tcslen(pcszValue);

		SetMinBufferSize(m_dwLength);

		if (m_dwLength == 0)
		{
			m_pBuffer[0] = TCHAR(0);
		}
		else
		{
			_tcscpy(m_pBuffer, pcszValue);
		}
	}
}

BOOL CVOString::operator == (LPCTSTR pcszValue) const
{
	if (!m_pBuffer)
	{
		if (!pcszValue)
		{
			return TRUE;
		}

		return (_tcsicmp(pcszValue, TEXT("")) == 0);
	}

	return(_tcsicmp(pcszValue, m_pBuffer) == 0);
}

BOOL CVOString::operator == (const CVOString& rCompare) const
{
	if (!m_pBuffer)
	{
		if (rCompare.GetLength() == 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	if (!rCompare.m_pBuffer)
	{
		return FALSE;
	}

	return(_tcsicmp(rCompare.m_pBuffer, m_pBuffer) == 0);
}

LPCTSTR CVOString::operator += (LPCTSTR pcszAppend)
{
	if (m_pBuffer)
	{
		SetMinBufferSize(GetLength() + _tcslen(pcszAppend));
		_tcscat(m_pBuffer, pcszAppend);
		m_dwLength = _tcslen(m_pBuffer);
	}
	else
	{
		*this = pcszAppend;
	}

	return *this;
}

LPCTSTR CVOString::operator += (TCHAR chAppend)
{
	SetMinBufferSize(m_dwLength + 2);
	m_dwLength++;
	m_pBuffer[m_dwLength] = 0;
	m_pBuffer[m_dwLength - 1] = chAppend;

	return *this;
}

CVOString CVOString::operator + (LPCTSTR pcszAppend) const
{
	CVOString strReturn(*this);

	strReturn += pcszAppend;

	return strReturn;
}

BOOL CVOString::operator != (LPCTSTR pcszValue) const
{
	return operator ==(pcszValue) == 0;
}

BOOL CVOString::operator != (const CVOString& rValue) const
{
	return operator ==(rValue) == 0;
}

CVOString::operator LPCTSTR() const
{
	return (LPCTSTR)m_pBuffer;
}

DWORD CVOString::GetLength() const
{
	return m_dwLength;
}

BOOL CVOString::SetMinBufferSize(DWORD dwChars)
{
#ifdef _DEBUG
	if (dwChars > 65536)
	{
		DebugBreak();	// This is just a precaution to protect against memory leaks.  Large strings are fine.
	}
#endif
	if ( (m_dwBufferSize < dwChars + 1) || (m_pBuffer == NULL) )
	{
		TCHAR*	pNewBuffer = NULL;
		DWORD	dwNewBufferSize = dwChars + 16;

		pNewBuffer = new TCHAR[dwNewBufferSize];

		memset(pNewBuffer, 0, sizeof(TCHAR) * dwNewBufferSize);

		if ( m_pBuffer )
		{
			memmove(pNewBuffer, m_pBuffer, m_dwBufferSize * sizeof(TCHAR));
			delete [] m_pBuffer;
		}

		m_pBuffer = pNewBuffer;

		m_dwBufferSize = dwNewBufferSize;
	}

	return TRUE;
}

TCHAR CVOString::GetAt(DWORD dwOffset) const
{
	if (dwOffset > m_dwLength - 1)
	{
		return (TCHAR) 0;
	}

	return m_pBuffer[dwOffset];
}

CVOString CVOString::Left(DWORD dwCount) const
{
	if (dwCount >= m_dwLength)
	{
		return CVOString(*this);
	}

	LPTSTR	pszTemp = new TCHAR[dwCount + 1];

	memset(pszTemp, 0, sizeof(TCHAR) * (dwCount + 1));
	memmove(pszTemp, m_pBuffer, sizeof(TCHAR) * dwCount);

	CVOString strValue(pszTemp);

	delete [] pszTemp;

	return strValue;
}

CVOString CVOString::Mid(DWORD dwOffset, int nLength) const
{
	if ( dwOffset > m_dwLength )
	{
		return CVOString(TEXT(""));
	}

	if (nLength == -1)
	{
		nLength = (int)(m_dwLength - dwOffset);
	}
	else if (dwOffset + (DWORD)nLength > m_dwLength)
	{
		nLength = (int)(m_dwLength - dwOffset);
	}

	LPTSTR	pszTemp = new TCHAR[(size_t)(nLength + 1)];

	memset(pszTemp, 0, sizeof(TCHAR) * ((size_t)nLength + 1));
	memmove(pszTemp, m_pBuffer + dwOffset, sizeof(TCHAR) * (size_t)nLength);

	CVOString strValue(pszTemp);

	delete [] pszTemp;

	return strValue;
}

LPTSTR CVOString::GetBuffer(DWORD dwMinSize)
{
	SetMinBufferSize(dwMinSize + 1);

	return m_pBuffer;
}

int CVOString::Format(LPCTSTR pcszFormat, ...)
{
	va_list vl;

	va_start( vl, pcszFormat);

	::wvsprintf(GetBuffer(256), pcszFormat, vl);
	ReleaseBuffer();

	va_end(vl);

	return (int)(GetLength());
}

int CVOString::Find(LPCTSTR pcszValue, int nStartingOffset) const
{
	LPTSTR pszSubstring;

	if (nStartingOffset < 0)
	{
		nStartingOffset = 0;
	}

	pszSubstring = _tcsstr(m_pBuffer + nStartingOffset, pcszValue);

	if (!pszSubstring)
	{
		return -1;
	}

	return (pszSubstring - m_pBuffer);
}

int CVOString::Find(TCHAR chValue, int nStartingOffset) const
{
	TCHAR pcszValue[2];

	pcszValue[0] = chValue;
	pcszValue[1] = 0;

	return Find(pcszValue, nStartingOffset);
}

int CVOString::ReverseFind(LPCTSTR pcszSubstring) const
{
	int nOffset = -1, nNextOffset = 0;

	while ((nNextOffset = Find(pcszSubstring, nNextOffset)) != -1)
	{
		nOffset = nNextOffset++;
	}

	return nOffset;
}

DWORD CVOString::ReleaseBuffer(int nChars)
{
	if ((m_pBuffer == NULL) || (m_dwBufferSize == 0))
	{
		return 0;
	}

	if (nChars == -1)
	{
		m_dwLength = _tcslen(m_pBuffer);
	}
	else
	{
		m_dwLength = (DWORD) nChars;

		if (m_dwLength > m_dwBufferSize)
		{
			m_dwLength = m_dwBufferSize;
		}

		m_pBuffer[m_dwLength] = 0;
	}

	return m_dwLength;
}

const CVOString& CVOString::TrimRight()
{
	while (GetLength() && GetAt(GetLength() - 1) == TCHAR(' '))
	{
		*this = Left(GetLength() - 1);
	}

	return *this;
}

const CVOString& CVOString::TrimLeft()
{
	while (GetLength() && GetAt(0) == TCHAR(' '))
	{
		*this = Mid(1);
	}

	return *this;
}

const CVOString& CVOString::Trim()
{
	TrimLeft();

	return TrimRight();
}

CVOString CVOString::Right(int nChars) const
{
	if ((int)GetLength() < nChars)
	{
		return *this;
	}

	return Mid(GetLength() - (DWORD)nChars);
}

void CVOString::MakeMixedCase()
{
	BOOL fFirstWord = TRUE;

	for (int i = 0; i < (int)GetLength(); ++i)
	{
		if ( (m_pBuffer[i] >= TCHAR('a') && m_pBuffer[i] <= TCHAR('z')) ||
			(m_pBuffer[i] >= TCHAR('A') && m_pBuffer[i] <= TCHAR('Z')) )	// Start of Word
		{
			int nWordEnd = i;

			while (	(m_pBuffer[nWordEnd] >= TCHAR('a') && m_pBuffer[nWordEnd] <= TCHAR('z')) ||
				(m_pBuffer[nWordEnd] >= TCHAR('A') && m_pBuffer[nWordEnd] <= TCHAR('Z')) )
			{
				nWordEnd++;
			}

			CVOString strWord;

			strWord = Mid((DWORD)i, nWordEnd - i);
			strWord.MakeLower();

			BOOL fCapitalize = FALSE;

			if (fFirstWord)
			{
				fCapitalize = TRUE;
				fFirstWord = FALSE;
			}
			else
			{
				fCapitalize = TRUE;

				if (strWord == TEXT("a"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("an"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("the"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("and"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("but"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("or"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("nor"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("at"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("by"))
				{
					fCapitalize = FALSE;
				}
				else if (strWord == TEXT("for"))
				{
					fCapitalize = FALSE;
				}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -