B
BartC
John Reye said:Hi,
recently I learned that various "standard C library" functions have
deficiencies in terms of high performance.
Examples include the return values of fgets(), strcpy(), strcat()
(thanks Eric Sosman for mentioning the last 2)
Example: char * strcat ( char * destination, const char *
source );
Return Value: destination is returned.
How useless is that! I already know the distination, in the first
place.
Why not return a pointer to the end of the concatenated string, or the
size of the string. This would cost the library no extra performance
cost whatsoever!
Are these deficiencies _only_ string-related?
It seems that way. But no-one is bothered about it, because writing
alternative versions (thin wrappers around standard functions) is trivial:
int strcat_l(char *s,char *t,int slen,int tlen){
if (slen<0) slen=strlen(s);
if (tlen<0) tlen=strlen(t);
memcpy(s+slen,t,tlen+1);
return slen+tlen;
}
int strcpy_l(char *s,char *t,int tlen){
if (tlen<0) tlen=strlen(t);
memcpy(s,t,tlen+1);
return tlen;
}
Etc.
(In this style that I sometimes use, you can optionally supply a string
length if you know it, otherwise -1 is passed. It's not hard to make them
faster than the standard functions. (Except when the lengths aren't known;
then it would probably be better for these to just call the standard
versions, instead of messing with strlen(). But I tend to use this form for
other kinds of string processing where a standard version doesn't exist.))