From 9100f7479afabc4bb2926c619b5ef09693cf9a94 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 9 Apr 2010 13:14:46 -0300 Subject: new implementation for Generic Buffer manipulation (using userdata as temporary buffer space) --- lstrlib.c | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'lstrlib.c') diff --git a/lstrlib.c b/lstrlib.c index c3a424f9..d3709a83 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.147 2009/12/17 12:50:20 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.148 2010/01/04 16:37:19 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -65,12 +65,13 @@ static int str_sub (lua_State *L) { static int str_reverse (lua_State *L) { - size_t l; + size_t l, i; luaL_Buffer b; const char *s = luaL_checklstring(L, 1, &l); - luaL_buffinit(L, &b); - while (l--) luaL_addchar(&b, s[l]); - luaL_pushresult(&b); + char *p = luaL_buffinitsize(L, &b, l); + for (i = 0; i < l; i++) + p[i] = s[l - i - 1]; + luaL_pushresultsize(&b, l); return 1; } @@ -80,10 +81,10 @@ static int str_lower (lua_State *L) { size_t i; luaL_Buffer b; const char *s = luaL_checklstring(L, 1, &l); - luaL_buffinit(L, &b); + char *p = luaL_buffinitsize(L, &b, l); for (i=0; i