building char[][] in templates

BCS BCS at pathlink.com
Mon Dec 18 11:06:32 PST 2006


Bill Baxter wrote:
> Kevin Bealer wrote:
> 
>> I've never done this myself, but there is an example of a 
>> metaprogramming regex
>> parser at the bottom of this page:
>>
>> http://www.digitalmars.com/d/templates-revisited.html
>>
>> Kevin
> 
> 
> If actually splitting the string doesn't work then maybe you can manage 
> to have it generate a tuple of ints that represent the split points.
> 
> 
> --bb

This is what I ended up using (and it actually does a better job for 
what I was trying to do) it returns a tuple.

/************
	Return a slice up-to but not including the first instance of t.
*/
template UseTill(char[] string, char t)
{
	static if(string == "" || string[0] == t)
		const char[] UseTill = "";
	else
		const char[] UseTill = string[0..1] ~ UseTill!(string[1..$],t);
}

/***********
	Return a slice starting after the first instance of t and containing 
the rest of the string.
*/
template DiscardTill(char[] string, char t)
{
	static if(string == "")
		const char[] DiscardTill = "";
	else static if(string[0] == t)
		const char[] DiscardTill = string[1..$];
	else
		const char[] DiscardTill = DiscardTill!(string[1..$],t);
}

/***********
	mixin temp with {V + 'string' broken up by 'c'}
*/
template BreakBy(char[] string, char d, V...)
{
	static if(string == "")
		alias V BreakBy;
	else static
		alias BreakBy!(DiscardTill!(string,d), d, V, UseTill!(string,d)) BreakBy;
}


More information about the Digitalmars-d-learn mailing list