diff options
Diffstat (limited to 'src/lj_str.c')
-rw-r--r-- | src/lj_str.c | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/lj_str.c b/src/lj_str.c index 84bab864..67353154 100644 --- a/src/lj_str.c +++ b/src/lj_str.c | |||
@@ -222,27 +222,28 @@ GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o) | |||
222 | const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) | 222 | const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) |
223 | { | 223 | { |
224 | SBuf *sb = &G(L)->tmpbuf; | 224 | SBuf *sb = &G(L)->tmpbuf; |
225 | lj_buf_need(L, sb, (MSize)strlen(fmt)); | 225 | setsbufL(sb, L); |
226 | lj_buf_need(sb, (MSize)strlen(fmt)); | ||
226 | lj_buf_reset(sb); | 227 | lj_buf_reset(sb); |
227 | for (;;) { | 228 | for (;;) { |
228 | const char *e = strchr(fmt, '%'); | 229 | const char *e = strchr(fmt, '%'); |
229 | if (e == NULL) break; | 230 | if (e == NULL) break; |
230 | lj_buf_putmem(L, sb, fmt, (MSize)(e-fmt)); | 231 | lj_buf_putmem(sb, fmt, (MSize)(e-fmt)); |
231 | /* This function only handles %s, %c, %d, %f and %p formats. */ | 232 | /* This function only handles %s, %c, %d, %f and %p formats. */ |
232 | switch (e[1]) { | 233 | switch (e[1]) { |
233 | case 's': { | 234 | case 's': { |
234 | const char *s = va_arg(argp, char *); | 235 | const char *s = va_arg(argp, char *); |
235 | if (s == NULL) s = "(null)"; | 236 | if (s == NULL) s = "(null)"; |
236 | lj_buf_putmem(L, sb, s, (MSize)strlen(s)); | 237 | lj_buf_putmem(sb, s, (MSize)strlen(s)); |
237 | break; | 238 | break; |
238 | } | 239 | } |
239 | case 'c': | 240 | case 'c': |
240 | lj_buf_putb(L, sb, va_arg(argp, int)); | 241 | lj_buf_putb(sb, va_arg(argp, int)); |
241 | break; | 242 | break; |
242 | case 'd': { | 243 | case 'd': { |
243 | char buf[LJ_STR_INTBUF]; | 244 | char buf[LJ_STR_INTBUF]; |
244 | char *p = lj_str_bufint(buf, va_arg(argp, int32_t)); | 245 | char *p = lj_str_bufint(buf, va_arg(argp, int32_t)); |
245 | lj_buf_putmem(L, sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); | 246 | lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); |
246 | break; | 247 | break; |
247 | } | 248 | } |
248 | case 'f': { | 249 | case 'f': { |
@@ -251,7 +252,7 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) | |||
251 | MSize len; | 252 | MSize len; |
252 | tv.n = (lua_Number)(va_arg(argp, LUAI_UACNUMBER)); | 253 | tv.n = (lua_Number)(va_arg(argp, LUAI_UACNUMBER)); |
253 | len = (MSize)lj_str_bufnum(buf, &tv); | 254 | len = (MSize)lj_str_bufnum(buf, &tv); |
254 | lj_buf_putmem(L, sb, buf, len); | 255 | lj_buf_putmem(sb, buf, len); |
255 | break; | 256 | break; |
256 | } | 257 | } |
257 | case 'p': { | 258 | case 'p': { |
@@ -260,7 +261,7 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) | |||
260 | ptrdiff_t p = (ptrdiff_t)(va_arg(argp, void *)); | 261 | ptrdiff_t p = (ptrdiff_t)(va_arg(argp, void *)); |
261 | ptrdiff_t i, lasti = 2+FMTP_CHARS; | 262 | ptrdiff_t i, lasti = 2+FMTP_CHARS; |
262 | if (p == 0) { | 263 | if (p == 0) { |
263 | lj_buf_putmem(L, sb, "NULL", 4); | 264 | lj_buf_putmem(sb, "NULL", 4); |
264 | break; | 265 | break; |
265 | } | 266 | } |
266 | #if LJ_64 | 267 | #if LJ_64 |
@@ -271,20 +272,20 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) | |||
271 | buf[1] = 'x'; | 272 | buf[1] = 'x'; |
272 | for (i = lasti-1; i >= 2; i--, p >>= 4) | 273 | for (i = lasti-1; i >= 2; i--, p >>= 4) |
273 | buf[i] = "0123456789abcdef"[(p & 15)]; | 274 | buf[i] = "0123456789abcdef"[(p & 15)]; |
274 | lj_buf_putmem(L, sb, buf, (MSize)lasti); | 275 | lj_buf_putmem(sb, buf, (MSize)lasti); |
275 | break; | 276 | break; |
276 | } | 277 | } |
277 | case '%': | 278 | case '%': |
278 | lj_buf_putb(L, sb, '%'); | 279 | lj_buf_putb(sb, '%'); |
279 | break; | 280 | break; |
280 | default: | 281 | default: |
281 | lj_buf_putb(L, sb, '%'); | 282 | lj_buf_putb(sb, '%'); |
282 | lj_buf_putb(L, sb, e[1]); | 283 | lj_buf_putb(sb, e[1]); |
283 | break; | 284 | break; |
284 | } | 285 | } |
285 | fmt = e+2; | 286 | fmt = e+2; |
286 | } | 287 | } |
287 | lj_buf_putmem(L, sb, fmt, (MSize)strlen(fmt)); | 288 | lj_buf_putmem(sb, fmt, (MSize)strlen(fmt)); |
288 | setstrV(L, L->top, lj_buf_str(L, sb)); | 289 | setstrV(L, L->top, lj_buf_str(L, sb)); |
289 | incr_top(L); | 290 | incr_top(L); |
290 | return strVdata(L->top - 1); | 291 | return strVdata(L->top - 1); |