aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-05-16 16:21:11 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2005-05-16 16:21:11 -0300
commitda32450c3d4c8abd3fd6709692859a12a8886511 (patch)
treea75289cd092dd6cbd9e454c9689de3b9c4d5de33
parenta2b78aad49388c1fd5286773085ef8a35545faa6 (diff)
downloadlua-da32450c3d4c8abd3fd6709692859a12a8886511.tar.gz
lua-da32450c3d4c8abd3fd6709692859a12a8886511.tar.bz2
lua-da32450c3d4c8abd3fd6709692859a12a8886511.zip
new API function `lua_tolstring'
-rw-r--r--lapi.c18
-rw-r--r--lauxlib.c10
-rw-r--r--lbaselib.c7
-rw-r--r--lstrlib.c10
-rw-r--r--lua.h5
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 @@
1/* 1/*
2** $Id: lapi.c,v 2.38 2005/04/05 15:35:15 roberto Exp roberto $ 2** $Id: lapi.c,v 2.39 2005/05/05 15:34:03 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -334,18 +334,20 @@ LUA_API int lua_toboolean (lua_State *L, int idx) {
334} 334}
335 335
336 336
337LUA_API const char *lua_tostring (lua_State *L, int idx) { 337LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
338 StkId o = index2adr(L, idx); 338 StkId o = index2adr(L, idx);
339 if (ttisstring(o)) 339 if (!ttisstring(o)) {
340 return svalue(o);
341 else {
342 const char *s;
343 lua_lock(L); /* `luaV_tostring' may create a new string */ 340 lua_lock(L); /* `luaV_tostring' may create a new string */
344 s = (luaV_tostring(L, o) ? svalue(o) : NULL); 341 if (!luaV_tostring(L, o)) { /* conversion failed? */
342 if (len != NULL) *len = 0;
343 lua_unlock(L);
344 return NULL;
345 }
345 luaC_checkGC(L); 346 luaC_checkGC(L);
346 lua_unlock(L); 347 lua_unlock(L);
347 return s;
348 } 348 }
349 if (len != NULL) *len = tsvalue(o)->len;
350 return svalue(o);
349} 351}
350 352
351 353
diff --git a/lauxlib.c b/lauxlib.c
index 835ae5b9..fa188cbc 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.129 2005/02/23 17:30:22 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.130 2005/03/16 16:58:41 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -158,9 +158,8 @@ LUALIB_API void luaL_checkany (lua_State *L, int narg) {
158 158
159 159
160LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { 160LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {
161 const char *s = lua_tostring(L, narg); 161 const char *s = lua_tolstring(L, narg, len);
162 if (!s) tag_error(L, narg, LUA_TSTRING); 162 if (!s) tag_error(L, narg, LUA_TSTRING);
163 if (len) *len = lua_strlen(L, narg);
164 return s; 163 return s;
165} 164}
166 165
@@ -497,9 +496,10 @@ LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
497 496
498LUALIB_API void luaL_addvalue (luaL_Buffer *B) { 497LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
499 lua_State *L = B->L; 498 lua_State *L = B->L;
500 size_t vl = lua_strlen(L, -1); 499 size_t vl;
500 const char *s = lua_tolstring(L, -1, &vl);
501 if (vl <= bufffree(B)) { /* fit into buffer? */ 501 if (vl <= bufffree(B)) { /* fit into buffer? */
502 memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */ 502 memcpy(B->p, s, vl); /* put it there */
503 B->p += vl; 503 B->p += vl;
504 lua_pop(L, 1); /* remove from stack */ 504 lua_pop(L, 1); /* remove from stack */
505 } 505 }
diff --git a/lbaselib.c b/lbaselib.c
index 358ca427..badce346 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbaselib.c,v 1.172 2005/03/22 16:04:29 roberto Exp roberto $ 2** $Id: lbaselib.c,v 1.173 2005/03/28 17:17:53 roberto Exp roberto $
3** Basic library 3** Basic library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -294,11 +294,8 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
294 return NULL; 294 return NULL;
295 } 295 }
296 else if (lua_isstring(L, -1)) { 296 else if (lua_isstring(L, -1)) {
297 const char *res;
298 lua_replace(L, 3); /* save string in a reserved stack slot */ 297 lua_replace(L, 3); /* save string in a reserved stack slot */
299 res = lua_tostring(L, 3); 298 return lua_tolstring(L, 3, size);
300 *size = lua_strlen(L, 3);
301 return res;
302 } 299 }
303 else luaL_error(L, "reader function must return a string"); 300 else luaL_error(L, "reader function must return a string");
304 return NULL; /* to avoid warnings */ 301 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 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.111 2005/03/22 16:54:29 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.112 2005/05/05 15:34:03 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -525,8 +525,8 @@ static int str_find (lua_State *L) {
525 525
526static int gfind_aux (lua_State *L) { 526static int gfind_aux (lua_State *L) {
527 MatchState ms; 527 MatchState ms;
528 const char *s = lua_tostring(L, lua_upvalueindex(1)); 528 size_t ls;
529 size_t ls = lua_strlen(L, lua_upvalueindex(1)); 529 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
530 const char *p = lua_tostring(L, lua_upvalueindex(2)); 530 const char *p = lua_tostring(L, lua_upvalueindex(2));
531 const char *src; 531 const char *src;
532 ms.L = L; 532 ms.L = L;
@@ -563,8 +563,8 @@ static void add_s (MatchState *ms, luaL_Buffer *b,
563 const char *s, const char *e) { 563 const char *s, const char *e) {
564 lua_State *L = ms->L; 564 lua_State *L = ms->L;
565 if (lua_isstring(L, 3)) { 565 if (lua_isstring(L, 3)) {
566 const char *news = lua_tostring(L, 3); 566 size_t l;
567 size_t l = lua_strlen(L, 3); 567 const char *news = lua_tolstring(L, 3, &l);
568 size_t i; 568 size_t i;
569 for (i=0; i<l; i++) { 569 for (i=0; i<l; i++) {
570 if (news[i] != L_ESC) 570 if (news[i] != L_ESC)
diff --git a/lua.h b/lua.h
index 7bf8d33f..eae6fd8d 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.205 2005/03/28 17:17:53 roberto Exp roberto $ 2** $Id: lua.h,v 1.206 2005/05/05 20:47:02 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil 4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5** http://www.lua.org mailto:info@lua.org 5** http://www.lua.org mailto:info@lua.org
@@ -148,6 +148,7 @@ LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx);
148LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); 148LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx);
149LUA_API int (lua_toboolean) (lua_State *L, int idx); 149LUA_API int (lua_toboolean) (lua_State *L, int idx);
150LUA_API const char *(lua_tostring) (lua_State *L, int idx); 150LUA_API const char *(lua_tostring) (lua_State *L, int idx);
151LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
151LUA_API size_t (lua_objsize) (lua_State *L, int idx); 152LUA_API size_t (lua_objsize) (lua_State *L, int idx);
152LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); 153LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx);
153LUA_API void *(lua_touserdata) (lua_State *L, int idx); 154LUA_API void *(lua_touserdata) (lua_State *L, int idx);
@@ -275,6 +276,8 @@ LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
275#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) 276#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s))
276#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) 277#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s))
277 278
279#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)
280
278 281
279 282
280/* 283/*