/* string.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2009 */

/*

Copyright (c) 2005-2009 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

#include	"compiler.h"


namespace sswf
{
namespace as
{




String::String(void)
{
	f_len = 0;
	f_max = 0;
	f_str = 0;
}
String::String(const String& string)
{
	f_len = 0;
	f_max = 0;
	f_str = 0;
	Set(string.f_str, string.f_len);
}
String::String(const char *str)
{
	f_len = 0;
	f_max = 0;
	f_str = 0;
	operator += (str);
}
String::~String()
{
	delete [] f_str;
}

String& String::operator = (const String& string)
{
	Set(string.f_str, string.f_len);
	return *this;
}

// Assume ISO-8859-1 as input
String& String::operator = (const char *str)
{
	Empty();
	return operator += (str);
}

bool String::operator == (const String& string) const
{
	if(f_len != string.f_len) {
		return false;
	}

	return memcmp(f_str, string.f_str, sizeof(long) * f_len) == 0;
}

bool String::operator != (const String& string) const
{
	return !operator == (string);
}

bool String::operator == (const char *str) const
{
	long	idx = 0;

	while(*str != '\0' && idx < f_len) {
		if(*str != f_str[idx]) {
			return false;
		}
		str++;
		idx++;
	}

	return *str == '\0' && idx == f_len;
}

bool String::operator != (const char *str) const
{
	return !operator == (str);
}

int String::Compare(const String& string) const
{
	long idx = 0;
	long max = f_len < string.f_len ? f_len : string.f_len;
	while(idx < max) {
		long r = f_str[idx] - string.f_str[idx];
		if(r != 0) {
			return r < 0 ? -1 : 1;
		}
		idx++;
	}
	if(max == f_len) {
		return max == string.f_len ? 0 : -1;
	}
	return 1;
}

int String::Compare(const char *string) const
{
	if(string == 0) {
		if(f_len == 0) {
			return 0;
		}
		return 1;
	}
	long idx = 0;
	long len = strlen(string);
	long max = f_len < len ? f_len : len;
	while(idx < max) {
		long r = f_str[idx] - ((const unsigned char *) string)[idx];
		if(r != 0) {
			return r < 0 ? -1 : 1;
		}
		idx++;
	}
	if(max == f_len) {
		return max == len ? 0 : -1;
	}
	return 1;
}

bool String::IsEmpty(void) const
{
	return f_len == 0;
}
void String::Empty(void)
{
	f_len = 0;
}

long String::GetLength(void) const
{
	return f_len;
}
const long *String::Get(void) const
{
	return f_str;
}
void String::Set(const long *str, long size)
{
	if(size > 0 && size < f_max) {
		f_len = size;
		memcpy(f_str, str, f_len * sizeof(long));
	}
	else {
		delete [] f_str;
		if(size <= 0) {
			f_len = 0;
			f_max = 0;
			f_str = 0;
		}
		else {
			f_len = size;
			f_max = (size + 255) & -256;
			f_str = new long[f_max];
			memcpy(f_str, str, f_len * sizeof(long));
		}
	}
}

void String::AppendChar(long c)
{
	if(f_len >= f_max) {
		f_max += 256;
		long *s = new long[f_max];
		memcpy(s, f_str, f_len * sizeof(long));
		delete [] f_str;
		f_str = s;
	}

	f_str[f_len] = c;
	++f_len;
}

String& String::AppendStr(const long *str, long len)
{
	if(len == 0) {
		return *this;
	}

	if(f_len + len > f_max) {
		f_max = (f_len + len + 255) & -256;
		long *s = new long[f_max];
		memcpy(s, f_str, f_len * sizeof(long));
		memcpy(s + f_len, str, len * sizeof(long));
		delete [] f_str;
		f_str = s;
	}
	else {
		memcpy(f_str + f_len, str, len * sizeof(long));
	}
	f_len += len;

	return *this;
}

String& String::operator += (const String& string)
{
	if(string.f_len == 0) {
		return *this;
	}

	if(f_len + string.f_len > f_max) {
		f_max = (f_len + string.f_len + 255) & -256;
		long *s = new long[f_max];
		if(f_len > 0) {
			memcpy(s, f_str, f_len * sizeof(long));
		}
		memcpy(s + f_len, string.f_str, string.f_len * sizeof(long));
		delete [] f_str;
		f_str = s;
	}
	else {
		memcpy(f_str + f_len, string.f_str, string.f_len * sizeof(long));
	}
	f_len += string.f_len;

	return *this;
}

// Assume ISO-8859-1 as input
String& String::operator += (const char *str)
{
	if(str == 0) {
		return *this;
	}
	long l = strlen(str);
	if(l == 0) {
		return *this;
	}

	long idx;
	if(f_len + l > f_max) {
		f_max = (f_len + l + 255) & -256;
		long *s = new long[f_max];
		if(f_len > 0) {
			memcpy(s, f_str, f_len * sizeof(long));
		}
		for(idx = 0; idx < l; ++idx) {
			s[f_len + idx] = str[idx];
		}
		delete [] f_str;
		f_str = s;
	}
	else {
		for(idx = 0; idx < l; ++idx) {
			f_str[f_len + idx] = str[idx];
		}
	}
	f_len += l;

	return *this;
}




int String::GetUTF8Length(void) const
{
	size_t		r, l, wc_len;
	long		*wc, c;

	// make sure we always have a null at the end...
	r = 0;

	wc_len = f_len;
	wc = f_str;
	while(wc_len > 0) {
		/* get one wide character */
		c = *wc++;
		wc_len--;
		/* encode in temp buffer */
		if(c < 0x80) {
			/* this will also encode '\0'... */
			l = 1;
		}
		else if(c < 0x800) {
			l = 2;
		}
		else if(c < 0x10000) {
			l = 3;
		}
		else if(c < 0x200000) {
			l = 4;
		}
		else if(c < 0x4000000) {
			l = 5;
		}
		else if(c > 0) {
			l = 6;
		}
		else {
			/* an invalid wide character (negative!) */
			return -1;
		}
		r += l;
	}

	return r;
}

int String::ToUTF8(char *mb, size_t& mb_len) const
{
	char		buf[6];
	size_t		l, wc_len;
	long		*wc, c;

	// make sure we always have a null at the end...
	mb_len--;
	mb[mb_len] = '\0';

	wc_len = f_len;
	wc = f_str;
	while(wc_len > 0) {
		/* get one wide character */
		c = *wc++;
		wc_len--;
		/* encode in temp buffer */
		if(c < 0x80) {
			/* this will also encode '\0'... */
			buf[0] = (char) c;
			l = 1;
		}
		else if(c < 0x800) {
			buf[0] = (c >> 6) | 0xC0;
			buf[1] = (c & 0x3F) | 0x80;
			l = 2;
		}
		else if(c < 0x10000) {
			buf[0] = (c >> 12) | 0xE0;
			buf[1] = ((c >> 6) & 0x3F) | 0x80;
			buf[2] = (c & 0x3F) | 0x80;
			l = 3;
		}
		else if(c < 0x200000) {
			buf[0] = (c >> 18) | 0xF0;
			buf[1] = ((c >> 12) & 0x3F) | 0x80;
			buf[2] = ((c >> 6) & 0x3F) | 0x80;
			buf[3] = (c & 0x3F) | 0x80;
			l = 4;
		}
		else if(c < 0x4000000) {
			buf[0] = (c >> 24) | 0xF8;
			buf[1] = ((c >> 18) & 0x3F) | 0x80;
			buf[2] = ((c >> 12) & 0x3F) | 0x80;
			buf[3] = ((c >> 6) & 0x3F) | 0x80;
			buf[4] = (c & 0x3F) | 0x80;
			l = 5;
		}
		else if(c > 0) {	// <=> (signed long) c < 0x80000000
			buf[0] = (c >> 30) | 0xFC;
			buf[1] = ((c >> 24) & 0x3F) | 0x80;
			buf[2] = ((c >> 18) & 0x3F) | 0x80;
			buf[3] = ((c >> 12) & 0x3F) | 0x80;
			buf[4] = ((c >> 6) & 0x3F) | 0x80;
			buf[5] = (c & 0x3F) | 0x80;
			l = 6;
		}
		else {
			/* an invalid wide character (negative!) */
			return -1;
		}
		if(l < mb_len) {
			mb_len -= l;
			memcpy(mb, buf, l);
			mb += l;
		}
		else {
			/* overflow error */
			return -1;
		}
	}

	*mb = '\0';

	mb_len++;	// put the '\0' back in what's left

	return 0;
}


// WARNING: this function appends the string mb to
//	    the current string in this object
int String::FromUTF8(const char *mb, size_t mb_len)
{
	unsigned char	c;
	long		w;
	size_t		l;

	while(mb_len > 0) {
		mb_len--;
		c = (unsigned char) *mb++;
		if(c < 0x80) {
			w = c;
		}
		else {
			if(c >= 0xC0 && c <= 0xDF) {
				l = 1;
				w = c & 0x1F;
			}
			else if(c >= 0xE0 && c <= 0xEF) {
				l = 2;
				w = c & 0x0F;
			}
			else if(c >= 0xF0 && c <= 0xF7) {
				l = 3;
				w = c & 0x07;
			}
			else if(c >= 0xF8 && c <= 0xFB) {
				l = 4;
				w = c & 0x03;
			}
			else if(c >= 0xFC && c <= 0xFD) {
				l = 5;
				w = c & 0x01;
			}
			else {
				return -1;
			}
			if(mb_len < l) {
				return -1;
			}
			mb_len -= l;
			while(l > 0) {
				c = (unsigned char) *mb++;
				if(c < 0x80 || c > 0xBF) {
					return -1;
				}
				l--;
				w = (w << 6) | (c & 0x3F);
			}
		}
		AppendChar(w);
	}

	return 0;
}


char *String::GetUTF8(void) const
{
	size_t len = GetUTF8Length() + 2;
	char *str = new char[len];
	ToUTF8(str, len);
	return str;
}



};	// namespace as
};	// namespace sswf
