Do you like my strlcpy strlcat etc?


R

RoSsIaCrIiLoIA

Do you like my strlcpy strlcat etc?
Thank you
/*---------------------------------------*/
.....
int main(void)
{
/*-------------------------------------{}*/
srand((unsigned)time(0));
test3(200000, 100, 100, strlcat, strlcat_m);
test1(3000000, 100, revstring, revstring_m );
return 0;
} /* main, endswith */
/* 25 + 25 + 10 = 60 0..59 */

char lettere(unsigned j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
return u[j%60];
}


void rand_phrase(char* a, size_t le)
{unsigned i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) goto l0;
la: a=lettere((unsigned)rand()); if(++i<le)goto la;
l0:
a=0;
}


/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
goto l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) goto le;
if(p2==searchme)
{r=1; goto le;}
if(p1!=phrase) goto l0;
le:
return r;
}



/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) goto l0;
}
return lgh;
} /* revstring */


size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{while( --sz && (*dst++ = *src++) );
--z;
if( sz )
{return z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) goto l0;
}
else if(src) goto l1;
return z-sz;
} /* strlcpy */


size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
return z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
return z + (src ? strlen(src): 0);
}
} /* strlcat */

int test1(cicli, len_s0, f0, f1)
unsigned cicli, len_s0;
size_t (*f0)(char*), (*f1)(char*);
{char *s0, *s1, *s2;
size_t i, k0, k1, h0, h1, h3, len_s1=len_s0;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||f0==0||f1==0)
return 0;
if(len_s0>100000)
return 0;
s0=malloc(len_s0);
if(s0==0) return 0;
if((s1=malloc(len_s1))==0)
{free(s0); return 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); return 0;}
i=0;
l0:
h0=rand()%len_s0;
rand_phrase(s0, h0);
strcpy(s1, s0);
if( (k0=f0(s0))!=(k1=f1(s1)) )
{l1:
printf("test failure:"
"s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s1, s0, s2, (unsigned)k0,
(unsigned)k1,(unsigned)strlen(s0),(unsigned)h0,(unsigned)h1,
(unsigned)i);
free(s0); free(s1); free(s2);
return 0;
}
if(strcmp(s0, s1)!=0)goto l1;
if(++i<cicli/50)goto l0;
i=0;
l34:
h1=rand()%len_s1; rand_phrase(s1, h1);
if(h1==0) goto l34;
t0=time(0);
l2:
*s1=++*s1;
if(f0(s1)==1234567) ++i;
if(++i<cicli)goto l2;
t1=time(0);
printf("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
*s1=++*s1;
if(f1(s1)==1234567) ++i;
if(++i<cicli)goto l3;
t1=time(0);
printf("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2);
printf("Test success\n");
return 1;
}

/*************************************/
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break

/* 25 + 25 + 10 = 60 0..59 */
char lettere(U j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
R u[j%60];
}

void rand_phrase(char* a, size_t le)
{U i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) G l0;
la: a=lettere((U)rand()); if(++i<le)G la;
l0:
a=0;
}

/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
G l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) G le;
if(p2==searchme)
{r=1; G le;}
if(p1!=phrase) G l0;
le:
R r;
}


/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) G l0;
}
return lgh;
} /* revstring */


size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{W( --sz && (*dst++ = *src++) );
--z;
if( sz )
{R z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) G l0;
}
else if(src) G l1;
R z-sz;
} /* strlcpy */


size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
R z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
R z + (src ? strlen(src): 0);
}
} /* strlcat */


int test3(cicli, len_s0, len_s1, f0, f1)
U cicli, len_s0, len_s1;
size_t (*f0)(char*, const char*, size_t),
(*f1)(char*, const char*, size_t);
{char *s2, *s0, *s1, *s3;
size_t i, k0, k1, h0, h1, h3;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||len_s1==0||f0==0||f1==0)
R 0;
if(len_s0>100000||len_s1>100000)
R 0;
s0=malloc(len_s0);
if(s0==0) R 0;
if((s1=malloc(len_s1))==0)
{free(s0); R 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); R 0;}
if((s3=malloc(len_s0))==0)
{free(s0);free(s1); free(s2); R 0;}
i=0;
l0:
h0=rand()%len_s0; h1=rand()%len_s1;
h3=h0+1+rand()%(len_s0-h0);
rand_phrase(s0, h0); rand_phrase(s1, h1);

strcpy(s3, s0); strcpy(s2, s0);
if( (k0=f0(s0, s1, h3 )) !=
(k1=f1(s2, s1, h3 )) )
{l1:
P("test failure:"
"s3=%s# s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s3, s1, s0, s2, (U)k0, (U)k1,(U)strlen(s0),(U)h0,(U)h1,
(U)i);
free(s0); free(s1); free(s2); free(s3);
R 0;
}
if(strcmp(s0, s2)!=0)G l1;
if(++i<cicli)G l0;
i=0;
//l34:
// h1=rand()%len_s1; rand_phrase(s1, h1);
// if(h1==0) G l34;
t0=time(0);
l2:
h0=rand()%len_s0; rand_phrase(s0, h3);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f0(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l2;
t1=time(0);
P("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
h0=rand()%len_s0; rand_phrase(s0, h0);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f1(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l3;
t1=time(0);
P("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2); free(s3);
P("Test success\n");
R 1;
}
 
Ad

Advertisements

B

Big K

Skimming through the whole thing in 2 sec., I must say it looks rather
sexy.

Have any specific questions?

Do you like my strlcpy strlcat etc?
Thank you
/*---------------------------------------*/
....
int main(void)
{
/*-------------------------------------{}*/
srand((unsigned)time(0));
test3(200000, 100, 100, strlcat, strlcat_m);
test1(3000000, 100, revstring, revstring_m );
return 0;
} /* main, endswith */
/* 25 + 25 + 10 = 60 0..59 */

char lettere(unsigned j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
return u[j%60];
}


void rand_phrase(char* a, size_t le)
{unsigned i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) goto l0;
la: a=lettere((unsigned)rand()); if(++i<le)goto la;
l0:
a=0;
}


/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
goto l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) goto le;
if(p2==searchme)
{r=1; goto le;}
if(p1!=phrase) goto l0;
le:
return r;
}



/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) goto l0;
}
return lgh;
} /* revstring */


size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{while( --sz && (*dst++ = *src++) );
--z;
if( sz )
{return z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) goto l0;
}
else if(src) goto l1;
return z-sz;
} /* strlcpy */


size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
return z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
return z + (src ? strlen(src): 0);
}
} /* strlcat */

int test1(cicli, len_s0, f0, f1)
unsigned cicli, len_s0;
size_t (*f0)(char*), (*f1)(char*);
{char *s0, *s1, *s2;
size_t i, k0, k1, h0, h1, h3, len_s1=len_s0;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||f0==0||f1==0)
return 0;
if(len_s0>100000)
return 0;
s0=malloc(len_s0);
if(s0==0) return 0;
if((s1=malloc(len_s1))==0)
{free(s0); return 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); return 0;}
i=0;
l0:
h0=rand()%len_s0;
rand_phrase(s0, h0);
strcpy(s1, s0);
if( (k0=f0(s0))!=(k1=f1(s1)) )
{l1:
printf("test failure:"
"s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s1, s0, s2, (unsigned)k0,
(unsigned)k1,(unsigned)strlen(s0),(unsigned)h0,(unsigned)h1,
(unsigned)i);
free(s0); free(s1); free(s2);
return 0;
}
if(strcmp(s0, s1)!=0)goto l1;
if(++i<cicli/50)goto l0;
i=0;
l34:
h1=rand()%len_s1; rand_phrase(s1, h1);
if(h1==0) goto l34;
t0=time(0);
l2:
*s1=++*s1;
if(f0(s1)==1234567) ++i;
if(++i<cicli)goto l2;
t1=time(0);
printf("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
*s1=++*s1;
if(f1(s1)==1234567) ++i;
if(++i<cicli)goto l3;
t1=time(0);
printf("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2);
printf("Test success\n");
return 1;
}

/*************************************/
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break

/* 25 + 25 + 10 = 60 0..59 */
char lettere(U j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
R u[j%60];
}

void rand_phrase(char* a, size_t le)
{U i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) G l0;
la: a=lettere((U)rand()); if(++i<le)G la;
l0:
a=0;
}

/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
G l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) G le;
if(p2==searchme)
{r=1; G le;}
if(p1!=phrase) G l0;
le:
R r;
}


/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) G l0;
}
return lgh;
} /* revstring */


size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{W( --sz && (*dst++ = *src++) );
--z;
if( sz )
{R z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) G l0;
}
else if(src) G l1;
R z-sz;
} /* strlcpy */


size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
R z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
R z + (src ? strlen(src): 0);
}
} /* strlcat */


int test3(cicli, len_s0, len_s1, f0, f1)
U cicli, len_s0, len_s1;
size_t (*f0)(char*, const char*, size_t),
(*f1)(char*, const char*, size_t);
{char *s2, *s0, *s1, *s3;
size_t i, k0, k1, h0, h1, h3;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||len_s1==0||f0==0||f1==0)
R 0;
if(len_s0>100000||len_s1>100000)
R 0;
s0=malloc(len_s0);
if(s0==0) R 0;
if((s1=malloc(len_s1))==0)
{free(s0); R 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); R 0;}
if((s3=malloc(len_s0))==0)
{free(s0);free(s1); free(s2); R 0;}
i=0;
l0:
h0=rand()%len_s0; h1=rand()%len_s1;
h3=h0+1+rand()%(len_s0-h0);
rand_phrase(s0, h0); rand_phrase(s1, h1);

strcpy(s3, s0); strcpy(s2, s0);
if( (k0=f0(s0, s1, h3 )) !=
(k1=f1(s2, s1, h3 )) )
{l1:
P("test failure:"
"s3=%s# s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s3, s1, s0, s2, (U)k0, (U)k1,(U)strlen(s0),(U)h0,(U)h1,
(U)i);
free(s0); free(s1); free(s2); free(s3);
R 0;
}
if(strcmp(s0, s2)!=0)G l1;
if(++i<cicli)G l0;
i=0;
//l34:
// h1=rand()%len_s1; rand_phrase(s1, h1);
// if(h1==0) G l34;
t0=time(0);
l2:
h0=rand()%len_s0; rand_phrase(s0, h3);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f0(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l2;
t1=time(0);
P("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
h0=rand()%len_s0; rand_phrase(s0, h0);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f1(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l3;
t1=time(0);
P("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2); free(s3);
P("Test success\n");
R 1;
}
 
B

Big K

Big said:
Skimming through the whole thing in 2 sec., I must say it looks rather
sexy.

Have any specific questions?

Mr. Big K, do not top-post please.
Do you like my strlcpy strlcat etc?
Thank you
/*---------------------------------------*/
....
int main(void)
{
/*-------------------------------------{}*/
srand((unsigned)time(0));
test3(200000, 100, 100, strlcat, strlcat_m);
test1(3000000, 100, revstring, revstring_m );
return 0;
} /* main, endswith */
/* 25 + 25 + 10 = 60 0..59 */

char lettere(unsigned j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
return u[j%60];
}


void rand_phrase(char* a, size_t le)
{unsigned i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) goto l0;
la: a=lettere((unsigned)rand()); if(++i<le)goto la;
l0:
a=0;
}


/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
goto l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) goto le;
if(p2==searchme)
{r=1; goto le;}
if(p1!=phrase) goto l0;
le:
return r;
}



/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) goto l0;
}
return lgh;
} /* revstring */


size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{while( --sz && (*dst++ = *src++) );
--z;
if( sz )
{return z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) goto l0;
}
else if(src) goto l1;
return z-sz;
} /* strlcpy */


size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
return z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
return z + (src ? strlen(src): 0);
}
} /* strlcat */

int test1(cicli, len_s0, f0, f1)
unsigned cicli, len_s0;
size_t (*f0)(char*), (*f1)(char*);
{char *s0, *s1, *s2;
size_t i, k0, k1, h0, h1, h3, len_s1=len_s0;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||f0==0||f1==0)
return 0;
if(len_s0>100000)
return 0;
s0=malloc(len_s0);
if(s0==0) return 0;
if((s1=malloc(len_s1))==0)
{free(s0); return 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); return 0;}
i=0;
l0:
h0=rand()%len_s0;
rand_phrase(s0, h0);
strcpy(s1, s0);
if( (k0=f0(s0))!=(k1=f1(s1)) )
{l1:
printf("test failure:"
"s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s1, s0, s2, (unsigned)k0,
(unsigned)k1,(unsigned)strlen(s0),(unsigned)h0,(unsigned)h1,
(unsigned)i);
free(s0); free(s1); free(s2);
return 0;
}
if(strcmp(s0, s1)!=0)goto l1;
if(++i<cicli/50)goto l0;
i=0;
l34:
h1=rand()%len_s1; rand_phrase(s1, h1);
if(h1==0) goto l34;
t0=time(0);
l2:
*s1=++*s1;
if(f0(s1)==1234567) ++i;
if(++i<cicli)goto l2;
t1=time(0);
printf("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
*s1=++*s1;
if(f1(s1)==1234567) ++i;
if(++i<cicli)goto l3;
t1=time(0);
printf("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2);
printf("Test success\n");
return 1;
}

/*************************************/
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break

/* 25 + 25 + 10 = 60 0..59 */
char lettere(U j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
R u[j%60];
}

void rand_phrase(char* a, size_t le)
{U i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) G l0;
la: a=lettere((U)rand()); if(++i<le)G la;
l0:
a=0;
}

/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
G l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) G le;
if(p2==searchme)
{r=1; G le;}
if(p1!=phrase) G l0;
le:
R r;
}


/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) G l0;
}
return lgh;
} /* revstring */


size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
/*----------------------------*/
if( sz )
{if( src )
{W( --sz && (*dst++ = *src++) );
--z;
if( sz )
{R z-sz;}
else {l0: *dst=0;
l1: z+=strlen(src);
}
}
else if(dst) G l0;
}
else if(src) G l1;
R z-sz;
} /* strlcpy */


size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
R z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
R z + (src ? strlen(src): 0);
}
} /* strlcat */


int test3(cicli, len_s0, len_s1, f0, f1)
U cicli, len_s0, len_s1;
size_t (*f0)(char*, const char*, size_t),
(*f1)(char*, const char*, size_t);
{char *s2, *s0, *s1, *s3;
size_t i, k0, k1, h0, h1, h3;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||len_s1==0||f0==0||f1==0)
R 0;
if(len_s0>100000||len_s1>100000)
R 0;
s0=malloc(len_s0);
if(s0==0) R 0;
if((s1=malloc(len_s1))==0)
{free(s0); R 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); R 0;}
if((s3=malloc(len_s0))==0)
{free(s0);free(s1); free(s2); R 0;}
i=0;
l0:
h0=rand()%len_s0; h1=rand()%len_s1;
h3=h0+1+rand()%(len_s0-h0);
rand_phrase(s0, h0); rand_phrase(s1, h1);

strcpy(s3, s0); strcpy(s2, s0);
if( (k0=f0(s0, s1, h3 )) !=
(k1=f1(s2, s1, h3 )) )
{l1:
P("test failure:"
"s3=%s# s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s3, s1, s0, s2, (U)k0, (U)k1,(U)strlen(s0),(U)h0,(U)h1,
(U)i);
free(s0); free(s1); free(s2); free(s3);
R 0;
}
if(strcmp(s0, s2)!=0)G l1;
if(++i<cicli)G l0;
i=0;
//l34:
// h1=rand()%len_s1; rand_phrase(s1, h1);
// if(h1==0) G l34;
t0=time(0);
l2:
h0=rand()%len_s0; rand_phrase(s0, h3);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f0(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l2;
t1=time(0);
P("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
h0=rand()%len_s0; rand_phrase(s0, h0);
h1=rand()%len_s1; rand_phrase(s1, h1);
h3=(h0+h1+1)%(len_s0);
// *s1=++*s1;
if(f1(s0, s1, h3)==1234567) ++i;
if(++i<cicli)G l3;
t1=time(0);
P("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2); free(s3);
P("Test success\n");
R 1;
}


My bad.
 
K

Keith Thompson

RoSsIaCrIiLoIA said:
Do you like my strlcpy strlcat etc? [snip]
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break
[snip]

No.
 
K

Keith Thompson

Big K said:
Big said:
Skimming through the whole thing in 2 sec., I must say it looks rather
sexy.

Have any specific questions?

Mr. Big K, do not top-post please.
RoSsIaCrIiLoIA wrote:
[huge snip]
My bad.

Unnecessarily quoting hundreds of lines that aren't relevant to your
followup is even worse than top-posting.
 
R

RoSsIaCrIiLoIA

Skimming through the whole thing in 2 sec., I must say it looks rather
sexy.

Have any specific questions?

Why CBFalconer's ones are more fast?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>

/* Yes, I know strlcat and strlcpy are in the implementors
namespace. Change them if you must.
See <http://cbfalconer.home.att.net/download/strlcpy.zip>
for documentation and rationale.

The objective is to detect whether a given string terminates
another string. This is a fairly tortuous way of doing
"endswith", but is fairly clear.

By C.B.Falconer. Released to public domain
*/

/* ---------------------- */
static int endswith_m(const char* , const char* );
static size_t revstring_m(char* string);
void rand_phrase(char* a, size_t le);
size_t strlcpy_m(char* dst, const char* src, size_t sz);

size_t strlcat_m(char* dst, const char* src, size_t sz);

int test3(unsigned cicli,unsigned len_s0,unsigned len_s1,
size_t (*f0)(char*, const char*, size_t),
size_t (*f1)(char*, const char*, size_t) );

int test1(unsigned cicli, unsigned len_s0,
size_t (*f0)(char*), size_t (*f1)(char*) );

/* reverse string in place. Return length */
static size_t revstring(char *string)
{
char *last, temp;
size_t lgh;

if ((lgh = strlen(string)) > 1) {
last = string + lgh; /* points to '\0' */
while (last-- > string) {
temp = *string; *string++ = *last; *last = temp;
}
}
return lgh;
} /* revstring */

/* ---------------------- */

static size_t strlcpy(char *dst, const char *src, size_t sz)
{
const char *start = src;

if (src && sz--) {
while ((*dst++ = *src))
if (sz--) src++;
else {
*(--dst) = '\0';
break;
}
}
if (src) {
while (*src++) continue;
return src - start - 1;
}
else if (sz) *dst = '\0';
return 0;
} /* strlcpy */

/* ---------------------- */

static size_t strlcat(char *dst, const char *src, size_t sz)
{
char *start = dst;

while (*dst++) /* assumes sz >= strlen(dst) */
if (sz) sz--; /* i.e. well formed string */
dst--;
return dst - start + strlcpy(dst, src, sz);
} /* strlcat */

/* ---------------------- */

/* does searchme end with the phrase phrase? */
/* illustrates the power of reversing things */
/* (if not the efficacy) */
static int endswith(char *phrase, char *searchme)
{
int result, lgh, i;

lgh = revstring(phrase); revstring(searchme);
result = 1;
for (i = 0; i < lgh; i++) /* strncmp if we had it */
if (phrase != searchme) {
result = 0; break;
}
revstring_m(phrase); revstring_m(searchme);
return result;
} /* endswith */

/*End of By C.B.Falconer's ones */
/* Start mine */
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break

int main(void)
{srand((U)time(0));
test3(40000000, 100, 100, strlcpy, strlcpy_m);
test1(3000000, 100, revstring, revstring_m );
return 0;
} /* main, endswith */


/* 25 + 25 + 10 = 60 0..59 */
char lettere(U j)
{char *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
R u[j%60];
}

void rand_phrase(char* a, size_t le)
{U i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) G l0;
la: a=lettere((U)rand()); if(++i<le)G la;
l0:
a=0;
}

/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
int endswith_m(const char* phrase, const char* searchme)
{const char *p1, *p2;
int r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +strlen(phrase ); /**p1=*p2=0 */
p2= searchme+strlen(searchme); /*ok caso *p1=0 o *p2=0 */
G l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) G le;
if(p2==searchme)G l2;
if(p1!=phrase) G l0;
G le;
l2: r=1;
le:
R r;
}


/* reverse string in place. Return length */
size_t revstring_m(char *string)
{char *last, temp;
size_t lgh;
/*-------------------------------------*/
if( (lgh=strlen(string))>1 )
{
last= string+lgh-1;
l0:
temp=*string, *string=*last, *last=temp;
--last; ++string;
if(last>string) G l0;
}
return lgh;
} /* revstring */


size_t
strlcpy_m(char* dst, const char* src, size_t sz)
{size_t z=sz;
char a;
/*----------------------------*/
if( sz )
{if( src )
{//W( --sz && (*dst++ = *src++) );
l1: --sz;
if(sz==0) G l2;
a=*src; *dst=a;
++src; ++dst;
if(a) G l1;
l2:
--z;
if( sz )
{R z-sz;}
else { *dst=0;
l0: z+=strlen(src);
}
}
else if(dst) *dst=0;
}
else if(src) G l0;
R z-sz;
} /* strlcpy */


size_t
strlcat_m(char* dst, const char* src, size_t sz)
{size_t z;
/*--------------------*/
if( (z=strlen(dst)) + 1 <= sz )
R z + strlcpy_m(dst+z, src, sz-z);
else {if(sz) dst[sz-1]=0; /* se dest=NULL could write it */
R z + (src ? strlen(src): 0);
}
} /* strlcat */

int test3(cicli, len_s0, len_s1, f0, f1)
U cicli, len_s0, len_s1;
size_t (*f0)(char*, const char*, size_t),
(*f1)(char*, const char*, size_t);
{char *s2, *s0, *s1, *s3;
size_t i, k0, k1, h0, h1, h3;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||len_s1==0||f0==0||f1==0)
R 0;
if(len_s0>100000||len_s1>100000)
R 0;
s0=malloc(len_s0);
if(s0==0) R 0;
if((s1=malloc(len_s1))==0)
{free(s0); R 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); R 0;}
if((s3=malloc(len_s0))==0)
{free(s0);free(s1); free(s2); R 0;}
i=0;
l0:
h0=rand()%len_s0; h1=rand()%len_s1;
h3=h0+1+rand()%(len_s0-h0);
rand_phrase(s0, h0); rand_phrase(s1, h1);

strcpy(s3, s0); strcpy(s2, s0);
if( (k0=f0(s0, s1, h3 )) !=
(k1=f1(s2, s1, h3 )) )
{l1:
P("test failure:"
"s3=%s# s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s3, s1, s0, s2, (U)k0, (U)k1,(U)strlen(s0),(U)h0,(U)h1,
(U)i);
free(s0); free(s1); free(s2); free(s3);
R 0;
}
if(strcmp(s0, s2)!=0)G l1;
if(++i<cicli/50)G l0;
i=0;
l34:
h1=rand()%len_s1; rand_phrase(s1, h1);
if(h1==0) G l34;
t0=time(0);
l2:
// h0=rand()%len_s0; rand_phrase(s0, h3);
// h1=rand()%len_s1; rand_phrase(s1, h1);
// h3=(h0+h1+1)%(len_s0);
*s1=++*s1;
if(f0(s0, s1, h1+1)==1234567) ++i;
if(++i<cicli)G l2;
t1=time(0);
P("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
// h0=rand()%len_s0; rand_phrase(s0, h0);
// h1=rand()%len_s1; rand_phrase(s1, h1);
// h3=(h0+h1+1)%(len_s0);
*s1=++*s1;
if(f1(s0, s1, h1+1)==1234567) ++i;
if(++i<cicli)G l3;
t1=time(0);
P("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2); free(s3);
P("Test success\n");
R 1;
}


int test1(cicli, len_s0, f0, f1)
U cicli, len_s0;
size_t (*f0)(char*), (*f1)(char*);
{char *s0, *s1, *s2;
size_t i, k0, k1, h0, h1, h3, len_s1=len_s0;
time_t t0, t1;
/*-------------------------------------{}*/
if(cicli==0||len_s0==0||f0==0||f1==0)
R 0;
if(len_s0>100000)
R 0;
s0=malloc(len_s0);
if(s0==0) R 0;
if((s1=malloc(len_s1))==0)
{free(s0); R 0;}
if((s2=malloc(len_s0))==0)
{free(s0);free(s1); R 0;}
i=0;
l0:
h0=rand()%len_s0;
rand_phrase(s0, h0);
strcpy(s1, s0);
if( (k0=f0(s0))!=(k1=f1(s1)) )
{l1:
P("test failure:"
"s1=%s# s0=%s# s2=%s# k0=%u# k1=%u# len(s0)=%u h0=%u h1=%u
giri=%u\n",
s1, s0, s2, (U)k0, (U)k1,(U)strlen(s0),(U)h0,(U)h1, (U)i);
free(s0); free(s1); free(s2);
R 0;
}
if(strcmp(s0, s1)!=0)G l1;
if(++i<cicli/50)G l0;
i=0;
l34:
h1=rand()%len_s1; rand_phrase(s1, h1);
if(h1==0) G l34;
t0=time(0);
l2:
*s1=++*s1;
if(f0(s1)==1234567) ++i;
if(++i<cicli)G l2;
t1=time(0);
P("d_0=%f\n", difftime(t1,t0));
i=0;
t0=time(0);
l3:
*s1=++*s1;
if(f1(s1)==1234567) ++i;
if(++i<cicli)G l3;
t1=time(0);
P("d_1=%f\n", difftime(t1,t0));
free(s0); free(s1); free(s2);
P("Test success\n");
R 1;
}
 
Ad

Advertisements

E

Emmanuel Delahaye

Keith Thompson wrote on 04/02/05 :
RoSsIaCrIiLoIA said:
Do you like my strlcpy strlcat etc? [snip]
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break
[snip]

No.

#define ooo goto
#define ooO unsigned
#define oOo return
#define oOO while
#define Ooo for
#define OoO printf
#define OOo break

is more fun ...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
J

Joona I Palaste

Emmanuel Delahaye said:
#define ooo goto
#define ooO unsigned
#define oOo return
#define oOO while
#define Ooo for
#define OoO printf
#define OOo break
is more fun ...

#define lll goto
#define llI unsigned
#define lIl return
#define lII while
#define Ill for
#define IlI printf
#define IIl break

is even more fun, particularly when read in a sans-serif, preferably
proportional, font.
 
T

Thomas Matthews

RoSsIaCrIiLoIA said:
Do you like my strlcpy strlcat etc?
Thank you
/*---------------------------------------*/
....

I think it could use some improvements:
#define M main
#define V void
#define I int
#define C char
#define R return
#define U unsigned
#define G goto
#define W while
#define F for
#define P printf
#define B break
#define SL strlen
#define CC const char


/************************************
* You should be consistent throughout your program.
* Consistency will help it run faster.
************************************/
i M (v)
{
srand((U)time(0));
test3(200000, 100, 100, strlcat, strlcat_m);
test1(3000000, 100, revstring, revstring_m );
R 0;
} /* main, endswith */
/* 25 + 25 + 10 = 60 0..59 */

C lettere(U j)
{C *u="abcdefghijklmnopqrstuvwyz"
"ABCDEFGHIJKLMNOPQRSTUVWYZ"
"0123456789";
R u[j%60];
}


V rand_phrase(char* a, size_t le)
{
U i=0;
/*------------------------------*/
assert(a!=0);
if(le==0) G l0;
la: a=lettere((U)rand()); if(++i<le)G la;
l0:
a=0;
}


/* does searchme end with the phrase phrase? */
/* By RoSsIaCrIiLoIA */
I endswith_m(CC* phrase, CC* searchme)
{CC *p1, *p2;
I r=0;
/*------------------------------*/
assert(phrase!=0 && searchme!=0);
p1= phrase +SL(phrase ); /**p1=*p2=0 */
p2= searchme+SL(searchme); /*ok caso *p1=0 o *p2=0 */
G l1;
l0: --p1; --p2;
l1: if(*p1!=*p2) G le;
if(p2==searchme)
{r=1;G le;}
if(p1!=phrase)G l0;
le:
return r;
}
[snip]

Since you don't want your programs easily readable
by others, here are some tips to make it compile
faster and to save time typing:
1. Get rid of all unnecessary spaces.
Spaces slow down the compilation phase.

2. Be consistent with abbreviations.
If you are going to use 'G' for 'goto', use
it for all 'gotos'. This will help your produce
programs faster since you don't have to type all
those unnecessary letters.

3. Get rid of all unnecessary line endings.
Line-ends are considered "white-space" and the
compiler must ignore them, wasting valuable
compilation time. The more expressions and
statements you can fit on a line, the better.

4. Reduce length of variables and function names.
This goes towards consistency and speed of
compilation. More letters occupy more space in a
compiler's symbol table. Also, more letters take
more time to process.

5. Comments slow down production.
They slow down the compiler and slow down your
typing. Produce more by typing less. After it
works, you won't have to deal with it anyway.

6. Use files sparingly.
Each #include file requires time to open, parse, and
close. Reduce the number in the source file.

Also, place as many functions or code into fewer files.
Again, the more files you have, the more time for
compilation and linking. One file, one compilation,
one linking, one program. Simple.

If you want people to assist you with your program,
you may have to make it readable and employ good coding
styles, which may violate some of the above rules.
You'll just have to decide which is more important.
We can't do that for you.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
K

Keith Thompson

Emmanuel Delahaye said:
Keith Thompson wrote on 04/02/05 :
RoSsIaCrIiLoIA said:
Do you like my strlcpy strlcat etc? [snip]
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break
[snip]

No.

#define ooo goto
#define ooO unsigned
#define oOo return
#define oOO while
#define Ooo for
#define OoO printf
#define OOo break

is more fun ...

I prefer:

#define O000 goto
#define O00O unsigned
#define O0O0 return
#define O0OO while
#define OO00 for
#define OO0O printf
#define OOO0 break
 
C

CBFalconer

Emmanuel said:
Keith Thompson wrote on 04/02/05 :
Do you like my strlcpy strlcat etc? [snip]
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break
[snip]

No.

#define ooo goto
#define ooO unsigned
#define oOo return
#define oOO while
#define Ooo for
#define OoO printf
#define OOo break

is more fun ...

Better yet, play with this:

#define O00
#define O0O
#define O0o
#define OO0
#define OOO
#define OOo
#define Oo0
#define OoO
#define Ooo
#define o00
#define o0O
#define o0o
#define oO0
#define oOO
#define oOo
#define oo0
#define ooO
#define ooo

(yes, they are all distinct (18) and legal identifiers) They are
in sorted order for ASCII.
 
Ad

Advertisements

C

CBFalconer

CBFalconer said:
Emmanuel said:
Keith Thompson wrote on 04/02/05 :
RoSsIaCrIiLoIA <[email protected]> writes:
Do you like my strlcpy strlcat etc? [snip]
#define G goto
#define U unsigned
#define R return
#define W while
#define F for
#define P printf
#define B break
[snip]

No.

#define ooo goto
#define ooO unsigned
#define oOo return
#define oOO while
#define Ooo for
#define OoO printf
#define OOo break

is more fun ...

Better yet, play with this:

#define O00
#define O0O
#define O0o
#define OO0
#define OOO
#define OOo
#define Oo0
#define OoO
#define Ooo
#define o00
#define o0O
#define o0o
#define oO0
#define oOO
#define oOo
#define oo0
#define ooO
#define ooo

(yes, they are all distinct (18) and legal identifiers) They are
in sorted order for ASCII.

and here's another set of 120:

O000 O001
O00O O00l O010 O011 O01O O01l O0O0 O0O1
O0OO O0Ol O0l0 O0l1 O0lO O0ll O100 O101
O10O O10l O110 O111 O11O O11l O1O0 O1O1
O1OO O1Ol O1l0 O1l1 O1lO O1ll OO00 OO01
OO0O OO0l OO10 OO11 OO1O OO1l OOO0 OOO1
OOOO OOOl OOl0 OOl1 OOlO OOll Ol00 Ol01
Ol0O Ol0l Ol10 Ol11 Ol1O Ol1l OlO0 OlO1
OlOO OlOl Oll0 Oll1 OllO Olll l000 l001
l00O l00l l010 l011 l01O l01l l0O0 l0O1
l0OO l0Ol l0l0 l0l1 l0lO l0ll l100 l101
l10O l10l l110 l111 l11O l11l l1O0 l1O1
l1OO l1Ol l1l0 l1l1 l1lO l1ll lO00 lO01
lO0O lO0l lO10 lO11 lO1O lO1l lOO0 lOO1
lOOO lOOl lOl0 lOl1 lOlO lOll ll00 ll01
ll0O ll0l ll10 ll11 ll1O ll1l llO0 llO1
llOO llOl lll0 lll1 lllO llll
 
K

Keith Thompson

Johan said:
No,

Real programmers do not use goto statements

john

Not true. Real programmers (at least good ones) do not use goto
statements inappropriately. They actually do have legitimate uses.
The real problem with goto statements is that misuse can create
spaghetti code. (Personally, I think a goto statement is often a sign
that the language is missing a feature that would make it unnecessary,
such as multi-level break or exception handling.)

Of course if you want to avoid them altogether yourself, that's fine.

But there are other reasons to dislike RoSsIaCrIiLoIA's code, which
we've discussed here before.

BTW, please don't top-post, and please don't quote hundreds of lines
of a previous article just to make a single comment.
 
Ad

Advertisements

C

CBFalconer

Johan said:
No,

Real programmers do not use goto statements

To spout this incorrect homily you had to quote over 300 lines of
nonsense? and top-post into the bargain! Well done, oh foul and
feckless critter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top