aboutsummaryrefslogtreecommitdiff
path: root/src/lj_str.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lj_str.c')
-rw-r--r--src/lj_str.c67
1 files changed, 16 insertions, 51 deletions
diff --git a/src/lj_str.c b/src/lj_str.c
index 6548ee4d..67353154 100644
--- a/src/lj_str.c
+++ b/src/lj_str.c
@@ -1,9 +1,6 @@
1/* 1/*
2** String handling. 2** String handling.
3** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h 3** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
4**
5** Portions taken verbatim or adapted from the Lua interpreter.
6** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7*/ 4*/
8 5
9#include <stdio.h> 6#include <stdio.h>
@@ -14,6 +11,7 @@
14#include "lj_obj.h" 11#include "lj_obj.h"
15#include "lj_gc.h" 12#include "lj_gc.h"
16#include "lj_err.h" 13#include "lj_err.h"
14#include "lj_buf.h"
17#include "lj_str.h" 15#include "lj_str.h"
18#include "lj_state.h" 16#include "lj_state.h"
19#include "lj_char.h" 17#include "lj_char.h"
@@ -220,54 +218,32 @@ GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o)
220 218
221/* -- String formatting --------------------------------------------------- */ 219/* -- String formatting --------------------------------------------------- */
222 220
223static void addstr(lua_State *L, SBuf *sb, const char *str, MSize len)
224{
225 char *p;
226 MSize i;
227 if (sb->n + len > sb->sz) {
228 MSize sz = sb->sz * 2;
229 while (sb->n + len > sz) sz = sz * 2;
230 lj_str_resizebuf(L, sb, sz);
231 }
232 p = sb->buf + sb->n;
233 sb->n += len;
234 for (i = 0; i < len; i++) p[i] = str[i];
235}
236
237static void addchar(lua_State *L, SBuf *sb, int c)
238{
239 if (sb->n + 1 > sb->sz) {
240 MSize sz = sb->sz * 2;
241 lj_str_resizebuf(L, sb, sz);
242 }
243 sb->buf[sb->n++] = (char)c;
244}
245
246/* Push formatted message as a string object to Lua stack. va_list variant. */ 221/* Push formatted message as a string object to Lua stack. va_list variant. */
247const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) 222const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
248{ 223{
249 SBuf *sb = &G(L)->tmpbuf; 224 SBuf *sb = &G(L)->tmpbuf;
250 lj_str_needbuf(L, sb, (MSize)strlen(fmt)); 225 setsbufL(sb, L);
251 lj_str_resetbuf(sb); 226 lj_buf_need(sb, (MSize)strlen(fmt));
227 lj_buf_reset(sb);
252 for (;;) { 228 for (;;) {
253 const char *e = strchr(fmt, '%'); 229 const char *e = strchr(fmt, '%');
254 if (e == NULL) break; 230 if (e == NULL) break;
255 addstr(L, sb, fmt, (MSize)(e-fmt)); 231 lj_buf_putmem(sb, fmt, (MSize)(e-fmt));
256 /* This function only handles %s, %c, %d, %f and %p formats. */ 232 /* This function only handles %s, %c, %d, %f and %p formats. */
257 switch (e[1]) { 233 switch (e[1]) {
258 case 's': { 234 case 's': {
259 const char *s = va_arg(argp, char *); 235 const char *s = va_arg(argp, char *);
260 if (s == NULL) s = "(null)"; 236 if (s == NULL) s = "(null)";
261 addstr(L, sb, s, (MSize)strlen(s)); 237 lj_buf_putmem(sb, s, (MSize)strlen(s));
262 break; 238 break;
263 } 239 }
264 case 'c': 240 case 'c':
265 addchar(L, sb, va_arg(argp, int)); 241 lj_buf_putb(sb, va_arg(argp, int));
266 break; 242 break;
267 case 'd': { 243 case 'd': {
268 char buf[LJ_STR_INTBUF]; 244 char buf[LJ_STR_INTBUF];
269 char *p = lj_str_bufint(buf, va_arg(argp, int32_t)); 245 char *p = lj_str_bufint(buf, va_arg(argp, int32_t));
270 addstr(L, sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); 246 lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p));
271 break; 247 break;
272 } 248 }
273 case 'f': { 249 case 'f': {
@@ -276,7 +252,7 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
276 MSize len; 252 MSize len;
277 tv.n = (lua_Number)(va_arg(argp, LUAI_UACNUMBER)); 253 tv.n = (lua_Number)(va_arg(argp, LUAI_UACNUMBER));
278 len = (MSize)lj_str_bufnum(buf, &tv); 254 len = (MSize)lj_str_bufnum(buf, &tv);
279 addstr(L, sb, buf, len); 255 lj_buf_putmem(sb, buf, len);
280 break; 256 break;
281 } 257 }
282 case 'p': { 258 case 'p': {
@@ -285,7 +261,7 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
285 ptrdiff_t p = (ptrdiff_t)(va_arg(argp, void *)); 261 ptrdiff_t p = (ptrdiff_t)(va_arg(argp, void *));
286 ptrdiff_t i, lasti = 2+FMTP_CHARS; 262 ptrdiff_t i, lasti = 2+FMTP_CHARS;
287 if (p == 0) { 263 if (p == 0) {
288 addstr(L, sb, "NULL", 4); 264 lj_buf_putmem(sb, "NULL", 4);
289 break; 265 break;
290 } 266 }
291#if LJ_64 267#if LJ_64
@@ -296,21 +272,21 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
296 buf[1] = 'x'; 272 buf[1] = 'x';
297 for (i = lasti-1; i >= 2; i--, p >>= 4) 273 for (i = lasti-1; i >= 2; i--, p >>= 4)
298 buf[i] = "0123456789abcdef"[(p & 15)]; 274 buf[i] = "0123456789abcdef"[(p & 15)];
299 addstr(L, sb, buf, (MSize)lasti); 275 lj_buf_putmem(sb, buf, (MSize)lasti);
300 break; 276 break;
301 } 277 }
302 case '%': 278 case '%':
303 addchar(L, sb, '%'); 279 lj_buf_putb(sb, '%');
304 break; 280 break;
305 default: 281 default:
306 addchar(L, sb, '%'); 282 lj_buf_putb(sb, '%');
307 addchar(L, sb, e[1]); 283 lj_buf_putb(sb, e[1]);
308 break; 284 break;
309 } 285 }
310 fmt = e+2; 286 fmt = e+2;
311 } 287 }
312 addstr(L, sb, fmt, (MSize)strlen(fmt)); 288 lj_buf_putmem(sb, fmt, (MSize)strlen(fmt));
313 setstrV(L, L->top, lj_str_new(L, sb->buf, sb->n)); 289 setstrV(L, L->top, lj_buf_str(L, sb));
314 incr_top(L); 290 incr_top(L);
315 return strVdata(L->top - 1); 291 return strVdata(L->top - 1);
316} 292}
@@ -326,14 +302,3 @@ const char *lj_str_pushf(lua_State *L, const char *fmt, ...)
326 return msg; 302 return msg;
327} 303}
328 304
329/* -- Buffer handling ----------------------------------------------------- */
330
331char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz)
332{
333 if (sz > sb->sz) {
334 if (sz < LJ_MIN_SBUF) sz = LJ_MIN_SBUF;
335 lj_str_resizebuf(L, sb, sz);
336 }
337 return sb->buf;
338}
339