• [C++]仿java.lang.String的字符串工具类[原]


    BwString.h:
    #pragma once
    #include <assert.h>
    
    namespace BW_Util{
    
        class BwString;
    
        //重载符 + const char* 
    
        BwString operator+(BwString& str, const char* pStr);
    
        //重载符 + const wchar_t* 
    
        BwString operator+(BwString& str, const wchar_t* pStr);
    
        //重载符 + EcpString& str
    
        BwString operator+(BwString& str1, BwString& str2);
    
        class BwString
        {
        public:
            BwString(void);
    
            BwString(const char * pStr);
    
            BwString(const wchar_t * pStr);
    
            //构造一个由pStr1和pStr2连接起来的对象。例如L"abc"和L"def"连接起来为L"abcdef"
    
            BwString(const wchar_t * pStr1, const wchar_t * pStr2);
    
            //构造一个由pStr1和pStr2连接起来的对象。例如"abc"和"def"连接起来为L"abcdef"
    
            BwString(const char * pStr1, const char * pStr2);
    
            //构造一个由pStr1和pStr2连接起来的对象。例如L"abc"和"def"连接起来为L"abcdef"
    
            BwString(const wchar_t * pStr1, const char * pStr2);
    
            //构造一个由pStr1和pStr2连接起来的对象。例如"abc"和L"def"连接起来为L"abcdef"
    
            BwString(const char * pStr1, const wchar_t * pStr2);
    
            ~BwString(void);
    
            //重载符 + const char* 
    
            BwString friend BW_Util::operator+(BwString& str, const char* pStr);
    
            //重载符 + const wchar_t* 
    
            BwString friend BW_Util::operator+(BwString& str, const wchar_t* pStr);
    
            //重载符 + EcpString& str
    
            BwString friend BW_Util::operator+(BwString& str1, BwString& str2);
    
            //重载符 = const char* 
    
            wchar_t* operator=(const char* pStr);
    
            //重载符 = const wchar_t* 
    
            wchar_t* operator=(const wchar_t* pStr);
    
            //重载符 = EcpString& str
    
            BwString& operator=(BwString& str);
    
            //重载符 += const char* 
    
            wchar_t* operator+=(const char* pStr);
    
            //重载符 += const wchar_t* 
    
            wchar_t* operator+=(const wchar_t* pStr);
    
            //重载符 += EcpString& str
    
            BwString& operator+=(BwString& str);
    
            //重载符 < EcpString& str:比较两个字符串的大小。
    
            bool operator<(BwString& str);
    
            //重载符 > EcpString& str:比较两个字符串的大小。
    
            bool operator>(BwString& str);
    
            //重载符 (wchar_t*)
    
            operator wchar_t*();
    
            //字符串长度
    
            int Length();
        private:
    
            //指向宽字符串的指针
    
            wchar_t *pw;
    
            //当前字符串的长度(不含结束符0)
    
            int currLen;
    
            //扩容量
    
            int growSize;
    
            //字符串的容量
    
            int maxLen;
    
            //初始化基本的成员变量
    
            void init();
    
            //在当前字符串后追加新字符串
    
            template <class T> wchar_t* Append(const T* pVal);
    
            //计算指定char*/wchar_t*的长度(不包括结束符)。这里定义为私有是因为其只对char*、wchar_t*和int*有效
    
            template <class T> int CharsCount(const T* pStr);
        };
    
    }

    BwString.cpp:

    #include "BwString.h"
    #include <string>
    
    void BW_Util::BwString::init()
    {
        currLen = 0;
        maxLen = 3;
        growSize = 3;
        pw = (wchar_t*)calloc(maxLen, sizeof(wchar_t));
    }
    
    BW_Util::BwString::BwString(void)
    {
        init();
    }
    
    BW_Util::BwString::BwString( const char * pStr )
    {
        init();
        (*this) = pStr;
    }
    
    BW_Util::BwString::BwString( const wchar_t * pStr )
    {
        init();
        (*this) = pStr;
    }
    
    BW_Util::BwString::BwString( const wchar_t * pStr1, const wchar_t * pStr2 )
    {
        init();
        (*this) = pStr1;
        Append(pStr2);
    }
    
    BW_Util::BwString::BwString( const char * pStr1, const char * pStr2 )
    {
        init();
        (*this) = pStr1;
        Append(pStr2);
    }
    
    BW_Util::BwString::BwString( const wchar_t * pStr1, const char * pStr2 )
    {
        init();
        (*this) = pStr1;
        Append(pStr2);
    }
    
    BW_Util::BwString::BwString( const char * pStr1, const wchar_t * pStr2 )
    {
        init();
        (*this) = pStr1;
        Append(pStr2);
    }
    
    BW_Util::BwString::~BwString(void)
    {
        if(pw)
        {
            free(pw);
            pw = 0;
        }
    }
    
    int BW_Util::BwString::Length()
    {
        return currLen;
    }
    
    wchar_t* BW_Util::BwString::operator=(const char* pStr )
    {
        memset(pw, '\0', maxLen);
        currLen = 0; 
        Append(pStr);
        return pw;
    }
    
    wchar_t* BW_Util::BwString::operator=( const wchar_t* pStr )
    {
        memset(pw, '\0', maxLen);
        currLen = 0; 
        Append(pStr);
        return pw;
    }
    
    BW_Util::BwString& BW_Util::BwString::operator=( BwString& str )
    {
        //此时调用了重载符:char* EcpString::operator=(const char* pStr )
    
        (*this) = str.pw;
        return str;
    }
    
    BW_Util::BwString::operator wchar_t*()
    {
        return pw;
    }
    
    wchar_t* BW_Util::BwString::operator+=( const char* pStr )
    {
        Append(pStr);
        return (*this);
    }
    
    wchar_t* BW_Util::BwString::operator+=( const wchar_t* pStr )
    {
        Append(pStr);
        return (*this);
    }
    
    BW_Util::BwString& BW_Util::BwString::operator+=( BwString& str )
    {
        Append(str.pw);
        return (*this);
    }
    
    template <class T>
    wchar_t* BW_Util::BwString::Append( const T* pVal )
    {
        int len = CharsCount(pVal);
        if(len + currLen >= maxLen )
        {
            while( len + currLen >= maxLen)
            {
                maxLen += growSize;
            }
            wchar_t * pwTemp = (wchar_t*)calloc(maxLen, sizeof(wchar_t));
            memcpy(pwTemp, pw, currLen * sizeof(wchar_t));
            free(pw);
            pw = pwTemp;
        }
        
        for(int i=currLen, j = 0; i<len + currLen; i++, j++)
        {
            pw[i] = pVal[j];
        }
    
        currLen += len;
    
        return pw;
    }
    
    bool BW_Util::BwString::operator<( BwString& str )
    {
        for(int i=0; i<currLen; i++)
        {
            if(pw[i] < str.pw[i])
                return true;
        }
        return false;
    }
    
    bool BW_Util::BwString::operator>( BwString& str )
    {
        for(int i=0; i<currLen; i++)
        {
            if(pw[i] > str.pw[i])
                return true;
        }
        return false;
    }
    
    template <class T>
    int BW_Util::BwString::CharsCount( const T* pStr )
    {
        int i=0;
        for(; pStr[i]; i++)
        {}
        return i;
    }
    
    BW_Util::BwString BW_Util::operator+( BwString& str, const char* pStr )
    {
        //这里直接调用构造函数,而不是实例一个对象后在返回这个对象,是为了避免调用拷贝构造函数
    
        return BwString(str.pw, pStr);
    }
    
    BW_Util::BwString BW_Util::operator+( BwString& str, const wchar_t* pStr )
    {
        return BwString(str.pw, pStr);
    }
    
    BW_Util::BwString BW_Util::operator+( BwString& str1, BwString& str2 )
    {
        return BwString(str1.pw, str2.pw);
    }

    测试代码:
    void _BwWString_Test()
    {
        //宽字符串测试
    
        BwString str = L"wangaiguo";
        wcout << "str=" << (wchar_t*)str << endl;    //str=wangaiguo
    
    
        BwString str2;
        str2 = L"wuqian ";
        wcout << "str2=" << (wchar_t*)str2 << endl;    //str2=wuqian
    
    
        str += L" nanjing ";
        wcout << "str=" << (wchar_t*)str << endl;    //str=wangaiguo nanjing
    
    
        str2 += str;
        wcout << "str2=" << (wchar_t*)str2 << endl;    //str2=wuqian wangaiguo nanjing
    
    
        BwString str3, str4;
        str3 = str3 + L"escarp";
        wcout << "str3=" << (wchar_t*)str3 << endl;    //str3=escarp
    
    
        str4 = str + str3;
        wcout << "str4=" << (wchar_t*)str4 << endl;    //str4=wangaiguo nanjing escarp
    
    
        str = str2;
        wcout << "str=" << (wchar_t*)str << endl;    //str=wuqian wangaiguo nanjing
    
    }
    
    void _BwString_Test()
    {
        //普通字符串测试
    
        BwString str = "wangaiguo";
        wcout << "str=" << (wchar_t*)str << endl;    //str=wangaiguo
    
    
        BwString str2;
        str2 = "wuqian ";
        wcout << "str2=" << (wchar_t*)str2 << endl;    //str2=wuqian
    
    
        str += " nanjing ";
        wcout << "str=" << (wchar_t*)str << endl;    //str=wangaiguo nanjing
    
    
        str2 += str;
        wcout << "str2=" << (wchar_t*)str2 << endl;    //str2=wuqian wangaiguo nanjing
    
    
        BwString str3, str4;
        str3 = str3 + "escarp";
        wcout << "str3=" << (wchar_t*)str3 << endl;    //str3=escarp
    
    
        str4 = str + str3;
        wcout << "str4=" << (wchar_t*)str4 << endl;    //str4=wangaiguo nanjing escarp
    
    
        str = str2;
        wcout << "str=" << (wchar_t*)str << endl;    //str=wuqian wangaiguo nanjing
    
    }


  • 相关阅读:
    矩阵运算(二维数组)
    AndroidManifest.xml
    单位和尺寸
    java Map集合类
    http相关
    文件管理与XMl、JSON解析
    Handler与多线程
    App内容分享
    Fragment以及懒加载
    广播接收器与短信
  • 原文地址:https://www.cnblogs.com/black/p/5171984.html
Copyright © 2020-2023  润新知