From dd14119ad5975b8f53f750edca330e20ef33d655 Mon Sep 17 00:00:00 2001 From: Albert Cahalan Date: Sat, 16 Aug 2003 18:48:39 -0700 Subject: [PATCH] [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.) --- lib/string.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/string.c b/lib/string.c index 05dfb7ff5fd92..4917b54e605ff 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 -- 2.53.0