#ifndef already_included_string_hh
#define already_included_string_hh
#include "io.hh"
class string;
final class string : public Writer
{
private:
class String_Internal
{
friend bool strings_equal(const string& a, const string& b);
friend bool strings_equal(const char* a, const string& b);
friend bool strings_equal(const string& a, const char* b);
friend Writer& operator << (Writer& w, const string& s);
private:
friend class string;
const int ARRAY_SIZE = 1000;
const int MAX_LENGTH = 1000-1; int ref_count;
int length;
char array[ARRAY_SIZE]; String_Internal() {
ref_count = 1;
length = 0;
array[0] = 0;
}
};
String_Internal* ptr;
void worker_current_down() {
ptr->ref_count--;
if (ptr->ref_count == 0) {
delete ptr;
ptr = null;
}
}
void worker_new_up(const string& s) {
ptr = s.ptr;
ptr->ref_count++;
}
void worker_mutator_clone() {
if (ptr->ref_count > 1) {
ptr->ref_count--;
String_Internal* old_ptr = ptr;
ptr = new String_Internal();
ptr->length = my_strncpy(ptr->array,old_ptr->array,
String_Internal::ARRAY_SIZE);
}
}
static int my_strncpy(char* to, const char* from, int mem_size);
public:
string()
{
ptr = new String_Internal();
}
string(const string& s)
{
worker_new_up(s);
}
string& operator = (const string& s)
{
worker_current_down();
worker_new_up(s);
return *this;
}
~string()
{
worker_current_down();
}
string(const char* s)
{
ptr = new String_Internal();
ptr->length = my_strncpy(ptr->array,s,String_Internal::ARRAY_SIZE);
}
string& operator = (const char* s)
{
worker_current_down();
ptr = new String_Internal();
ptr->length = my_strncpy(ptr->array,s,String_Internal::ARRAY_SIZE);
return *this;
}
friend bool strings_equal(const string& a, const string& b)
{
return (strings_equal(a.ptr->array,b.ptr->array));
}
friend bool strings_equal(const string& a, const char* b)
{
return (strings_equal(a.ptr->array,b));
}
friend bool strings_equal(const char* a, const string& b)
{
return (strings_equal(a,b.ptr->array));
}
friend Writer& operator << (Writer& w, const string& s);
char* char_star(char* s, int mem_size) const;
Writer& operator << (char ch);
Writer& operator << (int i);
Writer& operator << (double d);
Writer& operator << (const char* s);
string quoted() const;
string reversed() const;
string capitalised() const;
string substring(int start, int stop) const;
void set_char_at(int index, char ch);
char get_char_at(int index) const;
int get_length() const;
private:
static int number_of_errors;
};
#endif