刘晶ID:Tomsdinary
5771次访问,排名15961(1)好友0人,关注者1
我是一个喜欢C++热爱底层编程及算法设计的学生。
Tomsdinary的文章
原创 22 篇
翻译 1 篇
转载 0 篇
评论 1 篇
最近评论
agjyfm:wow gold
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 字符串收藏

    新一篇: 双向循环链表 | 旧一篇: 单链表

    /**
    文件名:       String.h
    作者:         Tomsdinary
    备注:         本文件中定义字符串类的接口
                   注意此字符串采用最简单的引用计数实现
    最后修改时间: 2007年10月21日
    */


    #ifndef _STRING_REF_HPP_
    #define _STRING_REF_HPP_

    #include 
    <cstddef>

    namespace Tomsdinary
    {
        
    class String
        
    {
        
    public:
            
    //C内建字符串构建
            explicit String (const char* value="");
            
    //复制构造
            String (const String& other);
            
    //析构
            ~String ();
            
    //重载赋值
            String& operator= (const String& other);
            String
    & operator= (const char* value);
            
    //获得字符串长度
            std::size_t GetLength () const;
            
    //重载下标运算符 常数版本
            const char& operator[] (const std::size_t& index) const;
            
    //重载下标运算符 读写版本
            char& operator[] (const std::size_t& index);
            
    //获得内建C风格字符串
            const char* const c_str () const;
            
    //判断字符串是否为空
            bool IsEmpty () const;
            
    //友元小于运算符重载
            friend bool operator< (const String& lbs, const String& rbs);
            friend 
    bool operator< (const String& lbs, const char* rbs);
            friend 
    bool operator< (const char* lbs, const String& rbs);
            
    //友元等于运算符重载
            friend bool operator== (const String& lbs, const String& rbs);
            friend 
    bool operator== (const String& lbs, const char* rbs);
            friend 
    bool operator== (const char* lbs, const String& rbs);
            
    //清空字符串
            void Clear ();
            
    //追加字符串对象
            void Append (const String& other);
            
    //追加C风格字符串
            void Append (const char* str);
            
    //追加字符
            void Append (const char& c);
            
    //友元重载连字符
            friend String operator+ (const String& lbs, const String& rbs);
            friend String 
    operator+ (const String& lbs, const char* rbs);
            friend String 
    operator+ (const char* lbs, const String& rbs);
            
    //重载连字赋值运算符
            String& operator+= (const String& rbs);
            String
    & operator+= (const char* rbs);
            
    //插入一个字符到字符串
            void Insert (const std::size_t& index, const char& c);
            
    //插入一个C风格字符串到字符串对象
            void Insert (const std::size_t& index, const char* s);
            
    //插入一个字符串到字符串
            void Insert (const std::size_t& index, const String& str);

            
    //获得从指定位置开始的子串
            String GetSubString (const std::size_t& start) const;
            
    //获得从指定位置开始长度为count的子串
            String GetSubString (const std::size_t& start, const std::size_t& count) const;
            
    //删除从指定位置开始的子串
            void RemoveSubString (const std::size_t& start);
            
    //删除从指定位置开始长度为count的子串
            void RemoveSubString (const std::size_t& start, const std::size_t& count);
        
    private:
            
    //字符串实际值结构
            struct StringValue
            
    {
                
    //基址指针
                char* m_base;
                
    //引用计数变量
                std::size_t m_ref;
                
    //引用共享变量
                bool m_shareable;
                StringValue (
    const char* value="");
                
    ~StringValue ();
            }
    ;// end of struct StringValue
        private:
            
    //字符串实际值智能指针
            class StringPointer
            
    {
            
    public:
                StringPointer (
    const char* value="");
                StringPointer (
    const StringPointer& other);
                
    ~StringPointer ();
                StringPointer
    & operator= (const StringPointer& other);
                StringValue
    & operator* () const;
                StringValue
    * operator-> () const;
                
    bool operator!= (const StringPointer& other);
            
    private:
                
    //内部对象
                StringValue* m_value;
            }
    ;// end of class StringPointer
        private:
            StringPointer m_string;
        }
    ;// end of class String
    }


    #endif



    /**
    文件名:       String.cpp
    作者:         Tomsdinary
    备注:         本文件中定义字符串类的实现
                   注意此字符串采用最简单的引用计数
    最后修改时间: 2007年10月21日
    */


    #include 
    "stdafx.h"
    #include 
    "String.h"
    #include 
    <string.h>

    namespace Tomsdinary
    {
        
    //---------- begin: String::StringValue ----------//
        String::StringValue::StringValue (const char* value)
        
    {
            m_base
    =new char[strlen(value)+1];
            strcpy(m_base, value);
            m_ref
    =1;
            m_shareable
    =true;
        }


        String::StringValue::
    ~StringValue ()
        
    {
            delete[] m_base;
        }

        
    //---------- end: String::StringValue ----------//

        
    //---------- begin: String::StringPointer ----------//
        String::StringPointer::StringPointer (const char* value)
        
    {
            m_value
    =new StringValue(value);
        }


        String::StringPointer::StringPointer (
    const StringPointer& other)
        
    {
            
    if (other.m_value->m_shareable)
            
    {
                m_value
    =other.m_value;
                
    ++m_value->m_ref;
            }

            
    else
            
    {
                m_value
    =new StringValue(other.m_value->m_base);
            }

        }


        String::StringPointer::
    ~StringPointer ()
        
    {
            
    if (--m_value->m_ref==0)
            
    {
                delete m_value;
            }

        }


        String::StringPointer
    & String::StringPointer::operator = (const String::StringPointer& other)
        
    {
            
    if (m_value!=other.m_value)
            
    {
                
    if (--m_value->m_ref==0)
                
    {
                    delete m_value;
                }

                
    if (other.m_value->m_shareable)
                
    {
                    m_value
    =other.m_value;
                    
    ++m_value->m_ref;
                }

                
    else
                
    {
                    m_value
    =new StringValue(other.m_value->m_base);
                }

            }

            
    return *this;
        }


        String::StringValue
    & String::StringPointer::operator* () const
        
    {
            
    return *m_value;
        }


        String::StringValue
    * String::StringPointer::operator-> () const
        
    {
            
    return m_value;
        }


        
    bool String::StringPointer::operator!= (const String::StringPointer& other)
        
    {
            
    return m_value!=other.m_value;
        }

        
    //---------- end: String::StringPointer ----------//

        
    //---------- begin: String ----------//
        String::String (const char* value) : m_string(value)
        
    {
            
    //do nothing else
        }


        String::String (
    const String& other) : m_string(other.m_string)
        
    {
            
    //do nothing else
        }


        String::
    ~String ()
        
    {
            
    //do nothing else
        }

        
        String
    & String::operator= (const String& other)
        
    {
            
    if (m_string!=other.m_string)
            
    {
                m_string
    =other.m_string;
            }

            
    return *this;
        }


        String
    & String::operator = (const char* value)
        
    {
            m_string
    =StringPointer(value);
            
    return *this;
        }


        std::size_t String::GetLength () 
    const
        
    {
            
    return strlen(m_string->m_base);
        }


        
    const char& String::operator[] (const std::size_t& index) const
        
    {
            
    return m_string->m_base[index];
        }


        
    char& String::operator[] (const std::size_t& index)
        
    {
            
    if (m_string->m_shareable)
            
    {
                StringPointer temp
    =m_string;
                m_string
    =StringPointer(temp->m_base);
                m_string
    ->m_shareable=false;
            }

            
    return m_string->m_base[index];
        }


        
    const char* const String::c_str () const
        
    {
            
    return m_string->m_base;
        }


        
    bool String::IsEmpty () const
        
    {
            
    return strlen(m_string->m_base)==0;
        }


        
    bool operator< (const String& lbs, const String& rbs)
        
    {
            
    return strcmp(lbs.m_string->m_base, rbs.m_string->m_base)<0;
        }


        
    bool operator< (const String& lbs, const char* rbs)
        
    {
            
    return strcmp(lbs.m_string->m_base, rbs)<0;
        }


        
    bool operator< (const char* lbs, const String& rbs)