summaryrefslogtreecommitdiff
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-github-34ac039fb84e3c12fb8c96c9c99c34224c09872b.tar.gz
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 @@
/*
-** $Id: lobject.c,v 2.86 2014/05/12 21:44:17 roberto Exp roberto $
+** $Id: lobject.c,v 2.87 2014/06/30 19:48:08 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@@ -327,6 +327,32 @@ int luaO_utf8esc (char *buff, unsigned int x) {
}
+/* maximum length of the conversion of a number to a string */
+#define MAXNUMBER2STR 50
+
+
+/*
+** Convert a number object to a string
+*/
+void luaO_tostring (lua_State *L, StkId obj) {
+ char buff[MAXNUMBER2STR];
+ size_t len;
+ lua_assert(ttisnumber(obj));
+ if (ttisinteger(obj))
+ len = lua_integer2str(buff, ivalue(obj));
+ else {
+ len = lua_number2str(buff, fltvalue(obj));
+#if !defined(LUA_COMPAT_FLOATSTRING)
+ if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
+ buff[len++] = '.';
+ buff[len++] = '0'; /* adds '.0' to result */
+ }
+#endif
+ }
+ setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
+}
+
+
static void pushstr (lua_State *L, const char *str, size_t l) {
setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
}
@@ -349,24 +375,23 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
break;
}
case 'c': {
- char buff;
- buff = cast(char, va_arg(argp, int));
+ char buff = cast(char, va_arg(argp, int));
pushstr(L, &buff, 1);
break;
}
case 'd': {
- setivalue(L->top++, cast_int(va_arg(argp, int)));
- luaV_tostring(L, L->top - 1);
+ setivalue(L->top++, va_arg(argp, int));
+ luaO_tostring(L, L->top - 1);
break;
}
case 'I': {
setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
- luaV_tostring(L, L->top - 1);
+ luaO_tostring(L, L->top - 1);
break;
}
case 'f': {
setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
- luaV_tostring(L, L->top - 1);
+ luaO_tostring(L, L->top - 1);
break;
}
case 'p': {