building char[][] in templates

BCS f at d.c
Sat Dec 16 16:12:07 PST 2006


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
>	}
>}



More information about the Digitalmars-d-learn mailing list