#include <assert.h>
#include <string.h>
#include "stdafx.h"
#include "nns Classes.h"

String::String()
{
	lenght = 0;
	text = new char(0);
	TypeOfString = any_len;
}
String::String(char* p, String::type tos)
{
	assert(p != 0);
	lenght = strlen(p);
	TypeOfString = tos;
	assert(lenght >= 0);
	text = new char[lenght+1];
	strcpy(text, p);
}
String::String(int len, String::type tos)
{
	assert(len >= 0);
	lenght = len;
	TypeOfString = tos;
	text = new char[lenght+1];
	for (int i = 0; i < lenght+1; i++)
		text[i] = 0;
}
String::String(char ch, int repeat, String::type tos)
{
	assert(repeat >= 0);
	lenght = repeat;
	TypeOfString = tos;
	text = new char[lenght+1];
	for (int i = 0; i < lenght; i++)
		text[i] = ch;
	text[lenght] = 0;
}
String::String(String& str, String::type tos)
{
	lenght = str.lenght;
	if (tos = same)
		TypeOfString = str.TypeOfString;
	else TypeOfString = tos;
	text = new char[lenght+1];
	strcpy(text, str.text);
}
int String::GetLenght()
 { return lenght; }
void String::Clean()
{
	if (lenght != 0)
	{
		if (TypeOfString == any_len)
		{
			lenght = 0;
			delete text;
			text = new char(0);
		}
		else
			for (int i = 0; i < lenght + 1; i++)
				text[i] = 0;
	}
}
bool String::IsEmpty()
 { return (lenght==0) ? true : false ;}
char* String::GetText()
 { return text; }
String String::leftSubstring(int count)
{
	assert(count >= 0);
	if (count > lenght) count = lenght;
	String temp(count, any_len);
	memcpy(temp.text, text, count);
	*(temp.text + count) = 0;
	return temp;
}
String String::Substring(int first)
{
	assert(first >= 0 && first < lenght) ;
	int count = lenght - first;
	String temp(count, any_len);
	memcpy(temp.text, text + first, count);
	*(temp.text + count) = 0;
	return temp;
}
String String::Substring(int first, int count)
{
	assert(first >= 0 && first < lenght && count >= 0);
	if (first + count > lenght) count = lenght - first;
	String temp(count, any_len);
	memcpy(temp.text, text + first, count);
	*(temp.text + count) = 0;
	return temp;
}
String String::rightSubstring(int count)
{
	assert(count >=0 );
	if (count > lenght) count = lenght;
	String temp(count, any_len);
	memcpy(temp.text, text + lenght - count, count);
	*(temp.text + count) = 0;
	return temp;
}
char String::GetChar(int index)
{
	assert(index >= 0 && index < lenght) ;
	return text[index] ;
}
char& String::operator[](int index)
{
	assert(index >= 0 && index < lenght) ;
	return text[index] ;
}
void String::SetChar(int index, char ch)
{
	assert(index >= 0 && index < lenght);
	text[index] = ch;
}
String String::operator =(String &rs)
{
	if (this == &rs) return *this;
	if (TypeOfString == String::any_len)
	{
		lenght = rs.lenght;
		delete text;
		text = new char[rs.lenght+1];
		strcpy(text, rs.text);
	}
	else
	{
		int count = 0;
		if (lenght >= rs.lenght) count = rs.lenght;
		else count = lenght;
		memcpy(text, rs.text, count);
		*(text + count) = 0;
	}
	return *this;
}
String String::operator =(char *rs)
{
	if (TypeOfString == String::any_len)
	{
		lenght = strlen(rs);
		delete text;
		text = new char[lenght+1];
		strcpy(text, rs);
	}
	else
	{
		int count = 0;
		if (lenght >= strlen(rs)) count = strlen(rs);
		else count = lenght;
		memcpy(text, rs, count);
		*(text + count) = 0;
	}
	return *this;
}
/*
String operator=(String& ls, String& rs)
{
	if (&ls == &rs) return ls;
	if (ls.TypeOfString == String::any_len)
	{
		ls.lenght = rs.lenght;
		delete ls.text;
		ls.text = new char[ls.lenght+1];
		strcpy(ls.text, rs.text);
	}
	else
	{
		int count = 0;
		if (ls.lenght >= rs.lenght) count = rs.lenght;
		else count = ls.lenght;
		memcpy(ls.text, rs.text, count);
		*(ls.text + count) = 0;
	}
	return ls;
}
String operator=(String& ls, char* rs)
{
	if (ls.TypeOfString == String::any_len)
	{
		ls.lenght = strlen(rs);
		delete ls.text;
		ls.text = new char[ls.lenght+1];
		strcpy(ls.text, rs);
	}
	else
	{
		int count = 0;
		if (ls.lenght >= strlen(rs)) count = strlen(rs);
		else count = ls.lenght;
		memcpy(ls.text, rs, count);
		*(ls.text + count) = 0;
	}
	return ls;
}
String operator=(char* ls, String& rs)
{
	delete ls;
	ls = new char[rs.lenght+1];
	strcpy(ls, rs.text);
	return rs;
}
*/
String operator+(String& ls, String& rs)
{
	String temp(ls.lenght+rs.lenght, String::any_len);
	strcpy(temp.text, ls.text);
	strcat(temp.text, rs.text);
	return temp;
}
String operator+(String& ls, char* rs)
{
	String temp(ls.lenght+strlen(rs), String::any_len);
	strcpy(temp.text, ls.text);
	strcat(temp.text, rs);
	return temp;
}
String operator+(char* ls, String& rs)
{
	String temp(strlen(ls)+rs.lenght, String::any_len);
	strcpy(temp.text, ls);
	strcat(temp.text, rs.text);
	return temp;
}
String operator+=(String& ls, String& rs)
{
	ls = ls + rs;
	return ls;
}
String operator+=(String& ls, char* rs)
{
	ls = ls + rs;
	return ls;
}
String operator+=(char* ls, String rs)
{
	//return ls = ls + rs;
	return rs = ls + rs;
}