Clean up TValue to buffer conversions.

This commit is contained in:
Mike Pall 2013-03-18 17:08:37 +01:00
parent 18d7c975d6
commit d1645c88a1
6 changed files with 43 additions and 39 deletions

View File

@ -506,21 +506,13 @@ LJLIB_CF(print)
} }
shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring); shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring);
for (i = 0; i < nargs; i++) { for (i = 0; i < nargs; i++) {
cTValue *o = &L->base[i];
char buf[LJ_STR_NUMBERBUF];
const char *str; const char *str;
size_t size; size_t size;
cTValue *o = &L->base[i]; MSize len;
if (shortcut && tvisstr(o)) { if (shortcut && (str = lj_str_buftv(buf, o, &len)) != NULL) {
str = strVdata(o); size = len;
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;
} else { } else {
copyTV(L, L->top+1, o); copyTV(L, L->top+1, o);
copyTV(L, L->top, L->top-1); copyTV(L, L->top, L->top-1);

View File

@ -231,19 +231,12 @@ static int io_file_write(lua_State *L, FILE *fp, int start)
cTValue *tv; cTValue *tv;
int status = 1; int status = 1;
for (tv = L->base+start; tv < L->top; tv++) { for (tv = L->base+start; tv < L->top; tv++) {
if (tvisstr(tv)) { char buf[LJ_STR_NUMBERBUF];
MSize len = strV(tv)->len; MSize len;
status = status && (fwrite(strVdata(tv), 1, len, fp) == len); const char *p = lj_str_buftv(buf, tv, &len);
} else if (tvisint(tv)) { if (!p)
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 {
lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING); lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING);
} status = status && (fwrite(p, 1, len, fp) == len);
} }
if (LJ_52 && status) { if (LJ_52 && status) {
L->top = L->base+1; L->top = L->base+1;

View File

@ -873,7 +873,7 @@ LJLIB_CF(string_format)
if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) { if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) {
/* Canonicalize output of non-finite values. */ /* Canonicalize output of non-finite values. */
char *p, nbuf[LJ_STR_NUMBUF]; 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') { if (strfrmt[-1] < 'a') {
nbuf[len-3] = nbuf[len-3] - 0x20; nbuf[len-3] = nbuf[len-3] - 0x20;
nbuf[len-2] = nbuf[len-2] - 0x20; nbuf[len-2] = nbuf[len-2] - 0x20;

View File

@ -570,7 +570,7 @@ GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size)
{ {
char buf[2*LJ_STR_NUMBUF+2+1]; char buf[2*LJ_STR_NUMBUF+2+1];
TValue re, im; TValue re, im;
size_t len; MSize len;
if (size == 2*sizeof(double)) { if (size == 2*sizeof(double)) {
re.n = *(double *)sp; im.n = ((double *)sp)[1]; re.n = *(double *)sp; im.n = ((double *)sp)[1];
} else { } else {

View File

@ -168,14 +168,14 @@ void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s)
/* -- Type conversions ---------------------------------------------------- */ /* -- Type conversions ---------------------------------------------------- */
/* Print number to buffer. Canonicalizes non-finite values. */ /* 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? */ if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */
lua_Number n = o->n; lua_Number n = o->n;
#if __BIONIC__ #if __BIONIC__
if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; } if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; }
#endif #endif
return (size_t)lua_number2str(s, n); return (MSize)lua_number2str(s, n);
} else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) { } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) {
s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3; s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3;
} else if ((o->u32.hi & 0x80000000) == 0) { } else if ((o->u32.hi & 0x80000000) == 0) {
@ -185,30 +185,48 @@ size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o)
} }
} }
/* Print integer to buffer. Returns pointer to start. */ /* Print integer to buffer. Returns pointer to start (!= buffer start). */
char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k) static char * str_bufint(char *p, int32_t k)
{ {
uint32_t u = (uint32_t)(k < 0 ? -k : 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); do { *--p = (char)('0' + u % 10); } while (u /= 10);
if (k < 0) *--p = '-'; if (k < 0) *--p = '-';
return 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. */ /* Convert number to string. */
GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np) GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np)
{ {
char buf[LJ_STR_NUMBUF]; 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); return lj_str_new(L, buf, len);
} }
/* Convert integer to string. */ /* Convert integer to string. */
GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k) GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k)
{ {
char s[1+10]; char buf[LJ_STR_INTBUF];
char *p = lj_str_bufint(s, k); char *p = str_bufint(buf, k);
return lj_str_new(L, p, (size_t)(s+sizeof(s)-p)); return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p));
} }
GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o) GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o)
@ -242,7 +260,7 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
break; break;
case 'd': { case 'd': {
char buf[LJ_STR_INTBUF]; 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)); lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p));
break; break;
} }

View File

@ -20,14 +20,15 @@ LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s);
#define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1)) #define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1))
/* Type conversions. */ /* Type conversions. */
LJ_FUNC size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o); LJ_FUNC MSize LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o);
LJ_FUNC char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k); 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_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_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); LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o);
#define LJ_STR_INTBUF (1+10) #define LJ_STR_INTBUF (1+10)
#define LJ_STR_NUMBUF LUAI_MAXNUMBER2STR #define LJ_STR_NUMBUF LUAI_MAXNUMBER2STR
#define LJ_STR_NUMBERBUF LUAI_MAXNUMBER2STR
/* String formatting. */ /* String formatting. */
LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp); LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp);