自已的xstring,string类

  1. // xstring.h: interface for the xstring class.
    //
    //
  2. #if !defined(AFX_XSTRING_H__3B66E9A3_9BA0_4D18_85B8_62060F270795__INCLUDED_)
    #define AFX_XSTRING_H__3B66E9A3_9BA0_4D18_85B8_62060F270795__INCLUDED_
  3. #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
  4. #include <string.h>
    #include <memory.h>
  5. typedef char   s8;
    typedef unsigned long s64;
  6. 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;
    };

  7. #endif // !defined(AFX_XSTRING_H__3B66E9A3_9BA0_4D18_85B8_62060F270795__INCLUDED_)
  8. // xstring.cpp: implementation of the xstring class.
    //
    //
  9. #include "stdafx.h"
    #include "xstring.h"

  10. //
    // Construction/Destruction
    //
  11. xstring::xstring():
            m_nLong(0),
            m_lpszValue(NULL)
    {
    }
           
    xstring::xstring(const char* szValue):
    m_nLong(0),
    m_lpszValue(NULL)
    {
        this->renewString(szValue,strlen(szValue));
    }
  12. xstring::xstring(xstring &xstr):
    m_nLong(0),
    m_lpszValue(NULL)
    {
        m_nLong = 0;
        m_lpszValue = NULL;
        this->renewString(xstr.m_lpszValue,xstr.m_nLong);
    }

  13. xstring::~xstring()
    {
        freeString();
    }

  14. //
    // 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;
    }
  15. s8 xstring::freeString(){
        if (m_lpszValue != NULL)
        {
            delete m_lpszValue;
            m_lpszValue = NULL;
        }
        return TRUE;
    }
  16. s64 xstring::getLength(){
        return m_nLong;
    }
  17. s8 xstring::setData(const char* lpszStr,s64 nLen){
        return renewString(lpszStr,nLen);
    }
  18. 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;
    }

  19. //
    // public:
  20. // 字符串替换函数.
    // 能替换所有的要替换的字符串,被替换的字符串和替换的字符串不一定一样长.
    // pSrc - 要被替换的子字符串
    // pDst - 要替换成的字符串
    xstring& xstring::replace(const char* lpszSrc,const char* lpszDst)
    {
        char    *pi, *p;
        int     nSrcLen, nDstLen, nLen;
  21.     xstring xsOut;
        pi = m_lpszValue;
  22.     // 计算被替换串和替换串的长度.
        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;
    }
  23. 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;
    }
  24. xstring& xstring::operator =(const xstring &xstr){
        if (this != &xstr)
        {
            renewString(xstr.m_lpszValue,xstr.m_nLong);
        }
        return *this;
    }
  25. xstring& xstring::operator =(const char* lpcszValue){
        renewString(lpcszValue,strlen(lpcszValue));
        return *this;
    }
  26. xstring& xstring::operator +(const char* lpcszValue){
        appendData(lpcszValue,strlen(lpcszValue));
        return *this;
    }
  27. xstring& xstring::operator +(const xstring &xstr){
        appendData(xstr.m_lpszValue,xstr.m_nLong);
        return *this;
    }
  28. xstring& xstring::operator +(long lnValue){
        TCHAR szTmp[128];
        memset(szTmp,0,sizeof(szTmp));
        _ltoa(lnValue,szTmp,10);
        appendData(szTmp,strlen(szTmp));
        return *this;
    }
  29. /*!强制转换*/
    /*
    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;
    }
  30. xstring::operator const char*() const{
        return m_lpszValue;
    }*/

  31. const char* xstring::c_str(){
        return m_lpszValue;
    }

  32. char xstring::getChar(s64 nPos){
        ASSERT(nPos <= m_nLong);
        PCHAR p = m_lpszValue + nPos;
        return *p;
    }
  33. 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;
    }
  34. /*!
    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;
    }
  35. ///

 

 

 

 

 

 

 


///

 
 
 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白錵錵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值