- // xstring.h: interface for the xstring class.
//
// - #if !defined(AFX_XSTRING_H__3B66E9A3_9BA0_4D18_85B8_62060F270795__INCLUDED_)
#define AFX_XSTRING_H__3B66E9A3_9BA0_4D18_85B8_62060F270795__INCLUDED_ - #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000 - #include <string.h>
#include <memory.h> - typedef char s8;
typedef unsigned long s64; - class xstring
{
public:
xstring();
xstring(const char* szValue);
xstring(xstring& xstr);
virtual ~xstring();
public:
xstring& operator =(const xstring &xstr);
xstring& operator =(const char* lpcszValue);
xstring& operator +(const char* lpcszValue);
xstring& operator +(const xstring &xstr);
xstring& operator +(long lnValue);
//强制转换.
//operator const char*() const;
//friend xstring operator +(long lnValue,const xstring &xstr);
public:
//返回字符串指针.
const char* c_str();
//获取字符串长度.
s64 getLength();
//获取指定位置的字符.
char getChar(s64 nPos);
//查找字符串.
long findString(const char* lpszStr,s64 nStartPos = 0);
//格式化字符串
xstring& format(const char* lpformat,...);
//查找全部替换
xstring& replace(const char* lpszSrc,const char* lpszDst);
//替换指定位置字符串
xstring& replace(s64 spos,s64 epos, const char* lpszDst);
public:
s8 setData(const char* lpszStr,s64 nLen);
s8 appendData(const char* lpczValue,s64 nlen);
private:
s8 renewString(const char* lpczValue,s64 nlen);
s8 freeString();
private:
char* m_lpszValue;
s64 m_nLong;
};
#endif // !defined(AFX_XSTRING_H__3B66E9A3_9BA0_4D18_85B8_62060F270795__INCLUDED_)- // xstring.cpp: implementation of the xstring class.
//
// - #include "stdafx.h"
#include "xstring.h"
//
// Construction/Destruction
//- xstring::xstring():
m_nLong(0),
m_lpszValue(NULL)
{
}
xstring::xstring(const char* szValue):
m_nLong(0),
m_lpszValue(NULL)
{
this->renewString(szValue,strlen(szValue));
} - xstring::xstring(xstring &xstr):
m_nLong(0),
m_lpszValue(NULL)
{
m_nLong = 0;
m_lpszValue = NULL;
this->renewString(xstr.m_lpszValue,xstr.m_nLong);
}
xstring::~xstring()
{
freeString();
}
//
// private:
s8 xstring::renewString(const char* lpczValue,s64 nlen){
try
{
m_nLong = nlen;
PCHAR pstr = new TCHAR[nlen + 1];
memset(pstr,0,nlen + 1);
memcpy(pstr,lpczValue,nlen);
freeString();
m_lpszValue = pstr;
}
catch (...)
{
m_lpszValue = NULL;
return FALSE;
}
return TRUE;
}- s8 xstring::freeString(){
if (m_lpszValue != NULL)
{
delete m_lpszValue;
m_lpszValue = NULL;
}
return TRUE;
} - s64 xstring::getLength(){
return m_nLong;
} - s8 xstring::setData(const char* lpszStr,s64 nLen){
return renewString(lpszStr,nLen);
} - s8 xstring::appendData(const char* lpczValue,s64 nlen){
if (nlen <= 0)return TRUE;
try
{
PCHAR p = new TCHAR[m_nLong + nlen + 1];
memset(p,0,m_nLong + nlen + 1);
memcpy(p,m_lpszValue,m_nLong);
memcpy(p + m_nLong , lpczValue , nlen);
freeString();
m_lpszValue = p;
m_nLong = m_nLong + nlen;
}
catch (...)
{
m_lpszValue = NULL;
return FALSE;
}
return TRUE;
}
//
// public:- // 字符串替换函数.
// 能替换所有的要替换的字符串,被替换的字符串和替换的字符串不一定一样长.
// pSrc - 要被替换的子字符串
// pDst - 要替换成的字符串
xstring& xstring::replace(const char* lpszSrc,const char* lpszDst)
{
char *pi, *p;
int nSrcLen, nDstLen, nLen; - xstring xsOut;
pi = m_lpszValue; - // 计算被替换串和替换串的长度.
nSrcLen = strlen(lpszSrc);
nDstLen = strlen(lpszDst);
// 查找pi指向字符串中第一次出现替换串的位置,并返回指针(找不到则返回null).
p = strstr(m_lpszValue, lpszSrc);
if(p)
{
while(p)
{
// 计算被替换串前边字符串的长度.
nLen = (int)(p - pi);
// 复制到输出字符串.
xsOut.appendData(pi,nLen);
xsOut.appendData(lpszDst,nDstLen);
// 跳过被替换串.
pi = p + nSrcLen;
// 继续查找.
p = strstr(pi, lpszSrc);
}
// 复制剩余字符串.
xsOut.appendData(pi,strlen(pi));
this->renewString(xsOut.m_lpszValue,xsOut.m_nLong);
}
return *this;
} - xstring& xstring::replace(s64 spos,s64 epos,const char* lpszDst){
xstring xsTmp;
xsTmp.appendData(m_lpszValue,spos - 1);
xsTmp.appendData(lpszDst,strlen(lpszDst));
if(epos > m_nLong)epos = m_nLong;
xsTmp.appendData(m_lpszValue + epos,m_nLong - epos);
this->renewString(xsTmp.m_lpszValue,xsTmp.m_nLong);
return *this;
} - xstring& xstring::operator =(const xstring &xstr){
if (this != &xstr)
{
renewString(xstr.m_lpszValue,xstr.m_nLong);
}
return *this;
} - xstring& xstring::operator =(const char* lpcszValue){
renewString(lpcszValue,strlen(lpcszValue));
return *this;
} - xstring& xstring::operator +(const char* lpcszValue){
appendData(lpcszValue,strlen(lpcszValue));
return *this;
} - xstring& xstring::operator +(const xstring &xstr){
appendData(xstr.m_lpszValue,xstr.m_nLong);
return *this;
} - xstring& xstring::operator +(long lnValue){
TCHAR szTmp[128];
memset(szTmp,0,sizeof(szTmp));
_ltoa(lnValue,szTmp,10);
appendData(szTmp,strlen(szTmp));
return *this;
} - /*!强制转换*/
/*
xstring operator +(long lnValue,const xstring &xstr){
TCHAR szTmp[128];
memset(szTmp,0,sizeof(szTmp));
_ltoa(lnValue,szTmp,10);
xstring nxstr(szTmp);
return nxstr + xstr;
} - xstring::operator const char*() const{
return m_lpszValue;
}*/
const char* xstring::c_str(){
return m_lpszValue;
}
char xstring::getChar(s64 nPos){
ASSERT(nPos <= m_nLong);
PCHAR p = m_lpszValue + nPos;
return *p;
}- long xstring::findString(const char* lpszStr,s64 nStartPos /* nStartPos = 0*/){
const char* pValue = m_lpszValue + nStartPos;
const char* p = strstr(pValue,lpszStr);
if(p == NULL)return -1;
return p - m_lpszValue;
} - /*!
format string,
xstring::Format("%d%s%c/n",int,const char*,char);
*/
xstring& xstring::format(const char* lpformat,...){
ASSERT(lpformat != NULL);
xstring xsTmp = lpformat;
xstring xsTmp2 = lpformat;
long nPos = 0;
long nLast = 0;
try
{
va_list argList;
va_start(argList,lpformat);
nPos = xsTmp.findString("%",nPos);
while (nPos >= 0)
{
TCHAR c = xsTmp.getChar(nPos+1);
switch (c)
{
case 'c':
case 's':
{
char* s = va_arg(argList,char*);
xsTmp.replace(nPos + 1,nPos + 2,s);
nLast = nPos + strlen(s);
nPos = xsTmp.findString("%",nPos + strlen(s));
}
break;
case 'd':
{
int n = va_arg(argList,int);
TCHAR szTmp[128];
memset(szTmp,0,sizeof(szTmp));
_ltoa(n,szTmp,10);
xsTmp.replace(nPos + 1,nPos + 2,szTmp);
nLast = nPos + strlen(szTmp);
nPos = xsTmp.findString("%",nPos + strlen(szTmp));
}
break;
case '//':
case '%':
case '/"':
{
xsTmp.replace(nPos + 1,nPos + 2,&c);
nLast = nPos + strlen(&c);
nPos = xsTmp.findString("%",nPos + strlen(&c));
}
break;
default:
{
}
}
}
this->renewString(xsTmp.m_lpszValue,xsTmp.m_nLong);
va_end(argList);
}
catch (...)
{
}
return *this;
} - ///
///