Browse Source

new API function `lua_tolstring'

Roberto Ierusalimschy 20 years ago
parent
commit
da32450c3d
5 changed files with 26 additions and 24 deletions
  1. 10 8
      lapi.c
  2. 5 5
      lauxlib.c
  3. 2 5
      lbaselib.c
  4. 5 5
      lstrlib.c
  5. 4 1
      lua.h

+ 10 - 8
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);
 }
 
 

+ 5 - 5
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 */
   }

+ 2 - 5
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 */

+ 5 - 5
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<l; i++) {
       if (news[i] != L_ESC)

+ 4 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.205 2005/03/28 17:17:53 roberto Exp roberto $
+** $Id: lua.h,v 1.206 2005/05/05 20:47:02 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
 ** http://www.lua.org	mailto:[email protected]
@@ -148,6 +148,7 @@ LUA_API lua_Number      (lua_tonumber) (lua_State *L, int idx);
 LUA_API lua_Integer     (lua_tointeger) (lua_State *L, int idx);
 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);
 LUA_API size_t          (lua_objsize) (lua_State *L, int idx);
 LUA_API lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);
 LUA_API void	       *(lua_touserdata) (lua_State *L, int idx);
@@ -275,6 +276,8 @@ LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
 #define lua_setglobal(L,s)	lua_setfield(L, LUA_GLOBALSINDEX, (s))
 #define lua_getglobal(L,s)	lua_getfield(L, LUA_GLOBALSINDEX, (s))
 
+#define lua_tostring(L,i)	lua_tolstring(L, (i), NULL)
+
 
 
 /*