lua-users home
lua-l archive

luaL_error doesn't support hex?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I was surprised with all the new hex stuff in 5.1 that luaL_error still doesn't support "%X" hex numbers. Especially when the fix is so simple. ???

I just added it in lobject.c luaO_pushvfstring(), in the switch:

case 'X': {
char buff[sizeof(int)*2+3];
sprintf(buff, "%X", va_arg(argp, int));
pushstr(L, buff);
break;
}
case 'x': {
char buff[sizeof(int)*2+3];
sprintf(buff, "%x", va_arg(argp, int));
pushstr(L, buff);
break;
}

Similar to the method used with "%p". Any chance of this making it into the official tree? I hate maintaining tiny changes like this and would rather use a vanilla Lua source base if possible.

However, that code could be rearranged to use vsprintf to get the full printf semantics (you're already using sprintf anyway). I mean, you're calling it from C code, it should support the whole syntax. The code using that method would probably be even smaller than it currently is. I mean the entire lua0_pushvfstring function would look something like:

const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
char *buf;
buf = malloc(size_of_format(fmt));
vsprintf(buf, fmt, argp);
pushstr(L, buf);
free(buf);
return svalue(L->top - 1);
}

Where size_of_format() could be taken from someones vasprintf implementation.

--
// Chris

AltStyle によって変換されたページ (->オリジナル) /