made a char wrapper: Char, which implements copy-on-write for String

This commit is contained in:
Kjistóf 2016-10-23 15:27:00 +02:00
parent cd7d35da9a
commit 8dcfca2d62
2 changed files with 40 additions and 0 deletions

20
Char.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "Char.h"
#include <cstring>
Char::operator char() const
{ return _string._str->operator[](_index); }
Char& Char::operator=(char other)
{
auto data = new char[_string.size() + 2]; // space for new char & null-terminator
std::strcpy(data, _string.c_str());
data[_index] = other;
_string._str->operator--();
_string._str = new StringValue(data);
return *this;
}

20
Char.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include "String.h"
class String;
class StringValue;
class Char
{
private:
size_t _index;
String& _string;
public:
Char(size_t index, String& string) :_index(index), _string(string) {}
operator char() const;
Char& operator=(char other);
};