From 8dcfca2d62f1e17ef515d293d1a73353e16377db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 23 Oct 2016 15:27:00 +0200 Subject: [PATCH] made a char wrapper: Char, which implements copy-on-write for String --- Char.cpp | 20 ++++++++++++++++++++ Char.h | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 Char.cpp create mode 100644 Char.h diff --git a/Char.cpp b/Char.cpp new file mode 100644 index 0000000..e610e01 --- /dev/null +++ b/Char.cpp @@ -0,0 +1,20 @@ +#include "Char.h" +#include + + + +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; +} \ No newline at end of file diff --git a/Char.h b/Char.h new file mode 100644 index 0000000..fb5cdc4 --- /dev/null +++ b/Char.h @@ -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); +}; \ No newline at end of file