New function 'lua_numbertostrbuff'

It converts a Lua number to a string in a buffer, without creating
a new Lua string.
This commit is contained in:
Roberto Ierusalimschy
2024-10-23 17:16:17 -03:00
parent 5ffcd458f0
commit e3ce88c9e8
7 changed files with 67 additions and 40 deletions

12
lapi.c
View File

@@ -368,6 +368,18 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
}
LUA_API unsigned (lua_numbertostrbuff) (lua_State *L, int idx, char *buff) {
const TValue *o = index2value(L, idx);
if (ttisnumber(o)) {
unsigned len = luaO_tostringbuff(o, buff);
buff[len++] = '\0'; /* add final zero */
return len;
}
else
return 0;
}
LUA_API size_t lua_stringtonumber (lua_State *L, const char *s) {
size_t sz = luaO_str2num(s, s2v(L->top.p));
if (sz != 0)