module utils;

/*
 * These codes borrowed from h3r3tic
 */

char[] intToStringCT(int i)
{
	char[] res = "";
	do
	{
		res ~= "0123456789"[i%10];
		i /= 10;
	} while (i > 0);
	
	for (int j = 0; j < res.length/2; ++j) {
		char c = res[j];
		res[j] = res[res.length-j-1];
		res[res.length-j-1] = c;
	}
	return res;
}

char[] rangeCodegen(int i)
{
	char[] res = `alias RangeTuple!(`;
	if (i > 0) {
		res ~= "0";
		for (int j = 1; j < i; ++j) {
			res ~= "," ~ intToStringCT(j);
		}
	}
	return res ~ ") Range;";
}

template RangeTuple(T ...)
{
	alias T RangeTuple;
}

template Range(int i)
{
	mixin(rangeCodegen(i));
}

char[][] GetNamesInAlias(alias target)()
{
	char[][] names = [];
	const int len = target.tupleof.length;
	int prefix = target.stringof.length + 3;		// "(Type)."
	//This Range! crud is to iterate through the tupple, as the index has to be a constant
	foreach (i; Range!(len)) 
	{
		names ~= target.tupleof[i].stringof[prefix..$];
	}
	return names;
}

char[][] GetTypesInAlias(alias target)()
{
	char[][] names = [];
	const int len = target.tupleof.length;
	foreach (i; Range!(len)) 
	{
		names ~= typeof(target.tupleof[i]).stringof;
	}
	return names;
}
