]> git.hungrycats.org Git - linux/commitdiff
[PATCH] fast AND correct strncpy
authorAlbert Cahalan <albert@users.sourceforge.net>
Sun, 17 Aug 2003 01:48:39 +0000 (18:48 -0700)
committerDoug Ledford <dledford@compaq.xsintricity.com>
Sun, 17 Aug 2003 01:48:39 +0000 (18:48 -0700)
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.)

lib/string.c

index 05dfb7ff5fd92407268144104a8f261dbc23ab10..4917b54e605ff93d8b4cbbc1cec45f669f1aff12 100644 (file)
@@ -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