private static int str_format(lua_State L) { int top = lua_gettop(L); int arg = 1; uint sfl; CharPtr strfrmt = luaL_checklstring(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { luaL_addchar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { luaL_addchar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else /* format item */ { strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = luaL_prepbuffsize(b, MAX_ITEM); /* to put formatted item */ int nb = 0; /* number of bytes in added item */ if (++arg > top) { luaL_argerror(L, arg, "no value"); } strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; //FIXME:added, move here strfrmt = strfrmt.next(); //FIXME:added, move here switch (ch) { case 'c': { nb = sprintf(buff, form, luaL_checkint(L, arg)); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { lua_Number n = luaL_checknumber(L, arg); LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : (LUA_INTFRM_T)(UInt64)n; //FIXME: changed here addlenmod(form, LUA_INTFRMLEN); nb = sprintf(buff, form, r); break; } case 'e': case 'E': case 'f': //#if defined(LUA_USE_AFORMAT) case 'a': case 'A': //#endif case 'g': case 'G': { addlenmod(form, LUA_FLTFRMLEN); nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); break; } case 's': { uint l; CharPtr s = luaL_tolstring(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ luaL_addvalue(b); break; } else { nb = sprintf(buff, form, s); lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ break; } } default: { /* also treat cases `pnLlh' */ return(luaL_error(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("format"), strfrmt[-1])); } } luaL_addsize(b, (uint)nb); //FIXME:changed, (uint) } } luaL_pushresult(b); return(1); }
private static int str_format(lua_State L) { int arg = 1; uint sfl; CharPtr strfrmt = luaL_checklstring(L, arg, out sfl); CharPtr strfrmt_end = strfrmt + sfl; luaL_Buffer b = new luaL_Buffer(); luaL_buffinit(L, b); while (strfrmt < strfrmt_end) { if (strfrmt[0] != L_ESC) { luaL_addchar(b, strfrmt[0]); strfrmt = strfrmt.next(); } else if (strfrmt[1] == L_ESC) { luaL_addchar(b, strfrmt[0]); /* %% */ strfrmt = strfrmt + 2; } else { /* format item */ strfrmt = strfrmt.next(); CharPtr form = new char[MAX_FORMAT]; /* to store the format (`%...') */ CharPtr buff = new char[MAX_ITEM]; /* to store the formatted item */ arg++; strfrmt = scanformat(L, strfrmt, form); char ch = strfrmt[0]; strfrmt = strfrmt.next(); switch (ch) { case 'c': { sprintf(buff, form, (int)luaL_checknumber(L, arg)); break; } case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': { lua_Number n = luaL_checknumber(L, arg); LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : (LUA_INTFRM_T)(UInt64)n; addintlen(form); sprintf(buff, form, r); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { sprintf(buff, form, (double)luaL_checknumber(L, arg)); break; } case 'q': { addquoted(L, b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { uint l; CharPtr s = luaL_checklstring(L, arg, out l); if ((strchr(form, '.') == null) && l >= 100) { /* no precision and string is too long to be formatted; * keep original string */ lua_pushvalue(L, arg); luaL_addvalue(b); continue; /* skip the `addsize' at the end */ } else { sprintf(buff, form, s); break; } } default: { /* also treat cases `pnLlh' */ return(luaL_error(L, "invalid option " + LUA_QL("%%%c") + " to " + LUA_QL("format"), strfrmt[-1])); } } luaL_addlstring(b, buff, (uint)strlen(buff)); } } luaL_pushresult(b); return(1); }