Bug/Gross InEfficiency in HeathField's fgetline program
- From: Antoninus Twink <nospam@xxxxxxxxxx>
- Date: Mon, 8 Oct 2007 00:16:07 +0200 (CEST)
The function below is from Richard HeathField's fgetline program. For
some reason, it makes three passes through the string (a strlen(), a
strcpy() then another pass to change dots) when two would clearly be
sufficient. This could lead to unnecessarily bad performance on very
long strings. It is also written in a hard-to-read and clunky style.
char *dot_to_underscore(const char *s)
{
char *t = malloc(strlen(s) + 1);
if(t != NULL)
{
char *u;
strcpy(t, s);
u = t;
while(*u)
{
if(*u == '.')
{
*u = '_';
}
++u;
}
}
return
t;
}
Proposed solution:
char *dot_to_underscore(const char *s)
{
char *t, *u;
if(t=u=malloc(strlen(s)+1))
while(*u++=(*s=='.' ? s++, '_' : *s++));
return t;
}
.
- Follow-Ups:
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: ¬a\\/b
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: John Bode
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: ¬a\\/b
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Richard
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Chris Hills
- Re:Bug/Gross InEfficiency in HeathField's fgetline program
- From: Peter Nilsson
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Tor Rustad
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Mark McIntyre
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- From: Richard Heathfield
- Re: Bug/Gross InEfficiency in HeathField's fgetline program
- Prev by Date: Re: First things you check for when you've got errors
- Next by Date: Re: [OT] Re: Should we broaden the topicality of this group?
- Previous by thread: Size of memory Pointed-to
- Next by thread: Re: Bug/Gross InEfficiency in HeathField's fgetline program
- Index(es):
Relevant Pages
|
|