diff options
| -rw-r--r-- | src/lib_base.c | 18 | ||||
| -rw-r--r-- | src/lib_io.c | 17 | ||||
| -rw-r--r-- | src/lib_string.c | 2 | ||||
| -rw-r--r-- | src/lj_ctype.c | 2 | ||||
| -rw-r--r-- | src/lj_str.c | 38 | ||||
| -rw-r--r-- | src/lj_str.h | 5 |
6 files changed, 43 insertions, 39 deletions
diff --git a/src/lib_base.c b/src/lib_base.c index 070970ed..8fecddea 100644 --- a/src/lib_base.c +++ b/src/lib_base.c | |||
| @@ -506,21 +506,13 @@ LJLIB_CF(print) | |||
| 506 | } | 506 | } |
| 507 | shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring); | 507 | shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring); |
| 508 | for (i = 0; i < nargs; i++) { | 508 | for (i = 0; i < nargs; i++) { |
| 509 | cTValue *o = &L->base[i]; | ||
| 510 | char buf[LJ_STR_NUMBERBUF]; | ||
| 509 | const char *str; | 511 | const char *str; |
| 510 | size_t size; | 512 | size_t size; |
| 511 | cTValue *o = &L->base[i]; | 513 | MSize len; |
| 512 | if (shortcut && tvisstr(o)) { | 514 | if (shortcut && (str = lj_str_buftv(buf, o, &len)) != NULL) { |
| 513 | str = strVdata(o); | 515 | size = len; |
| 514 | size = strV(o)->len; | ||
| 515 | } else if (shortcut && tvisint(o)) { | ||
| 516 | char buf[LJ_STR_INTBUF]; | ||
| 517 | char *p = lj_str_bufint(buf, intV(o)); | ||
| 518 | size = (size_t)(buf+LJ_STR_INTBUF-p); | ||
| 519 | str = p; | ||
| 520 | } else if (shortcut && tvisnum(o)) { | ||
| 521 | char buf[LJ_STR_NUMBUF]; | ||
| 522 | size = lj_str_bufnum(buf, o); | ||
| 523 | str = buf; | ||
| 524 | } else { | 516 | } else { |
| 525 | copyTV(L, L->top+1, o); | 517 | copyTV(L, L->top+1, o); |
| 526 | copyTV(L, L->top, L->top-1); | 518 | copyTV(L, L->top, L->top-1); |
diff --git a/src/lib_io.c b/src/lib_io.c index 5369d450..18d87a89 100644 --- a/src/lib_io.c +++ b/src/lib_io.c | |||
| @@ -231,19 +231,12 @@ static int io_file_write(lua_State *L, FILE *fp, int start) | |||
| 231 | cTValue *tv; | 231 | cTValue *tv; |
| 232 | int status = 1; | 232 | int status = 1; |
| 233 | for (tv = L->base+start; tv < L->top; tv++) { | 233 | for (tv = L->base+start; tv < L->top; tv++) { |
| 234 | if (tvisstr(tv)) { | 234 | char buf[LJ_STR_NUMBERBUF]; |
| 235 | MSize len = strV(tv)->len; | 235 | MSize len; |
| 236 | status = status && (fwrite(strVdata(tv), 1, len, fp) == len); | 236 | const char *p = lj_str_buftv(buf, tv, &len); |
| 237 | } else if (tvisint(tv)) { | 237 | if (!p) |
| 238 | char buf[LJ_STR_INTBUF]; | ||
| 239 | char *p = lj_str_bufint(buf, intV(tv)); | ||
| 240 | size_t len = (size_t)(buf+LJ_STR_INTBUF-p); | ||
| 241 | status = status && (fwrite(p, 1, len, fp) == len); | ||
| 242 | } else if (tvisnum(tv)) { | ||
| 243 | status = status && (fprintf(fp, LUA_NUMBER_FMT, numV(tv)) > 0); | ||
| 244 | } else { | ||
| 245 | lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING); | 238 | lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING); |
| 246 | } | 239 | status = status && (fwrite(p, 1, len, fp) == len); |
| 247 | } | 240 | } |
| 248 | if (LJ_52 && status) { | 241 | if (LJ_52 && status) { |
| 249 | L->top = L->base+1; | 242 | L->top = L->base+1; |
diff --git a/src/lib_string.c b/src/lib_string.c index 36fd3f53..69b29356 100644 --- a/src/lib_string.c +++ b/src/lib_string.c | |||
| @@ -873,7 +873,7 @@ LJLIB_CF(string_format) | |||
| 873 | if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) { | 873 | if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) { |
| 874 | /* Canonicalize output of non-finite values. */ | 874 | /* Canonicalize output of non-finite values. */ |
| 875 | char *p, nbuf[LJ_STR_NUMBUF]; | 875 | char *p, nbuf[LJ_STR_NUMBUF]; |
| 876 | size_t len = lj_str_bufnum(nbuf, &tv); | 876 | MSize len = lj_str_bufnum(nbuf, &tv); |
| 877 | if (strfrmt[-1] < 'a') { | 877 | if (strfrmt[-1] < 'a') { |
| 878 | nbuf[len-3] = nbuf[len-3] - 0x20; | 878 | nbuf[len-3] = nbuf[len-3] - 0x20; |
| 879 | nbuf[len-2] = nbuf[len-2] - 0x20; | 879 | nbuf[len-2] = nbuf[len-2] - 0x20; |
diff --git a/src/lj_ctype.c b/src/lj_ctype.c index 57a0d7cc..69ba76d1 100644 --- a/src/lj_ctype.c +++ b/src/lj_ctype.c | |||
| @@ -570,7 +570,7 @@ GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size) | |||
| 570 | { | 570 | { |
| 571 | char buf[2*LJ_STR_NUMBUF+2+1]; | 571 | char buf[2*LJ_STR_NUMBUF+2+1]; |
| 572 | TValue re, im; | 572 | TValue re, im; |
| 573 | size_t len; | 573 | MSize len; |
| 574 | if (size == 2*sizeof(double)) { | 574 | if (size == 2*sizeof(double)) { |
| 575 | re.n = *(double *)sp; im.n = ((double *)sp)[1]; | 575 | re.n = *(double *)sp; im.n = ((double *)sp)[1]; |
| 576 | } else { | 576 | } else { |
diff --git a/src/lj_str.c b/src/lj_str.c index 67353154..5482f7c4 100644 --- a/src/lj_str.c +++ b/src/lj_str.c | |||
| @@ -168,14 +168,14 @@ void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s) | |||
| 168 | /* -- Type conversions ---------------------------------------------------- */ | 168 | /* -- Type conversions ---------------------------------------------------- */ |
| 169 | 169 | ||
| 170 | /* Print number to buffer. Canonicalizes non-finite values. */ | 170 | /* Print number to buffer. Canonicalizes non-finite values. */ |
| 171 | size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o) | 171 | MSize LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o) |
| 172 | { | 172 | { |
| 173 | if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */ | 173 | if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */ |
| 174 | lua_Number n = o->n; | 174 | lua_Number n = o->n; |
| 175 | #if __BIONIC__ | 175 | #if __BIONIC__ |
| 176 | if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; } | 176 | if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; } |
| 177 | #endif | 177 | #endif |
| 178 | return (size_t)lua_number2str(s, n); | 178 | return (MSize)lua_number2str(s, n); |
| 179 | } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) { | 179 | } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) { |
| 180 | s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3; | 180 | s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3; |
| 181 | } else if ((o->u32.hi & 0x80000000) == 0) { | 181 | } else if ((o->u32.hi & 0x80000000) == 0) { |
| @@ -185,30 +185,48 @@ size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o) | |||
| 185 | } | 185 | } |
| 186 | } | 186 | } |
| 187 | 187 | ||
| 188 | /* Print integer to buffer. Returns pointer to start. */ | 188 | /* Print integer to buffer. Returns pointer to start (!= buffer start). */ |
| 189 | char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k) | 189 | static char * str_bufint(char *p, int32_t k) |
| 190 | { | 190 | { |
| 191 | uint32_t u = (uint32_t)(k < 0 ? -k : k); | 191 | uint32_t u = (uint32_t)(k < 0 ? -k : k); |
| 192 | p += 1+10; | 192 | p += LJ_STR_INTBUF; |
| 193 | do { *--p = (char)('0' + u % 10); } while (u /= 10); | 193 | do { *--p = (char)('0' + u % 10); } while (u /= 10); |
| 194 | if (k < 0) *--p = '-'; | 194 | if (k < 0) *--p = '-'; |
| 195 | return p; | 195 | return p; |
| 196 | } | 196 | } |
| 197 | 197 | ||
| 198 | /* Print TValue to buffer (only for numbers) and return pointer to start. */ | ||
| 199 | const char *lj_str_buftv(char *buf, cTValue *o, MSize *lenp) | ||
| 200 | { | ||
| 201 | if (tvisstr(o)) { | ||
| 202 | *lenp = strV(o)->len; | ||
| 203 | return strVdata(o); | ||
| 204 | } else if (tvisint(o)) { | ||
| 205 | char *p = str_bufint(buf, intV(o)); | ||
| 206 | *lenp = (MSize)(buf+LJ_STR_INTBUF-p); | ||
| 207 | return p; | ||
| 208 | } else if (tvisnum(o)) { | ||
| 209 | *lenp = lj_str_bufnum(buf, o); | ||
| 210 | return buf; | ||
| 211 | } else { | ||
| 212 | return NULL; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 198 | /* Convert number to string. */ | 216 | /* Convert number to string. */ |
| 199 | GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np) | 217 | GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np) |
| 200 | { | 218 | { |
| 201 | char buf[LJ_STR_NUMBUF]; | 219 | char buf[LJ_STR_NUMBUF]; |
| 202 | size_t len = lj_str_bufnum(buf, (TValue *)np); | 220 | MSize len = lj_str_bufnum(buf, (TValue *)np); |
| 203 | return lj_str_new(L, buf, len); | 221 | return lj_str_new(L, buf, len); |
| 204 | } | 222 | } |
| 205 | 223 | ||
| 206 | /* Convert integer to string. */ | 224 | /* Convert integer to string. */ |
| 207 | GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k) | 225 | GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k) |
| 208 | { | 226 | { |
| 209 | char s[1+10]; | 227 | char buf[LJ_STR_INTBUF]; |
| 210 | char *p = lj_str_bufint(s, k); | 228 | char *p = str_bufint(buf, k); |
| 211 | return lj_str_new(L, p, (size_t)(s+sizeof(s)-p)); | 229 | return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p)); |
| 212 | } | 230 | } |
| 213 | 231 | ||
| 214 | GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o) | 232 | 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) | |||
| 242 | break; | 260 | break; |
| 243 | case 'd': { | 261 | case 'd': { |
| 244 | char buf[LJ_STR_INTBUF]; | 262 | char buf[LJ_STR_INTBUF]; |
| 245 | char *p = lj_str_bufint(buf, va_arg(argp, int32_t)); | 263 | char *p = str_bufint(buf, va_arg(argp, int32_t)); |
| 246 | lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); | 264 | lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); |
| 247 | break; | 265 | break; |
| 248 | } | 266 | } |
diff --git a/src/lj_str.h b/src/lj_str.h index 3dcd1a9b..5d91203b 100644 --- a/src/lj_str.h +++ b/src/lj_str.h | |||
| @@ -20,14 +20,15 @@ LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s); | |||
| 20 | #define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1)) | 20 | #define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1)) |
| 21 | 21 | ||
| 22 | /* Type conversions. */ | 22 | /* Type conversions. */ |
| 23 | LJ_FUNC size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o); | 23 | LJ_FUNC MSize LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o); |
| 24 | LJ_FUNC char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k); | 24 | LJ_FUNC const char *lj_str_buftv(char *buf, cTValue *o, MSize *lenp); |
| 25 | LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np); | 25 | LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np); |
| 26 | LJ_FUNC GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k); | 26 | LJ_FUNC GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k); |
| 27 | LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o); | 27 | LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o); |
| 28 | 28 | ||
| 29 | #define LJ_STR_INTBUF (1+10) | 29 | #define LJ_STR_INTBUF (1+10) |
| 30 | #define LJ_STR_NUMBUF LUAI_MAXNUMBER2STR | 30 | #define LJ_STR_NUMBUF LUAI_MAXNUMBER2STR |
| 31 | #define LJ_STR_NUMBERBUF LUAI_MAXNUMBER2STR | ||
| 31 | 32 | ||
| 32 | /* String formatting. */ | 33 | /* String formatting. */ |
| 33 | LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp); | 34 | LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp); |
