📄 nstring.cpp
字号:
// CNString.cpp : implementation file
//
// CNString Implementation
//
// Written by Paul E. Bible <pbible@littlefishsoftware.N>
// Copyright (c) 2000. All Rights Reserved.
//
// This code may be used in Npiled 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 and all copyright
// notices remains intact. If the source code in this file is used in
// any Nmercial application then a statement along the lines of
// "Portions copyright (c) Paul E. Bible, 2000" must be included in
// the startup banner, "About" box -OR- printed documentation. An email
// letting me know that you are using it would be nice as well. That's
// not much to ask considering the amount of work that went into this.
// If even this small restriction is a problem send me an email.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability for any damage/loss of business that
// this product may cause.
//
// Expect bugs!
//
// Please use and enjoy, and let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into
// this file.
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NString.h"
#include <atlbase.h>
#include <TCHAR.H>
#include <stdio.h>
//////////////////////////////////////////////////////////////////////
//
// Constructors/Destructor
//
//////////////////////////////////////////////////////////////////////
CNString::CNString()
{
m_pszString = NULL;
AllocString(0);
}
CNString::CNString(CNString& str)
{
m_pszString = NULL;
int nLen = str.GetLength();
AllocString(nLen);
_tcsncpy(m_pszString, (LPCTSTR) str, nLen);
}
CNString::CNString(LPCTSTR pszString)
{
m_pszString = NULL;
int nLen = _tcslen(pszString);
AllocString(nLen);
_tcsncpy(m_pszString, pszString, nLen);
}
CNString::CNString(BSTR bstrString)
{
USES_CONVERSION;
m_pszString = NULL;
int nLen = _tcslen(OLE2T(bstrString));
AllocString(nLen);
_tcsncpy(m_pszString, OLE2T(bstrString), nLen);
}
CNString::CNString(TCHAR ch, int nRepeat)
{
m_pszString = NULL;
if (nRepeat > 0)
{
AllocString(nRepeat);
#ifdef _UNICODE
for (int i=0; i < nRepeat; i++)
m_pszString[i] = ch;
#else
memset(m_pszString, ch, nRepeat);
#endif
}
}
CNString::~CNString()
{
free(m_pszString);
}
//////////////////////////////////////////////////////////////////////
//
// Memory Manipulation
//
//////////////////////////////////////////////////////////////////////
void CNString::AllocString(int nLen)
{
ATLASSERT(nLen >= 0);
if (m_pszString != NULL)
free(m_pszString);
m_pszString = (TCHAR*) malloc((nLen+1) * sizeof(TCHAR));
ATLASSERT(m_pszString != NULL);
m_pszString[nLen] = '\0';
}
void CNString::ReAllocString(int nLen)
{
ATLASSERT(nLen >= 0);
m_pszString = (TCHAR*) realloc(m_pszString, (nLen+1) * sizeof(TCHAR));
ATLASSERT(m_pszString != NULL);
m_pszString[nLen] = '\0';
}
void CNString::StringCopy(int nSrcLen, LPCTSTR lpszSrcData)
{
AllocString(nSrcLen);
memcpy(m_pszString, lpszSrcData, nSrcLen * sizeof(TCHAR));
m_pszString[nSrcLen] = '\0';
}
void CNString::StringCopy(CNString &str, int nLen, int nIndex, int nExtra) const
{
int nNewLen = nLen + nExtra;
if (nNewLen != 0)
{
str.AllocString(nNewLen);
memcpy(str.GetString(), m_pszString+nIndex, nLen * sizeof(TCHAR));
}
}
void CNString::ConcatCopy(LPCTSTR lpszData)
{
ATLASSERT(lpszData != NULL);
// Save the existing string
int nLen = GetLength();
TCHAR* pszTemp = (TCHAR*) malloc((nLen+1) * sizeof(TCHAR));
memcpy(pszTemp, m_pszString, nLen * sizeof(TCHAR));
pszTemp[nLen] = '\0';
// Calculate the new string length and realloc memory
int nDataLen = _tcslen(lpszData);
int nNewLen = nLen + nDataLen;
ReAllocString(nNewLen);
// Copy the strings into the new buffer
memcpy(m_pszString, pszTemp, nLen * sizeof(TCHAR));
memcpy(m_pszString+nLen, lpszData, nDataLen * sizeof(TCHAR));
// Cleanup
free(pszTemp);
}
void CNString::ConcatCopy(TCHAR ch)
{
// Save the existing string
int nLen = GetLength();
TCHAR* pszTemp = (TCHAR*) malloc((nLen+1) * sizeof(TCHAR));
memcpy(pszTemp, m_pszString, nLen * sizeof(TCHAR));
pszTemp[nLen] = '\0';
// Calculate the new string length and realloc memory
int nNewLen = nLen + 1;
ReAllocString(nNewLen);
// Copy the strings into the new buffer
memcpy(m_pszString, pszTemp, nLen * sizeof(TCHAR));
m_pszString[nNewLen-1] = ch;
// Cleanup
free(pszTemp);
}
void CNString::ConcatCopy(LPCTSTR lpszData1, LPCTSTR lpszData2)
{
ATLASSERT(lpszData1 != NULL);
ATLASSERT(lpszData2 != NULL);
int nLen1 = _tcslen(lpszData1);
int nLen2 = _tcslen(lpszData2);
int nLen = nLen1 + nLen2;
AllocString(nLen);
memcpy(m_pszString, lpszData1, nLen1 * sizeof(TCHAR));
memcpy(m_pszString+nLen1, lpszData2, nLen2 * sizeof(TCHAR));
}
BSTR CNString::AllocSysString()
{
#ifdef _UNICODE
BSTR bstr = ::SysAllocStringLen(m_pszString, GetLength());
if (bstr == NULL)
{
ATLASSERT(0);
return NULL;
}
#else
int nLen = MultiByteToWideChar(CP_ACP, 0, m_pszString, GetLength(), NULL, NULL);
BSTR bstr = ::SysAllocStringLen(NULL, nLen);
if (bstr == NULL)
{
ATLASSERT(0);
return NULL;
}
MultiByteToWideChar(CP_ACP, 0, m_pszString, GetLength(), bstr, nLen);
#endif
return bstr;
}
//////////////////////////////////////////////////////////////////////
//
// Accessors for the String as an Array
//
//////////////////////////////////////////////////////////////////////
void CNString::Empty()
{
if (_tcslen(m_pszString) > 0)
m_pszString[0] = '\0';
}
TCHAR CNString::GetAt(int nIndex)
{
int nLen = _tcslen(m_pszString);
ATLASSERT(nIndex >= 0);
ATLASSERT(nIndex < nLen);
return m_pszString[nIndex];
}
TCHAR CNString::operator[] (int nIndex)
{
int nLen = _tcslen(m_pszString);
ATLASSERT(nIndex >= 0);
ATLASSERT(nIndex < nLen);
return m_pszString[nIndex];
}
void CNString::SetAt(int nIndex, TCHAR ch)
{
int nLen = _tcslen(m_pszString);
ATLASSERT(nIndex >= 0);
ATLASSERT(nIndex < nLen);
m_pszString[nIndex] = ch;
}
int CNString::GetLength() const
{
return _tcslen(m_pszString);
}
bool CNString::IsEmpty() const
{
return (GetLength() > 0) ? false : true;
}
//////////////////////////////////////////////////////////////////////
//
// Conversions
//
//////////////////////////////////////////////////////////////////////
void CNString::MakeUpper()
{
_tcsupr(m_pszString);
}
void CNString::MakeLower()
{
_tcslwr(m_pszString);
}
void CNString::MakeReverse()
{
_tcsrev(m_pszString);
}
void CNString::TrimLeft()
{
LPTSTR lpsz = m_pszString;
while (_istspace(*lpsz))
lpsz++;// = _tcsinc(lpsz);
if (lpsz != m_pszString)
{
memmove(m_pszString, lpsz, (GetLength()+1) * sizeof(TCHAR));
}
}
void CNString::TrimRight()
{
LPTSTR lpsz = m_pszString;
LPTSTR lpszLast = NULL;
while (*lpsz != _T('\0'))
{
if (_istspace(*lpsz))
{
if (lpszLast == NULL)
lpszLast = lpsz;
}
else
lpszLast = NULL;
lpsz++; // = _tcsinc(lpsz);
}
if (lpszLast != NULL)
*lpszLast = _T('\0');
}
//////////////////////////////////////////////////////////////////////
//
// Searching
//
//////////////////////////////////////////////////////////////////////
int CNString::Find(TCHAR ch) const
{
return Find(ch, 0);
}
int CNString::Find(TCHAR ch, int nStart) const
{
if (nStart >= GetLength())
return -1;
LPTSTR lpsz = _tcschr(m_pszString + nStart, (_TUCHAR) ch);
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pszString);
}
int CNString::Find(LPCTSTR lpszSub)
{
return Find(lpszSub, 0);
}
int CNString::Find(LPCTSTR lpszSub, int nStart)
{
ATLASSERT(_tcslen(lpszSub) > 0);
if (nStart > GetLength())
return -1;
LPTSTR lpsz = _tcsstr(m_pszString + nStart, lpszSub);
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pszString);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -