char* replace_a(char*s1,char*s2,char*s3=NULL)
{
char *p,*from,*to,*begin=s1;
int c1,c2,c3,c;
c2=strlen(s2);
c3=(s3!=NULL)?strlen(s3):0;
if(c2==0)return s1;
while(true)
{
c1=strlen(begin);
p=strstr(begin,s2);
if(p==NULL)
return s1;
if(c2>c3)
{
from=p+c2;
to=p+c3;
c=c1-c2+begin-p+1;
while(c--)
*to++=*from++;
}
else if(c2<c3)
{
from=begin+c1;
to=from-c2+c3;
c=from-p-c2+1;
while(c--)
*to--=*from--;
}
if(c3)
{
from=s3,to=p,c=c3;
while(c--)
*to++=*from++;
}
begin=p+c3;
}
}
wchar_t* replace_w(wchar_t* s1, wchar_t* s2, wchar_t* s3 = NULL)
{
wchar_t* p, * from, * to, * begin = s1;
int c1, c2, c3, c;
c2 = wcslen(s2);
c3 = (s3 != NULL) ? wcslen(s3) : 0;
if (c2 == 0)return s1;
while (true)
{
c1 = wcslen(begin);
p = wcsstr(begin, s2);
if (p == NULL)
return s1;
if (c2 > c3)
{
from = p + c2;
to = p + c3;
c = c1 - c2 + begin - p + 1;
while (c--)
* to++ = *from++;
}
else if (c2 < c3)
{
from = begin + c1;
to = from - c2 + c3;
c = from - p - c2 + 1;
while (c--)
* to-- = *from--;
}
if (c3)
{
from = s3, to = p, c = c3;
while (c--)
* to++ = *from++;
}
begin = p + c3;
}
}