C++:String的写时拷贝

String的写时拷贝

//test.h
#pragma once

#include <iostream>
#include <string.h>
#include <cstdio>
#include <assert.h>
using namespace std;

#define TESTHEADER printf("\n================%s=============\n", __FUNCTION__)
class String
{
public:
    String(const char* str);
    String(const String& s);
    String& operator = (const String s);
    ~String();
    char* c_str();
    void CopyOnWrite();
    char& operator [] (size_t pos);
    const char& operator [] (size_t pos)const;
    String& Insert(size_t pos, char ch);
    String& Insert(size_t pos, const char* str);
    String& Append(const char* str);
    String& Erase(size_t pos, size_t n);
    void Expand(size_t n);
private:
    char* _str;
    int* _pcount;
    size_t _size;
    size_t _capacity;
};

//test.cc
#include "test.h"
String::String(const char* str)
    :_size(strlen(str))
,_capacity(_size)
{
    _str = new char [strlen(str) + 1];
    _pcount = new int(1);
    strcpy(_str, str);
}


//s(s1)
String::String(const String& s)
{
    _str = s._str;
    _pcount = s._pcount;
    ++(*_pcount);
    _size = s._size;
    _capacity = s._capacity;
}

//s1 = s2
String& String:: operator = (const String s) 
{
    if(_str != s._str)
    {
        _str = s._str;
        --(*_pcount);
        if(*_pcount == 0)
        {
            delete [] _str;
            delete _pcount;
        }
        _size = s._size;
        _capacity = s._capacity;
    }
    return *this;
}

String::~String()
{
    delete [] _str;
    delete _pcount;
    _size = 0;
    _capacity = 0;
}
char* String::c_str()
{
    return _str;
}

void String::CopyOnWrite()
{
    if(*_pcount > 1)
    {
        char* tmp = new char[_size + 1];
        strcpy(tmp, _str);
        --(*_pcount);
         _str = tmp;
         _pcount = new int(1);
    }
}

//s[1]
char& String::operator [] (size_t pos)
{
    CopyOnWrite();
    return _str[pos];
}

const char& String:: operator [] (size_t pos)const
{
    return _str[pos];
}

void String::Expand(size_t n)
{
    if( *_pcount  == 1)
    {
        char* tmp = new char [n];
        strcpy(tmp, _str);
        delete [] _str;
        --(*_pcount);
        delete _pcount;
        _pcount = new int(1);
        _str = tmp;
    }
    else
    {
        char* tmp = new char [n];
        strcpy(tmp, _str);
        _str = tmp;
        --(*_pcount);
        _pcount = new int(1);
    }
}

String& String::Insert(size_t pos, char ch)
{
    assert(pos < _size);
    if(_size == _capacity)
    {
        Expand(2 * _capacity);
    }
    int end = _size;
    for(; end >= (int)pos; end--)
    {
        _str[end] = _str[end - 1];
    }
    _str[pos] = ch;
    return *this;
}

String& String::Insert(size_t pos, const char* str)
{
    assert(pos <= _size);
    int len = strlen(str);
    if(_size == _capacity)
    {
        Expand(_size + len);
    }
    int end = _size;
    for(; end >= (int)pos; end--)
    {
        _str[end + len] = _str[end];
    }
    strncpy(_str + pos, str, len);
    _size += len;
    /* _str[_size] = '\0'; */
    return *this;
}


String& String::Append(const char* str)
{
    int len_ = strlen(_str);
    int len = strlen(str);
    if(_size == _capacity)
    {
        Expand(_size + len);
    }
    strcpy(_str + len_, str);
    _size += len;
    return *this;
}


String& String::Erase(size_t pos, size_t n)
{
    assert(pos <= _size);
    CopyOnWrite();
    if(pos + _size > _capacity)
    {
        _str[pos] = '\0';
    }
    strcpy(_str + pos, _str + pos + n);
    _size -= n;
    return *this;
}

猜你喜欢

转载自blog.csdn.net/qq_41027326/article/details/81064775