diff options
| author | Albert Cahalan <albert@users.sourceforge.net> | 2003-08-16 18:48:39 -0700 |
|---|---|---|
| committer | Doug Ledford <dledford@compaq.xsintricity.com> | 2003-08-16 18:48:39 -0700 |
| commit | dd14119ad5975b8f53f750edca330e20ef33d655 (patch) | |
| tree | c323536d4018d1008da73af67c604f10b5db871b /lib/string.c | |
| parent | fc132fde973a2727f05c2d6806302f6e25801534 (diff) | |
[PATCH] fast AND correct strncpy
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.)
Diffstat (limited to 'lib/string.c')
| -rw-r--r-- | lib/string.c | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/lib/string.c b/lib/string.c index 05dfb7ff5fd9..4917b54e605f 100644 --- a/lib/string.c +++ b/lib/string.c @@ -87,13 +87,12 @@ char * strncpy(char * dest,const char *src,size_t count) { 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 |
