
#include "AwaString.h"
#include <string.h>

AwaString::AwaString()
{
	_size = 1;
	_data = new char[1];
	*_data = 0;
}

AwaString::AwaString(const AwaString& rhs)
{
	_size = rhs._size;
	_data = new char[_size];
	strcpy(_data, rhs._data);
}

AwaString::AwaString(const char* c)
{
	_size = strlen(c) + 1;
	_data = new char[_size];
	strcpy(_data, c);
}

AwaString::AwaString(char* data, unsigned int size):
_data(data),
_size(size)
{
}

AwaString::~AwaString()
{
	delete [] _data;
}

AwaString&
AwaString::operator = (const AwaString& rhs)
{
	delete [] _data;
	_size = rhs._size;
	_data = new char[_size];
	strcpy(_data, rhs._data);
	return *this;
}

AwaString&
AwaString::operator = (const char* c)
{
	delete [] _data;
	_size = strlen(c) + 1;
	_data = new char[_size];
	strcpy(_data, c);
	return *this;
}

AwaString&
AwaString::operator += (const AwaString& rhs)
{
	unsigned int sizeAux = _size - 1 + rhs._size;
	char* dataAux = new char[sizeAux];

	strcpy(dataAux, _data);
	strcpy(dataAux + _size - 1, rhs._data);

	delete [] _data;
	_size = sizeAux;
	_data = dataAux;

	return *this;
}

AwaString&
AwaString::operator += (const char* c)
{
	unsigned int sizeAux = _size + strlen(c);
	char* dataAux = new char[sizeAux];

	strcpy(dataAux, _data);
	strcpy(dataAux + _size - 1, c);

	delete [] _data;
	_size = sizeAux;
	_data = dataAux;

	return *this;
}

AwaString
AwaString::operator + (const AwaString& rhs) const
{
	unsigned int sizeAux = _size - 1 + rhs._size;
	char* dataAux = new char[sizeAux];

	strcpy(dataAux, _data);
	strcpy(dataAux + _size - 1, rhs._data);

	return AwaString(dataAux, sizeAux);
}

AwaString
AwaString::operator + (const char* c) const
{
	unsigned int sizeAux = _size + strlen(c);
	char* dataAux = new char[sizeAux];

	strcpy(dataAux, _data);
	strcpy(dataAux + _size - 1, c);

	return AwaString(dataAux, sizeAux);
}

AwaString
AwaString::operator - (const char* c) const
{
	const char* p1 = _data;
	const char* p2 = c;

	while ((*p1 != 0) && (*p2 != 0))
	{
		if (*p1 == *p2)
		{
			++p1;
			++p2;
		}
		else
			break;
	}

	if (*p1 != 0)
		return AwaString(p1);
	else
		return AwaString();
}

AwaString&
AwaString::operator -= (const char* c)
{
	operator = (AwaString(*this - c));
	return *this;
}

bool
AwaString::operator == (const char* c) const
{
	for (unsigned int i=0; i<_size; i++)
	{
		if (_data[i] != c[i])
			return false;
	}

	return true;
}

bool
AwaString::operator != (const char* c) const
{
	return ! operator == (c);
}

AwaString
AwaString::suppressFromEnd(const AwaString& s) const
{
	char* p1 = _data + _size - 1;
	const char* p2 = s._data + s._size - 1;

	while ((p1 >= _data) && (p2 >= s._data))
	{
		if (*p1 == *p2)
		{
			--p1;
			--p2;
		}
		else
			break;
	}

	AwaString result;

	if (p1 >= _data)
	{
		++p1;
		char c = *p1;
		*p1 = 0;
		result = AwaString(_data);
		*p1 = c;
	}

	return result;
}

AwaString&
AwaString::upperCase()
{
	char* end = _data + _size - 1;
	for (char* p=_data; p<end; p++)
	{
		if ((*p >= 'a') && (*p <= 'z'))
			*p -= 32;
	}

	return *this;
}

AwaString::operator char*() const
{
	return _data;
}

char&
AwaString::operator [] (const unsigned int offset)
{
	if (offset >= _size)
		throw AwaStringException();

	return _data[offset];
}

unsigned int
AwaString::size() const
{
	return _size - 1;
}

bool
AwaString::startWith(const char* c) const
{
	const char* pt1 = _data;
	const char* pt2 = c;

	while ((*pt1 != 0) && (*pt2 != 0) && (*pt1 == *pt2))
	{
		++pt1;
		++pt2;
	}

	if (*pt2 == 0)
		return true;
	else
		return false;
}

bool
AwaString::endWith(const char* c) const
{
	const char* pt1 = _data + _size - 1;
	const char* pt2 = c + strlen(c);

	while ((pt1 >= _data) && (pt2 >= c) && (*pt1 == *pt2))
	{
		--pt1;
		--pt2;
	}

	if (pt2 == c-1)
		return true;
	else
		return false;
}
