Re: Comment on trim string function please
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Sat, 12 Jul 2008 17:49:11 +0100
"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:
<snip>
In any event, does the following win the prize for most efficient
implementation of the presumed requirements of the function?
I think you have a bug.
char *remove_beg_end_non_text(char *text) {
char *beg;
size_t length;
for(beg=text;*beg!='\0';beg++)
if(!isspace(*beg)) break;
I'd guess
for (beg = text; isspace((unsigned char)*beg); beg++);
makes shorter code with some compilers.
length=strlen(beg);
while(length>0)
if(!isspace(*(beg+(--length)))) break;
*(beg+(++length))='\0';
If length never is never decremented (because it was zero after the
initial space scan) then this writes outside the string. It always
helps to walk through what your code does in boundary cases like ""
and " ".
return beg==text ? text : memmove(text,beg,length+1);
}
Gotta admit, you couldn't reduce the cycles too much on
that, could you? And it could even win a little bonus prize
for obfuscatory conditions like if(!isspace(*(beg+(--length))))...
You might have obscure it even to yourself!
--
Ben.
.
- Follow-Ups:
- Re: Comment on trim string function please
- From: Bill Reid
- Re: Comment on trim string function please
- References:
- Comment on trim string function please
- From: swengineer001@xxxxxxxxx
- Re: Comment on trim string function please
- From: Jens Thoms Toerring
- Re: Comment on trim string function please
- From: Bill Reid
- Re: Comment on trim string function please
- From: Willem
- Re: Comment on trim string function please
- From: Bill Reid
- Comment on trim string function please
- Prev by Date: Re: The C language in the planet Mars
- Next by Date: Stats for comp.lang.c (last 7 days)
- Previous by thread: Re: Comment on trim string function please
- Next by thread: Re: Comment on trim string function please
- Index(es):