}
shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring);
for (i = 0; i < nargs; i++) {
+ cTValue *o = &L->base[i];
+ char buf[LJ_STR_NUMBERBUF];
const char *str;
size_t size;
- cTValue *o = &L->base[i];
- if (shortcut && tvisstr(o)) {
- str = strVdata(o);
- size = strV(o)->len;
- } else if (shortcut && tvisint(o)) {
- char buf[LJ_STR_INTBUF];
- char *p = lj_str_bufint(buf, intV(o));
- size = (size_t)(buf+LJ_STR_INTBUF-p);
- str = p;
- } else if (shortcut && tvisnum(o)) {
- char buf[LJ_STR_NUMBUF];
- size = lj_str_bufnum(buf, o);
- str = buf;
+ MSize len;
+ if (shortcut && (str = lj_str_buftv(buf, o, &len)) != NULL) {
+ size = len;
} else {
copyTV(L, L->top+1, o);
copyTV(L, L->top, L->top-1);
cTValue *tv;
int status = 1;
for (tv = L->base+start; tv < L->top; tv++) {
- if (tvisstr(tv)) {
- MSize len = strV(tv)->len;
- status = status && (fwrite(strVdata(tv), 1, len, fp) == len);
- } else if (tvisint(tv)) {
- char buf[LJ_STR_INTBUF];
- char *p = lj_str_bufint(buf, intV(tv));
- size_t len = (size_t)(buf+LJ_STR_INTBUF-p);
- status = status && (fwrite(p, 1, len, fp) == len);
- } else if (tvisnum(tv)) {
- status = status && (fprintf(fp, LUA_NUMBER_FMT, numV(tv)) > 0);
- } else {
+ char buf[LJ_STR_NUMBERBUF];
+ MSize len;
+ const char *p = lj_str_buftv(buf, tv, &len);
+ if (!p)
lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING);
- }
+ status = status && (fwrite(p, 1, len, fp) == len);
}
if (LJ_52 && status) {
L->top = L->base+1;
if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) {
/* Canonicalize output of non-finite values. */
char *p, nbuf[LJ_STR_NUMBUF];
- size_t len = lj_str_bufnum(nbuf, &tv);
+ MSize len = lj_str_bufnum(nbuf, &tv);
if (strfrmt[-1] < 'a') {
nbuf[len-3] = nbuf[len-3] - 0x20;
nbuf[len-2] = nbuf[len-2] - 0x20;
{
char buf[2*LJ_STR_NUMBUF+2+1];
TValue re, im;
- size_t len;
+ MSize len;
if (size == 2*sizeof(double)) {
re.n = *(double *)sp; im.n = ((double *)sp)[1];
} else {
/* -- Type conversions ---------------------------------------------------- */
/* Print number to buffer. Canonicalizes non-finite values. */
-size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o)
+MSize LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o)
{
if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */
lua_Number n = o->n;
#if __BIONIC__
if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; }
#endif
- return (size_t)lua_number2str(s, n);
+ return (MSize)lua_number2str(s, n);
} else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) {
s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3;
} else if ((o->u32.hi & 0x80000000) == 0) {
}
}
-/* Print integer to buffer. Returns pointer to start. */
-char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k)
+/* Print integer to buffer. Returns pointer to start (!= buffer start). */
+static char * str_bufint(char *p, int32_t k)
{
uint32_t u = (uint32_t)(k < 0 ? -k : k);
- p += 1+10;
+ p += LJ_STR_INTBUF;
do { *--p = (char)('0' + u % 10); } while (u /= 10);
if (k < 0) *--p = '-';
return p;
}
+/* Print TValue to buffer (only for numbers) and return pointer to start. */
+const char *lj_str_buftv(char *buf, cTValue *o, MSize *lenp)
+{
+ if (tvisstr(o)) {
+ *lenp = strV(o)->len;
+ return strVdata(o);
+ } else if (tvisint(o)) {
+ char *p = str_bufint(buf, intV(o));
+ *lenp = (MSize)(buf+LJ_STR_INTBUF-p);
+ return p;
+ } else if (tvisnum(o)) {
+ *lenp = lj_str_bufnum(buf, o);
+ return buf;
+ } else {
+ return NULL;
+ }
+}
+
/* Convert number to string. */
GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np)
{
char buf[LJ_STR_NUMBUF];
- size_t len = lj_str_bufnum(buf, (TValue *)np);
+ MSize len = lj_str_bufnum(buf, (TValue *)np);
return lj_str_new(L, buf, len);
}
/* Convert integer to string. */
GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k)
{
- char s[1+10];
- char *p = lj_str_bufint(s, k);
- return lj_str_new(L, p, (size_t)(s+sizeof(s)-p));
+ char buf[LJ_STR_INTBUF];
+ char *p = str_bufint(buf, k);
+ return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p));
}
GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o)
break;
case 'd': {
char buf[LJ_STR_INTBUF];
- char *p = lj_str_bufint(buf, va_arg(argp, int32_t));
+ char *p = str_bufint(buf, va_arg(argp, int32_t));
lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p));
break;
}
#define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1))
/* Type conversions. */
-LJ_FUNC size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o);
-LJ_FUNC char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k);
+LJ_FUNC MSize LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o);
+LJ_FUNC const char *lj_str_buftv(char *buf, cTValue *o, MSize *lenp);
LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np);
LJ_FUNC GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k);
LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o);
#define LJ_STR_INTBUF (1+10)
#define LJ_STR_NUMBUF LUAI_MAXNUMBER2STR
+#define LJ_STR_NUMBERBUF LUAI_MAXNUMBER2STR
/* String formatting. */
LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp);