📄 vostring.cpp
字号:
//-------------------------------------------------------------------
// 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"
#ifndef WIN32_PLATFORM_WFSP
#include <atlbase.h>
#endif
#include <tchar.h>
#include <stdarg.h>
#include "VOString.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVOString::CVOString(LPCTSTR pcszValue)
{
m_pBuffer = NULL;
m_dwBufferSize = 0;
*this = pcszValue;
}
// Copy Constructor
CVOString::CVOString(const CVOString &rSrc)
{
m_pBuffer = NULL;
m_dwBufferSize = 0;
*this = rSrc.m_pBuffer;
}
CVOString::~CVOString()
{
if(m_pBuffer)
delete m_pBuffer;
}
void CVOString::operator = (const CVOString& rSrc)
{
*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);
}
}
#ifndef _UNICODE
#ifndef _DOS
void CVOString::operator =(LPCWSTR pcwszValue)
{
if(!pcwszValue)
{
m_dwLength = 0;
if(m_pBuffer)
{
delete m_pBuffer;
m_pBuffer = NULL;
}
return;
}
if(sizeof(TCHAR) == sizeof(WCHAR))
{
m_dwLength = wcslen(pcwszValue);
SetMinBufferSize(m_dwLength);
wcscpy((LPWSTR)m_pBuffer, pcwszValue);
}
else
{
int nChars = wcslen(pcwszValue) + 1;
LPTSTR pszBuffer = new TCHAR[nChars];
for(int n = 0; n < nChars; n++)
pszBuffer[n] = (TCHAR) pcwszValue[n];
m_dwLength = _tcslen(pszBuffer);
SetMinBufferSize(m_dwLength);
_tcscpy(m_pBuffer, pszBuffer);
delete pszBuffer;
}
}
#endif
#endif
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)
{
CVOString strReturn(*this);
strReturn += pcszAppend;
return strReturn;
}
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;
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 < 0 || dwOffset > m_dwLength)
return CVOString(TEXT(""));
if(nLength == -1)
nLength = m_dwLength - dwOffset;
else if(dwOffset + nLength > m_dwLength)
nLength = m_dwLength - dwOffset;
LPTSTR pszTemp = new TCHAR[nLength + 1];
memset(pszTemp, 0, sizeof(TCHAR) * (nLength + 1));
memmove(pszTemp, m_pBuffer + dwOffset, sizeof(TCHAR) * 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(2048), pcszFormat, vl);
ReleaseBuffer();
va_end(vl);
return 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)
{
if((int)GetLength() < nChars)
return *this;
return Mid(GetLength() - nChars);
}
void CVOString::MakeMixedCase()
{
BOOL fNextCaps = TRUE;
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(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;
else if(strWord == TEXT("from"))
fCapitalize = FALSE;
else if(strWord == TEXT("in"))
fCapitalize = FALSE;
else if(strWord == TEXT("into"))
fCapitalize = FALSE;
else if(strWord == TEXT("of"))
fCapitalize = FALSE;
else if(strWord == TEXT("off"))
fCapitalize = FALSE;
else if(strWord == TEXT("on"))
fCapitalize = FALSE;
else if(strWord == TEXT("onto"))
fCapitalize = FALSE;
else if(strWord == TEXT("out"))
fCapitalize = FALSE;
else if(strWord == TEXT("to"))
fCapitalize = FALSE;
else if(strWord == TEXT("up"))
fCapitalize = FALSE;
else if(strWord == TEXT("with "))
fCapitalize = FALSE;
}
if(fCapitalize)
strWord.m_pBuffer[0] = strWord.m_pBuffer[0] - (TCHAR('a') - TCHAR('A'));
memmove(m_pBuffer + i, strWord.m_pBuffer, strWord.GetLength() * sizeof(TCHAR));
i = nWordEnd - 1;
}
else if(m_pBuffer[i] == TCHAR('.') || m_pBuffer[i] == TCHAR('?') || m_pBuffer[i] == TCHAR('!'))
fFirstWord = TRUE;
}
}
void CVOString::MakeLower()
{
for(int i = 0; i < (int)GetLength(); i++)
{
if(m_pBuffer[i] >= TCHAR('A') && m_pBuffer[i] <= TCHAR('Z'))
m_pBuffer[i] = m_pBuffer[i] + (TCHAR('a') - TCHAR('A'));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -