This is Erik Andersen's excellent strncpy.
It works like magic. That "if" isn't a jump;
gcc uses a few integer instructions to wipe
out all jumps except for the loop itself and
the function call/return.
This has been exhaustively tested against glibc.
The existing code has 5 extra branches and
is over twice as large. (my gcc, etc.)
{
char *tmp = dest;
- while (count && (*dest++ = *src++) != '\0')
- count--;
- while (count > 1) {
- *dest++ = 0;
+ while (count) {
+ if ((*tmp = *src) != 0) src++;
+ tmp++;
count--;
}
- return tmp;
+ return dest;
}
#endif