From 462375ba47de2052687b3d17ff2eef4db4cab739 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 3 Apr 2012 16:06:19 -0300 Subject: [PATCH] 'if' to avoid empty 'memcpy' (may be expensive) --- lstrlib.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lstrlib.c b/lstrlib.c index b41ccd20..75f90432 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.172 2011/10/25 12:01:20 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.173 2011/11/30 18:24:56 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -119,7 +119,9 @@ static int str_rep (lua_State *L) { char *p = luaL_buffinitsize(L, &b, totallen); while (n-- > 1) { /* first n-1 copies (followed by separator) */ memcpy(p, s, l * sizeof(char)); p += l; - memcpy(p, sep, lsep * sizeof(char)); p += lsep; + if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */ + memcpy(p, sep, lsep * sizeof(char)); p += lsep; + } } memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */ luaL_pushresultsize(&b, totallen);