aboutsummaryrefslogtreecommitdiff
path: root/liolib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-10-23 17:16:17 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-10-23 17:16:17 -0300
commite3ce88c9e850b7e79751083014699c5eae1bff31 (patch)
treee73392a16c560ed532ef2238132f0e58d2eb23b3 /liolib.c
parent5ffcd458f001fce02e5f20a6130e145c6a3caf53 (diff)
downloadlua-e3ce88c9e850b7e79751083014699c5eae1bff31.tar.gz
lua-e3ce88c9e850b7e79751083014699c5eae1bff31.tar.bz2
lua-e3ce88c9e850b7e79751083014699c5eae1bff31.zip
New function 'lua_numbertostrbuff'
It converts a Lua number to a string in a buffer, without creating a new Lua string.
Diffstat (limited to 'liolib.c')
-rw-r--r--liolib.c22
1 files changed, 9 insertions, 13 deletions
diff --git a/liolib.c b/liolib.c
index 17522bb2..98ff3d0d 100644
--- a/liolib.c
+++ b/liolib.c
@@ -665,20 +665,16 @@ static int g_write (lua_State *L, FILE *f, int arg) {
665 int status = 1; 665 int status = 1;
666 errno = 0; 666 errno = 0;
667 for (; nargs--; arg++) { 667 for (; nargs--; arg++) {
668 if (lua_type(L, arg) == LUA_TNUMBER) { 668 char buff[LUA_N2SBUFFSZ];
669 /* optimization: could be done exactly as for strings */ 669 const char *s;
670 int len = lua_isinteger(L, arg) 670 size_t len = lua_numbertostrbuff(L, arg, buff); /* try as a number */
671 ? fprintf(f, LUA_INTEGER_FMT, 671 if (len > 0) { /* did conversion work (value was a number)? */
672 (LUAI_UACINT)lua_tointeger(L, arg)) 672 s = buff;
673 : fprintf(f, LUA_NUMBER_FMT, 673 len--;
674 (LUAI_UACNUMBER)lua_tonumber(L, arg));
675 status = status && (len > 0);
676 }
677 else {
678 size_t l;
679 const char *s = luaL_checklstring(L, arg, &l);
680 status = status && (fwrite(s, sizeof(char), l, f) == l);
681 } 674 }
675 else /* must be a string */
676 s = luaL_checklstring(L, arg, &len);
677 status = status && (fwrite(s, sizeof(char), len, f) == len);
682 } 678 }
683 if (l_likely(status)) 679 if (l_likely(status))
684 return 1; /* file handle already on stack top */ 680 return 1; /* file handle already on stack top */