www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - building char[][] in templates

reply BCS <f d.c> writes:
I'm about ready to pull my hair out!!!
I'm trying to write a template that will split a string based on a character:

 char[][] foo = SplitString!("hello|world|this|is|a|test",'|');
 // foo == ["hello","world","this","is","a","test"];

I keep getting an error about this being non-const:
 null[0..0] ~ "hello" ~ "world" ~ "this" ~ "is" ~ "a" ~ "test"

I'm doing something like this:
template SplitString(char[] string, char c)
{
	static if(string == "")
		char[][] SplitString = (char[][]).init;
	else
		char[][] SplitString = SplitString!(BeforLast!(string,c),c) ~
AfterLast!(string,c);
}

Any ideas?? Better yet would be a template that evaluates to a tuple
template SplitString(char[] string, char c, V...)
{
	static if(string == "")
		auto SplitString = V;
	else
		auto SplitString = SplitString!(BeforLast!(string,c),c, V,
AfterLast!(string,c));
}

that would allow this:
void foo(char[] string)
{
	// static foreach
	foreach(s;SplitString!("hello|world|this|is|a|test",'|'))
	{
		//use s as const
	}
}

Dec 16 2006
parent BCS <f d.c> writes:
OOPS!
Dec 16 2006