Browse Source

`lua_pushfstring' now supports `%p' option too

v5-2
Roberto Ierusalimschy 21 years ago
parent
commit
76d8b8db06
  1. 27
      lbaselib.c
  2. 12
      liolib.c
  3. 22
      lobject.c

27
lbaselib.c

@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.151 2004/07/01 14:26:28 roberto Exp roberto $
** $Id: lbaselib.c,v 1.152 2004/07/02 18:09:11 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@ -383,45 +383,36 @@ static int luaB_xpcall (lua_State *L) {
static int luaB_tostring (lua_State *L) {
char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */
const char *tn = "";
const void *p = NULL;
luaL_checkany(L, 1);
if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
return 1; /* use its value */
switch (lua_type(L, 1)) {
case LUA_TNUMBER:
lua_pushstring(L, lua_tostring(L, 1));
return 1;
break;
case LUA_TSTRING:
lua_pushvalue(L, 1);
return 1;
break;
case LUA_TBOOLEAN:
lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
return 1;
break;
case LUA_TNIL:
lua_pushliteral(L, "nil");
return 1;
break;
case LUA_TTABLE:
p = lua_topointer(L, 1);
tn = "table";
lua_pushfstring(L, "table: %p", lua_topointer(L, 1));
break;
case LUA_TFUNCTION:
p = lua_topointer(L, 1);
tn = "function";
lua_pushfstring(L, "function: %p", lua_topointer(L, 1));
break;
case LUA_TUSERDATA:
case LUA_TLIGHTUSERDATA:
p = lua_touserdata(L, 1);
tn = "userdata";
lua_pushfstring(L, "userdata: %p", lua_topointer(L, 1));
break;
case LUA_TTHREAD:
p = lua_tothread(L, 1);
tn = "thread";
lua_pushfstring(L, "thread: %p", lua_topointer(L, 1));
break;
}
sprintf(buff, "%p", p);
lua_pushfstring(L, "%s: %s", tn, buff);
return 1;
}

12
liolib.c

@ -1,5 +1,5 @@
/*
** $Id: liolib.c,v 2.53 2004/05/28 18:35:05 roberto Exp roberto $
** $Id: liolib.c,v 2.54 2004/07/09 15:47:48 roberto Exp roberto $
** Standard I/O (and system) library
** See Copyright Notice in lua.h
*/
@ -114,13 +114,11 @@ static int io_gc (lua_State *L) {
static int io_tostring (lua_State *L) {
char buff[4*sizeof(void *) + 8]; /* enough space for a `%p' */
FILE **f = topfile(L);
if (*f == NULL)
strcpy(buff, "closed");
FILE *f = *topfile(L);
if (f == NULL)
lua_pushstring(L, "file (closed)");
else
sprintf(buff, "%p", lua_touserdata(L, 1));
lua_pushfstring(L, "file (%s)", buff);
lua_pushfstring(L, "file (%p)", f);
return 1;
}

22
lobject.c

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.2 2004/04/30 20:13:38 roberto Exp roberto $
** $Id: lobject.c,v 2.3 2004/05/03 12:30:41 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -94,7 +94,7 @@ static void pushstr (lua_State *L, const char *str) {
}
/* this function handles only `%d', `%c', %f, and `%s' formats */
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
int n = 1;
pushstr(L, "");
@ -104,9 +104,10 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
incr_top(L);
switch (*(e+1)) {
case 's':
case 's': {
pushstr(L, va_arg(argp, char *));
break;
}
case 'c': {
char buff[2];
buff[0] = cast(char, va_arg(argp, int));
@ -114,17 +115,26 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
pushstr(L, buff);
break;
}
case 'd':
case 'd': {
setnvalue(L->top, cast(lua_Number, va_arg(argp, int)));
incr_top(L);
break;
case 'f':
}
case 'f': {
setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber)));
incr_top(L);
break;
case '%':
}
case 'p': {
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
sprintf(buff, "%p", va_arg(argp, void *));
pushstr(L, buff);
break;
}
case '%': {
pushstr(L, "%");
break;
}
default: {
char buff[3];
buff[0] = '%';

Loading…
Cancel
Save