Static Foreach
nazo
lovesyao at gmail.com
Tue Sep 19 20:31:21 PDT 2006
I want to write like this code for optimize. The compiler cannot
optimize code like this code. CGI needs more speeds!
char[] replace(char[][] before,char[][] after)(char[] str){
static assert(before.length==after.length);
compile char[][] t=before;
compile char[][] u=after;
/*sort*/
static for(compile int i;i<t.length-1;i++)
static for(compile int j=j+1;j<t.length;j++)
static if(t[i]>t[j]){
compile char[] tmp=t[i];
t[i]=t[j];
t[j]=tmp;
compile char[] tmp=u[i];
t[i]=u[j];
u[j]=tmp;
}
/**/
int len=str.length;
compile before_len=0;
for(int i;i<str.length-b[0].length;i++){
static foreach(compile int j,compile char[] b;t){
static if(before_len!=b.length){
if(b.length>len)
return str;
}
if(str[i..i+b.length]==b){
str=str[0..i]~u[j]~str[i+b.length..$];
i+=u[j].length-1;
len-=u[j].length;
break;/*for*/
}
before_len=b.length;
}
len--;
}
return str;
}
private import std.stdio;
void main(char[][] args){
if(args.length<2)return -1;
writefln(replace!(["aa","bb"],["cc","dd"])(args[1]));
}
More information about the Digitalmars-d
mailing list