aboutsummaryrefslogtreecommitdiff
path: root/lobject.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-30 11:00:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-30 11:00:14 -0300
commit34ac039fb84e3c12fb8c96c9c99c34224c09872b (patch)
treea859f6338c6c851088a67b129765de662ed6f223 /lobject.c
parent1aa526263405fb4906eafab3011b13de4e5daf73 (diff)
downloadlua-34ac039fb84e3c12fb8c96c9c99c34224c09872b.tar.gz
lua-34ac039fb84e3c12fb8c96c9c99c34224c09872b.tar.bz2
lua-34ac039fb84e3c12fb8c96c9c99c34224c09872b.zip
new macro 'cvt2str' to better control whether numbers are convertible
to strings
Diffstat (limited to 'lobject.c')
-rw-r--r--lobject.c39
1 files changed, 32 insertions, 7 deletions
diff --git a/lobject.c b/lobject.c
index cd049075..d079e937 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 2.86 2014/05/12 21:44:17 roberto Exp roberto $ 2** $Id: lobject.c,v 2.87 2014/06/30 19:48:08 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -327,6 +327,32 @@ int luaO_utf8esc (char *buff, unsigned int x) {
327} 327}
328 328
329 329
330/* maximum length of the conversion of a number to a string */
331#define MAXNUMBER2STR 50
332
333
334/*
335** Convert a number object to a string
336*/
337void luaO_tostring (lua_State *L, StkId obj) {
338 char buff[MAXNUMBER2STR];
339 size_t len;
340 lua_assert(ttisnumber(obj));
341 if (ttisinteger(obj))
342 len = lua_integer2str(buff, ivalue(obj));
343 else {
344 len = lua_number2str(buff, fltvalue(obj));
345#if !defined(LUA_COMPAT_FLOATSTRING)
346 if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
347 buff[len++] = '.';
348 buff[len++] = '0'; /* adds '.0' to result */
349 }
350#endif
351 }
352 setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
353}
354
355
330static void pushstr (lua_State *L, const char *str, size_t l) { 356static void pushstr (lua_State *L, const char *str, size_t l) {
331 setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); 357 setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
332} 358}
@@ -349,24 +375,23 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
349 break; 375 break;
350 } 376 }
351 case 'c': { 377 case 'c': {
352 char buff; 378 char buff = cast(char, va_arg(argp, int));
353 buff = cast(char, va_arg(argp, int));
354 pushstr(L, &buff, 1); 379 pushstr(L, &buff, 1);
355 break; 380 break;
356 } 381 }
357 case 'd': { 382 case 'd': {
358 setivalue(L->top++, cast_int(va_arg(argp, int))); 383 setivalue(L->top++, va_arg(argp, int));
359 luaV_tostring(L, L->top - 1); 384 luaO_tostring(L, L->top - 1);
360 break; 385 break;
361 } 386 }
362 case 'I': { 387 case 'I': {
363 setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt))); 388 setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
364 luaV_tostring(L, L->top - 1); 389 luaO_tostring(L, L->top - 1);
365 break; 390 break;
366 } 391 }
367 case 'f': { 392 case 'f': {
368 setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); 393 setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
369 luaV_tostring(L, L->top - 1); 394 luaO_tostring(L, L->top - 1);
370 break; 395 break;
371 } 396 }
372 case 'p': { 397 case 'p': {