made a char wrapper: Char, which implements copy-on-write for String
This commit is contained in:
parent
cd7d35da9a
commit
8dcfca2d62
20
Char.cpp
Normal file
20
Char.cpp
Normal 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
20
Char.h
Normal 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);
|
||||
};
|
Loading…
Reference in New Issue
Block a user