From da32450c3d4c8abd3fd6709692859a12a8886511 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 16 May 2005 16:21:11 -0300 Subject: [PATCH] new API function `lua_tolstring' --- lapi.c | 18 ++++++++++-------- lauxlib.c | 10 +++++----- lbaselib.c | 7 ++----- lstrlib.c | 10 +++++----- lua.h | 5 ++++- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lapi.c b/lapi.c index f6244e39..1f040b4e 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.38 2005/04/05 15:35:15 roberto Exp roberto $ +** $Id: lapi.c,v 2.39 2005/05/05 15:34:03 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -334,18 +334,20 @@ LUA_API int lua_toboolean (lua_State *L, int idx) { } -LUA_API const char *lua_tostring (lua_State *L, int idx) { +LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { StkId o = index2adr(L, idx); - if (ttisstring(o)) - return svalue(o); - else { - const char *s; + if (!ttisstring(o)) { lua_lock(L); /* `luaV_tostring' may create a new string */ - s = (luaV_tostring(L, o) ? svalue(o) : NULL); + if (!luaV_tostring(L, o)) { /* conversion failed? */ + if (len != NULL) *len = 0; + lua_unlock(L); + return NULL; + } luaC_checkGC(L); lua_unlock(L); - return s; } + if (len != NULL) *len = tsvalue(o)->len; + return svalue(o); } diff --git a/lauxlib.c b/lauxlib.c index 835ae5b9..fa188cbc 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.129 2005/02/23 17:30:22 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.130 2005/03/16 16:58:41 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -158,9 +158,8 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) { LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { - const char *s = lua_tostring(L, narg); + const char *s = lua_tolstring(L, narg, len); if (!s) tag_error(L, narg, LUA_TSTRING); - if (len) *len = lua_strlen(L, narg); return s; } @@ -497,9 +496,10 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) { LUALIB_API void luaL_addvalue (luaL_Buffer *B) { lua_State *L = B->L; - size_t vl = lua_strlen(L, -1); + size_t vl; + const char *s = lua_tolstring(L, -1, &vl); if (vl <= bufffree(B)) { /* fit into buffer? */ - memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */ + memcpy(B->p, s, vl); /* put it there */ B->p += vl; lua_pop(L, 1); /* remove from stack */ } diff --git a/lbaselib.c b/lbaselib.c index 358ca427..badce346 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.172 2005/03/22 16:04:29 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.173 2005/03/28 17:17:53 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -294,11 +294,8 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { return NULL; } else if (lua_isstring(L, -1)) { - const char *res; lua_replace(L, 3); /* save string in a reserved stack slot */ - res = lua_tostring(L, 3); - *size = lua_strlen(L, 3); - return res; + return lua_tolstring(L, 3, size); } else luaL_error(L, "reader function must return a string"); return NULL; /* to avoid warnings */ diff --git a/lstrlib.c b/lstrlib.c index 93f8e776..502ad9b3 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.111 2005/03/22 16:54:29 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.112 2005/05/05 15:34:03 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -525,8 +525,8 @@ static int str_find (lua_State *L) { static int gfind_aux (lua_State *L) { MatchState ms; - const char *s = lua_tostring(L, lua_upvalueindex(1)); - size_t ls = lua_strlen(L, lua_upvalueindex(1)); + size_t ls; + const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); const char *p = lua_tostring(L, lua_upvalueindex(2)); const char *src; ms.L = L; @@ -563,8 +563,8 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e) { lua_State *L = ms->L; if (lua_isstring(L, 3)) { - const char *news = lua_tostring(L, 3); - size_t l = lua_strlen(L, 3); + size_t l; + const char *news = lua_tolstring(L, 3, &l); size_t i; for (i=0; i