Refactor internal string formatting.

This commit is contained in:
Mike Pall 2013-05-12 23:52:09 +02:00
parent 7d5acc2918
commit 19a9206caf
14 changed files with 149 additions and 126 deletions

View File

@ -16,11 +16,12 @@ lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h \
lj_ctype.h lj_cparse.h lj_cdata.h lj_cconv.h lj_carith.h lj_ccall.h \
lj_ccallback.h lj_clib.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
lj_ccallback.h lj_clib.h lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h \
lj_libdef.h
lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h
lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_state.h \
lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
lj_strfmt.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h
lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h \
lj_obj.h lj_def.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h \
lj_bc.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_target.h \
@ -44,7 +45,7 @@ lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h
lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \
lj_meta.h lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h \
lj_dispatch.h lj_traceerr.h lj_vm.h lj_strscan.h
lj_dispatch.h lj_traceerr.h lj_vm.h lj_strscan.h lj_strfmt.h
lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h \
lj_ircall.h lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h \
@ -54,7 +55,8 @@ lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \
lj_bcdef.h
lj_bcread.o: lj_bcread.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_bc.h \
lj_ctype.h lj_cdata.h lualib.h lj_lex.h lj_bcdump.h lj_state.h
lj_ctype.h lj_cdata.h lualib.h lj_lex.h lj_bcdump.h lj_state.h \
lj_strfmt.h
lj_bcwrite.o: lj_bcwrite.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_gc.h lj_buf.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h \
lj_ir.h lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h
@ -81,10 +83,10 @@ lj_cdata.o: lj_cdata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h
lj_clib.o: lj_clib.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
lj_err.h lj_errmsg.h lj_tab.h lj_str.h lj_udata.h lj_ctype.h lj_cconv.h \
lj_cdata.h lj_clib.h
lj_cdata.h lj_clib.h lj_strfmt.h
lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_ctype.h lj_cparse.h \
lj_frame.h lj_bc.h lj_vm.h lj_char.h lj_strscan.h
lj_frame.h lj_bc.h lj_vm.h lj_char.h lj_strscan.h lj_strfmt.h
lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h \
lj_gc.h lj_cdata.h lj_cparse.h lj_cconv.h lj_clib.h lj_ccall.h lj_ff.h \
@ -95,7 +97,7 @@ lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_ccallback.h
lj_debug.o: lj_debug.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_err.h lj_errmsg.h lj_debug.h lj_buf.h lj_gc.h lj_str.h lj_tab.h \
lj_state.h lj_frame.h lj_bc.h lj_jit.h lj_ir.h
lj_state.h lj_frame.h lj_bc.h lj_strfmt.h lj_jit.h lj_ir.h
lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_err.h lj_errmsg.h lj_buf.h lj_gc.h lj_str.h lj_func.h lj_tab.h \
lj_meta.h lj_debug.h lj_state.h lj_frame.h lj_bc.h lj_ff.h lj_ffdef.h \
@ -104,7 +106,7 @@ lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_err.o: lj_err.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_err.h \
lj_errmsg.h lj_debug.h lj_str.h lj_func.h lj_state.h lj_frame.h lj_bc.h \
lj_ff.h lj_ffdef.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \
lj_traceerr.h lj_vm.h
lj_traceerr.h lj_vm.h lj_strfmt.h
lj_ffrecord.o: lj_ffrecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ff.h \
lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \
@ -126,7 +128,8 @@ lj_ir.o: lj_ir.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
lj_carith.h lj_vm.h lj_strscan.h lj_lib.h
lj_lex.o: lj_lex.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h lj_ctype.h lj_cdata.h \
lualib.h lj_state.h lj_lex.h lj_parse.h lj_char.h lj_strscan.h
lualib.h lj_state.h lj_lex.h lj_parse.h lj_char.h lj_strscan.h \
lj_strfmt.h
lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \
lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \
lj_dispatch.h lj_jit.h lj_ir.h lj_vm.h lj_strscan.h lj_lex.h lj_bcdump.h \
@ -179,9 +182,9 @@ lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_trace.h lj_jit.h \
lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_alloc.h
lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_state.h lj_char.h
lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_char.h
lj_strfmt.o: lj_strfmt.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_buf.h lj_gc.h lj_str.h lj_char.h lj_strfmt.h
lj_buf.h lj_gc.h lj_str.h lj_state.h lj_char.h lj_strfmt.h
lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
lj_char.h lj_strscan.h
lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
@ -202,11 +205,11 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h \
lj_func.h lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h \
lj_cdata.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_traceerr.h \
lj_vm.h lj_err.c lj_debug.h lj_ff.h lj_ffdef.h lj_char.c lj_char.h \
lj_bc.c lj_bcdef.h lj_obj.c lj_buf.c lj_str.c lj_tab.c lj_func.c \
lj_udata.c lj_meta.c lj_strscan.h lj_lib.h lj_debug.c lj_state.c \
lj_lex.h lj_alloc.h lj_dispatch.c lj_ccallback.h luajit.h lj_vmevent.c \
lj_vmevent.h lj_vmmath.c lj_strscan.c lj_strfmt.c lj_strfmt.h lj_api.c \
lj_vm.h lj_err.c lj_debug.h lj_ff.h lj_ffdef.h lj_strfmt.h lj_char.c \
lj_char.h lj_bc.c lj_bcdef.h lj_obj.c lj_buf.c lj_str.c lj_tab.c \
lj_func.c lj_udata.c lj_meta.c lj_strscan.h lj_lib.h lj_debug.c \
lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c lj_ccallback.h luajit.h \
lj_vmevent.c lj_vmevent.h lj_vmmath.c lj_strscan.c lj_strfmt.c lj_api.c \
lj_lex.c lualib.h lj_parse.h lj_parse.c lj_bcread.c lj_bcdump.h \
lj_bcwrite.c lj_load.c lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c \
lj_ccall.c lj_ccall.h lj_ccallback.c lj_target.h lj_target_*.h \

View File

@ -29,6 +29,7 @@
#include "lj_ccall.h"
#include "lj_ccallback.h"
#include "lj_clib.h"
#include "lj_strfmt.h"
#include "lj_ff.h"
#include "lj_lib.h"
@ -317,7 +318,7 @@ LJLIB_CF(ffi_meta___tostring)
}
}
}
lj_str_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
lj_strfmt_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p);
checkgc:
lj_gc_check(L);
return 1;

View File

@ -22,6 +22,7 @@
#include "lj_buf.h"
#include "lj_str.h"
#include "lj_state.h"
#include "lj_strfmt.h"
#include "lj_ff.h"
#include "lj_lib.h"
@ -85,7 +86,7 @@ static IOFileUD *io_file_open(lua_State *L, const char *mode)
IOFileUD *iof = io_file_new(L);
iof->fp = fopen(fname, mode);
if (iof->fp == NULL)
luaL_argerror(L, 1, lj_str_pushf(L, "%s: %s", fname, strerror(errno)));
luaL_argerror(L, 1, lj_strfmt_pushf(L, "%s: %s", fname, strerror(errno)));
return iof;
}

View File

@ -24,6 +24,7 @@
#include "lj_trace.h"
#include "lj_vm.h"
#include "lj_strscan.h"
#include "lj_strfmt.h"
/* -- Common helper functions --------------------------------------------- */
@ -606,7 +607,7 @@ LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt,
va_list argp)
{
lj_gc_check(L);
return lj_str_pushvf(L, fmt, argp);
return lj_strfmt_pushvf(L, fmt, argp);
}
LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
@ -615,7 +616,7 @@ LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...)
va_list argp;
lj_gc_check(L);
va_start(argp, fmt);
ret = lj_str_pushvf(L, fmt, argp);
ret = lj_strfmt_pushvf(L, fmt, argp);
va_end(argp);
return ret;
}

View File

@ -21,6 +21,7 @@
#include "lj_lex.h"
#include "lj_bcdump.h"
#include "lj_state.h"
#include "lj_strfmt.h"
/* Reuse some lexer fields for our own purposes. */
#define bcread_flags(ls) ls->level
@ -39,7 +40,7 @@ static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em)
const char *name = ls->chunkarg;
if (*name == BCDUMP_HEAD1) name = "(binary)";
else if (*name == '@' || *name == '=') name++;
lj_str_pushf(L, "%s: %s", name, err2msg(em));
lj_strfmt_pushf(L, "%s: %s", name, err2msg(em));
lj_err_throw(L, LUA_ERRSYNTAX);
}

View File

@ -16,6 +16,7 @@
#include "lj_cconv.h"
#include "lj_cdata.h"
#include "lj_clib.h"
#include "lj_strfmt.h"
/* -- OS-specific functions ----------------------------------------------- */
@ -61,7 +62,7 @@ static const char *clib_extname(lua_State *L, const char *name)
#endif
) {
if (!strchr(name, '.')) {
name = lj_str_pushf(L, CLIB_SOEXT, name);
name = lj_strfmt_pushf(L, CLIB_SOEXT, name);
L->top--;
#ifdef __CYGWIN__
} else {
@ -70,7 +71,7 @@ static const char *clib_extname(lua_State *L, const char *name)
}
if (!(name[0] == CLIB_SOPREFIX[0] && name[1] == CLIB_SOPREFIX[1] &&
name[2] == CLIB_SOPREFIX[2])) {
name = lj_str_pushf(L, CLIB_SOPREFIX "%s", name);
name = lj_strfmt_pushf(L, CLIB_SOPREFIX "%s", name);
L->top--;
}
}
@ -178,7 +179,7 @@ LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,
if (!FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err, 0, buf, sizeof(buf), NULL))
buf[0] = '\0';
lj_err_callermsg(L, lj_str_pushf(L, fmt, name, buf));
lj_err_callermsg(L, lj_strfmt_pushf(L, fmt, name, buf));
}
static int clib_needext(const char *s)
@ -193,7 +194,7 @@ static int clib_needext(const char *s)
static const char *clib_extname(lua_State *L, const char *name)
{
if (clib_needext(name)) {
name = lj_str_pushf(L, "%s.dll", name);
name = lj_strfmt_pushf(L, "%s.dll", name);
L->top--;
}
return name;
@ -266,7 +267,7 @@ static void *clib_getsym(CLibrary *cl, const char *name)
LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt,
const char *name)
{
lj_err_callermsg(L, lj_str_pushf(L, fmt, name, "no support for this OS"));
lj_err_callermsg(L, lj_strfmt_pushf(L, fmt, name, "no support for this OS"));
}
static void *clib_loadlib(lua_State *L, const char *name, int global)
@ -350,7 +351,7 @@ TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name)
CTInfo cconv = ctype_cconv(ct->info);
if (cconv == CTCC_FASTCALL || cconv == CTCC_STDCALL) {
CTSize sz = clib_func_argsize(cts, ct);
const char *symd = lj_str_pushf(L,
const char *symd = lj_strfmt_pushf(L,
cconv == CTCC_FASTCALL ? "@%s@%d" : "_%s@%d",
sym, sz);
L->top--;

View File

@ -17,6 +17,7 @@
#include "lj_vm.h"
#include "lj_char.h"
#include "lj_strscan.h"
#include "lj_strfmt.h"
/*
** Important note: this is NOT a validating C parser! This is a minimal
@ -47,9 +48,9 @@ static const char *cp_tok2str(CPState *cp, CPToken tok)
if (tok > CTOK_OFS)
return ctoknames[tok-CTOK_OFS-1];
else if (!lj_char_iscntrl(tok))
return lj_str_pushf(cp->L, "%c", tok);
return lj_strfmt_pushf(cp->L, "%c", tok);
else
return lj_str_pushf(cp->L, "char(%d)", tok);
return lj_strfmt_pushf(cp->L, "char(%d)", tok);
}
/* End-of-line? */
@ -117,12 +118,12 @@ LJ_NORET static void cp_errmsg(CPState *cp, CPToken tok, ErrMsg em, ...)
}
L = cp->L;
va_start(argp, em);
msg = lj_str_pushvf(L, err2msg(em), argp);
msg = lj_strfmt_pushvf(L, err2msg(em), argp);
va_end(argp);
if (tokstr)
msg = lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tokstr);
msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tokstr);
if (cp->linenumber > 1)
msg = lj_str_pushf(L, "%s at line %d", msg, cp->linenumber);
msg = lj_strfmt_pushf(L, "%s at line %d", msg, cp->linenumber);
lj_err_callermsg(L, msg);
}
@ -998,7 +999,7 @@ static void cp_decl_asm(CPState *cp, CPDecl *decl)
if (cp->tok == CTOK_STRING) {
GCstr *str = cp->str;
while (cp_next(cp) == CTOK_STRING) {
lj_str_pushf(cp->L, "%s%s", strdata(str), strdata(cp->str));
lj_strfmt_pushf(cp->L, "%s%s", strdata(str), strdata(cp->str));
cp->L->top--;
str = strV(cp->L->top);
}

View File

@ -15,6 +15,7 @@
#include "lj_state.h"
#include "lj_frame.h"
#include "lj_bc.h"
#include "lj_strfmt.h"
#if LJ_HASJIT
#include "lj_jit.h"
#endif
@ -350,12 +351,12 @@ void lj_debug_addloc(lua_State *L, const char *msg,
GCproto *pt = funcproto(fn);
char buf[LUA_IDSIZE];
lj_debug_shortname(buf, proto_chunkname(pt), pt->firstline);
lj_str_pushf(L, "%s:%d: %s", buf, line, msg);
lj_strfmt_pushf(L, "%s:%d: %s", buf, line, msg);
return;
}
}
}
lj_str_pushf(L, "%s", msg);
lj_strfmt_pushf(L, "%s", msg);
}
/* Push location string for a bytecode position to Lua stack. */
@ -366,7 +367,7 @@ void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc)
MSize i, len = name->len;
BCLine line = lj_debug_line(pt, pc);
if (pt->firstline == ~(BCLine)0) {
lj_str_pushf(L, "builtin:%s", s);
lj_strfmt_pushf(L, "builtin:%s", s);
} else if (*s == '@') {
s++; len--;
for (i = len; i > 0; i--)
@ -374,13 +375,13 @@ void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc)
s += i+1;
break;
}
lj_str_pushf(L, "%s:%d", s, line);
lj_strfmt_pushf(L, "%s:%d", s, line);
} else if (len > 40) {
lj_str_pushf(L, "%p:%d", pt, line);
lj_strfmt_pushf(L, "%p:%d", pt, line);
} else if (*s == '=') {
lj_str_pushf(L, "%s:%d", s+1, line);
lj_strfmt_pushf(L, "%s:%d", s+1, line);
} else {
lj_str_pushf(L, "\"%s\":%d", s, line);
lj_strfmt_pushf(L, "\"%s\":%d", s, line);
}
}

View File

@ -16,6 +16,7 @@
#include "lj_ff.h"
#include "lj_trace.h"
#include "lj_vm.h"
#include "lj_strfmt.h"
/*
** LuaJIT can either use internal or external frame unwinding:
@ -569,7 +570,7 @@ LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
va_list argp;
va_start(argp, em);
if (curr_funcisL(L)) L->top = curr_topL(L);
msg = lj_str_pushvf(L, err2msg(em), argp);
msg = lj_strfmt_pushvf(L, err2msg(em), argp);
va_end(argp);
lj_debug_addloc(L, msg, L->base-1, NULL);
lj_err_run(L);
@ -588,10 +589,10 @@ LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,
char buff[LUA_IDSIZE];
const char *msg;
lj_debug_shortname(buff, src, line);
msg = lj_str_pushvf(L, err2msg(em), argp);
msg = lj_str_pushf(L, "%s:%d: %s", buff, line, msg);
msg = lj_strfmt_pushvf(L, err2msg(em), argp);
msg = lj_strfmt_pushf(L, "%s:%d: %s", buff, line, msg);
if (tok)
lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
lj_err_throw(L, LUA_ERRSYNTAX);
}
@ -675,7 +676,7 @@ LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
const char *msg;
va_list argp;
va_start(argp, em);
msg = lj_str_pushvf(L, err2msg(em), argp);
msg = lj_strfmt_pushvf(L, err2msg(em), argp);
va_end(argp);
lj_err_callermsg(L, msg);
}
@ -695,9 +696,9 @@ LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
if (narg < 0 && narg > LUA_REGISTRYINDEX)
narg = (int)(L->top - L->base) + narg + 1;
if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */
msg = lj_str_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
else
msg = lj_str_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
lj_err_callermsg(L, msg);
}
@ -707,7 +708,7 @@ LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
const char *msg;
va_list argp;
va_start(argp, em);
msg = lj_str_pushvf(L, err2msg(em), argp);
msg = lj_strfmt_pushvf(L, err2msg(em), argp);
va_end(argp);
err_argmsg(L, narg, msg);
}
@ -723,7 +724,7 @@ LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
{
TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;
const char *tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];
const char *msg = lj_str_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
const char *msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
err_argmsg(L, narg, msg);
}
@ -773,7 +774,7 @@ LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
const char *msg;
va_list argp;
va_start(argp, fmt);
msg = lj_str_pushvf(L, fmt, argp);
msg = lj_strfmt_pushvf(L, fmt, argp);
va_end(argp);
lj_err_callermsg(L, msg);
return 0; /* unreachable */

View File

@ -25,6 +25,7 @@
#include "lj_parse.h"
#include "lj_char.h"
#include "lj_strscan.h"
#include "lj_strfmt.h"
/* Lua lexer token names. */
static const char *const tokennames[] = {
@ -444,9 +445,9 @@ const char *lj_lex_token2str(LexState *ls, LexToken tok)
if (tok > TK_OFS)
return tokennames[tok-TK_OFS-1];
else if (!lj_char_iscntrl(tok))
return lj_str_pushf(ls->L, "%c", tok);
return lj_strfmt_pushf(ls->L, "%c", tok);
else
return lj_str_pushf(ls->L, "char(%d)", tok);
return lj_strfmt_pushf(ls->L, "char(%d)", tok);
}
/* Lexer error. */

View File

@ -13,7 +13,6 @@
#include "lj_err.h"
#include "lj_buf.h"
#include "lj_str.h"
#include "lj_state.h"
#include "lj_char.h"
/* -- String helpers ------------------------------------------------------ */
@ -316,68 +315,3 @@ GCstr * LJ_FASTCALL lj_str_fromchar(lua_State *L, int c)
return lj_str_new(L, buf, 1);
}
/* -- String formatting --------------------------------------------------- */
/* Push formatted message as a string object to Lua stack. va_list variant. */
const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
{
SBuf *sb = &G(L)->tmpbuf;
setsbufL(sb, L);
lj_buf_need(sb, (MSize)strlen(fmt));
lj_buf_reset(sb);
for (;;) {
const char *e = strchr(fmt, '%');
if (e == NULL) break;
lj_buf_putmem(sb, fmt, (MSize)(e-fmt));
/* This function only handles %s, %c, %d, %f and %p formats. */
switch (e[1]) {
case 's': {
const char *s = va_arg(argp, char *);
if (s == NULL) s = "(null)";
lj_buf_putmem(sb, s, (MSize)strlen(s));
break;
}
case 'c':
lj_buf_putb(sb, va_arg(argp, int));
break;
case 'd':
setsbufP(sb, lj_str_bufint(lj_buf_more(sb, LJ_STR_INTBUF),
va_arg(argp, int32_t)));
break;
case 'f': {
TValue tv;
tv.n = va_arg(argp, lua_Number);
setsbufP(sb, lj_str_bufnum(lj_buf_more(sb, LJ_STR_NUMBUF), &tv));
break;
}
case 'p':
setsbufP(sb, lj_str_bufptr(lj_buf_more(sb, LJ_STR_PTRBUF),
va_arg(argp, void *)));
break;
case '%':
lj_buf_putb(sb, '%');
break;
default:
lj_buf_putb(sb, '%');
lj_buf_putb(sb, e[1]);
break;
}
fmt = e+2;
}
lj_buf_putmem(sb, fmt, (MSize)strlen(fmt));
setstrV(L, L->top, lj_buf_str(L, sb));
incr_top(L);
return strVdata(L->top - 1);
}
/* Push formatted message as a string object to Lua stack. Vararg variant. */
const char *lj_str_pushf(lua_State *L, const char *fmt, ...)
{
const char *msg;
va_list argp;
va_start(argp, fmt);
msg = lj_str_pushvf(L, fmt, argp);
va_end(argp);
return msg;
}

View File

@ -39,12 +39,4 @@ LJ_FUNC GCstr * LJ_FASTCALL lj_str_fromchar(lua_State *L, int c);
#define LJ_STR_NUMBERBUF LUAI_MAXNUMBER2STR
#define LJ_STR_PTRBUF (2*sizeof(ptrdiff_t)+2)
/* String formatting. */
LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp);
LJ_FUNC const char *lj_str_pushf(lua_State *L, const char *fmt, ...)
#if defined(__GNUC__)
__attribute__ ((format (printf, 2, 3)))
#endif
;
#endif

View File

@ -10,6 +10,7 @@
#include "lj_obj.h"
#include "lj_buf.h"
#include "lj_state.h"
#include "lj_char.h"
#include "lj_strfmt.h"
@ -293,3 +294,78 @@ SBuf *lj_strfmt_putnum(SBuf *sb, SFormat sf, lua_Number n)
return sb;
}
/* -- Internal string formatting ------------------------------------------ */
/*
** These functions are only used for lua_pushfstring(), lua_pushvfstring()
** and for internal string formatting (e.g. error messages). Caveat: unlike
** string.format(), only a limited subset of formats and flags are supported!
**
** LuaJIT has support for a couple more formats than Lua 5.1/5.2:
** - %d %u %o %x with full formatting, 32 bit integers only.
** - %f and other FP formats are really %.14g.
** - %s %c %p without formatting.
*/
/* Push formatted message as a string object to Lua stack. va_list variant. */
const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, va_list argp)
{
SBuf *sb = lj_buf_tmp_(L);
FormatState fs;
SFormat sf;
GCstr *str;
lj_strfmt_init(&fs, fmt, strlen(fmt));
while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {
switch (STRFMT_TYPE(sf)) {
case STRFMT_LIT:
lj_buf_putmem(sb, fs.str, fs.len);
break;
case STRFMT_INT:
lj_strfmt_putxint(sb, sf, va_arg(argp, int32_t));
break;
case STRFMT_UINT:
lj_strfmt_putxint(sb, sf, va_arg(argp, uint32_t));
break;
case STRFMT_NUM: {
TValue tv;
tv.n = va_arg(argp, lua_Number);
setsbufP(sb, lj_str_bufnum(lj_buf_more(sb, LJ_STR_NUMBUF), &tv));
break;
}
case STRFMT_STR: {
const char *s = va_arg(argp, char *);
if (s == NULL) s = "(null)";
lj_buf_putmem(sb, s, (MSize)strlen(s));
break;
}
case STRFMT_CHAR:
lj_buf_putb(sb, va_arg(argp, int));
break;
case STRFMT_PTR:
setsbufP(sb, lj_str_bufptr(lj_buf_more(sb, LJ_STR_PTRBUF),
va_arg(argp, void *)));
break;
case STRFMT_ERR:
default:
lj_buf_putb(sb, '?');
lua_assert(0);
break;
}
}
str = lj_buf_str(L, sb);
setstrV(L, L->top, str);
incr_top(L);
return strdata(str);
}
/* Push formatted message as a string object to Lua stack. Vararg variant. */
const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...)
{
const char *msg;
va_list argp;
va_start(argp, fmt);
msg = lj_strfmt_pushvf(L, fmt, argp);
va_end(argp);
return msg;
}

View File

@ -73,6 +73,7 @@ static LJ_AINLINE void lj_strfmt_init(FormatState *fs, const char *p, MSize len)
}
LJ_FUNC SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs);
LJ_FUNC SBuf *lj_strfmt_putchar(SBuf *sb, SFormat, int32_t c);
LJ_FUNC SBuf *lj_strfmt_putstr(SBuf *sb, SFormat, GCstr *str);
LJ_FUNC SBuf *lj_strfmt_putquoted(SBuf *sb, GCstr *str);
@ -81,4 +82,12 @@ LJ_FUNC SBuf *lj_strfmt_putnum_int(SBuf *sb, SFormat sf, lua_Number n);
LJ_FUNC SBuf *lj_strfmt_putnum_uint(SBuf *sb, SFormat sf, lua_Number n);
LJ_FUNC SBuf *lj_strfmt_putnum(SBuf *sb, SFormat, lua_Number n);
LJ_FUNC const char *lj_strfmt_pushvf(lua_State *L, const char *fmt,
va_list argp);
LJ_FUNC const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...)
#ifdef __GNUC__
__attribute__ ((format (printf, 2, 3)))
#endif
;
#endif