#ifndef ALREADY_INCLUDED_STRING_HH
#define ALREADY_INCLUDED_STRING_HH
#include <string.h>
#include "../noallegro/my-allegro.hh"
class String_Internal
{
public:
static const int MIN_LENGTH = 50; int ref_count;
int length;
int maxlength;
char array[1];
public:
void init(int maxlength)
{
this->ref_count = 1;
this->length = 0;
this->maxlength = maxlength;
this->array[0] = 0;
}
};
class string
{
protected:
String_Internal* ptr;
void worker_current_down() { if (--ptr->ref_count == 0) { delete ptr; ptr = null; } }
void worker_new_up(const string& s) { s.ptr->ref_count++; }
void worker_mutator_clone();
friend int my_strncpy(char* to, const char* from, int mem_size);
public:
string();
string(const string& s);
string& operator = (const string& s);
~string();
string(char ch);
string(int i);
string(double d);
string(const char* s);
string& operator = (const char* s);
string& operator += (const string& s);
string& operator += (const char* s);
bool equals(const string& other) const
{
return !(strcmp(this->ptr->array, other.ptr->array));
}
bool equals(const char* other) const
{
return !(strcmp(this->ptr->array, other));
}
const char* const_char_star() const {
return ptr->array;
}
char* char_star(int mem_size, char * s) const;
char get_char_at(int index) const;
int get_length() const;
friend bool strings_equal(const char* s1, const string& s2) {
if ((s1 == null) && (&s2 == null)) {
return true;
} else if ((s1 == null) || (&s2 == null)) {
return false;
} else {
return strcmp(s1, s2.ptr->array) == 0;
}
}
friend bool strings_equal(const string& s1, const char* s2) {
if ((&s1 == null) && (s2 == null)) {
return true;
} else if ((&s1 == null) || (s2 == null)) {
return false;
} else {
return (0 == strcmp(s1.ptr->array, s2));
}
}
friend bool strings_equal(const string& s1, const string& s2) {
if ((&s1 == null) && (&s2 == null)) {
return true;
} else if ((&s1 == null) || (&s2 == null)) {
return false;
} else {
return (0 == strcmp(s1.ptr->array, s2.ptr->array));
}
}
};
extern string substring(const string& s, int start, int stop);
extern string quoted(const string& s);
extern string reversed(const string& s);
extern string capitalised(const string& s);
extern int index_of(const string& s, const string& key);
#endif