int wwstr_format_helper (luaL_Buffer* b, lua_State *L, int arg) { size_t sfl; const lua_WChar *strfrmt = luaL_checklwstring(L, arg, &sfl); const lua_WChar *strfrmt_end = strfrmt+sfl; luaL_wbuffinit(L, b); while (strfrmt < strfrmt_end) { if (*strfrmt != WL_ESC) luaL_addwchar(b, *strfrmt++); else if (*++strfrmt == WL_ESC) luaL_addwchar(b, *strfrmt++); /* %% */ else { /* format item */ lua_WChar form[MAX_FORMAT]; /* to store the format (`%...') */ lua_WChar buff[MAX_ITEM]; /* to store the formatted item */ char _form[MAX_FORMAT]; char _buff[MAX_ITEM]; if (iswdigit(*strfrmt) && *(strfrmt+1) == '$') return luaL_error(L, "obsolete `format' option (d$)"); arg++; strfrmt = wscanformat(L, strfrmt, form); switch (*strfrmt++) { case 'c': { translate_wide_to_single(form, _form); sprintf(_buff, _form, luaL_checkint(L, arg)); translate_single_to_wide(_buff, buff); break; } case 'd': case 'i': { waddintlen(form); translate_wide_to_single(form, _form); sprintf(_buff, _form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); translate_single_to_wide(_buff, buff); break; } case 'o': case 'u': case 'x': case 'X': { //?? How should this be for integers? translate_wide_to_single(form, _form); sprintf(_buff, _form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); translate_single_to_wide(_buff, buff); break; } case 'e': case 'E': case 'f': case 'g': case 'G': { translate_wide_to_single(form, _form); sprintf(_buff, _form, luaL_checknumber(L, arg)); translate_single_to_wide(_buff, buff); break; } case 'q': { luaI_waddquoted(L, b, arg); continue; /* skip the 'addsize' at the end */ } case 's': { size_t l; const lua_WChar *s = luaL_checklwstring(L, arg, &l); (void)s; if (!lua_WChar_chr(form, '.') && 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 { assert(0); // swprintf((wchar_t*)buff, (wchar_t*)form, s); break; } } case 'b': { buff[1] = buff[2] = buff[3] = buff[4] = buff[5] = buff[6] = buff[7] = buff[8] = 0; switch (*strfrmt++) { case 'b': { unsigned int num = (unsigned int)luaL_checkint(L, arg); buff[0] = (unsigned char)num; luaL_addlwstring(b, buff, 1); break; } case 'd': { unsigned int num = (unsigned int)luaL_checkint(L, arg); *(unsigned int*)(&buff) = num; luaL_addlwstring(b, buff, 4); break; } case 'w': { unsigned int num = (unsigned int)luaL_checkint(L, arg); *(unsigned short*)(&buff) = (unsigned short)num; luaL_addlwstring(b, buff, 2); break; } case 'f': { float numF = (float)luaL_checknumber(L, arg); *(float*)(&buff) = numF; luaL_addlwstring(b, buff, 4); break; } case 'F': { double numD = (double)luaL_checknumber(L, arg); *(double*)(&buff) = numD; luaL_addlwstring(b, buff, 8); break; } default: break; } buff[0] = 0; break; } } luaL_addlwstring(b, buff, lua_WChar_len(buff)); } } return 1; }
static void luaI_addquotedbinary (lua_State *L, luaL_Buffer *b, int arg) { size_t l; if (lua_type(L, arg) == LUA_TWSTRING) { const lua_WChar *s = luaL_checklwstring(L, arg, &l); luaL_addwchar(b, 'L'); luaL_addwchar(b, '"'); while (l--) { switch (*s) { case '"': case '\\': luaL_addwchar(b, '\\'); luaL_addwchar(b, *s); break; case '\a': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'a'); break; case '\b': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'b'); break; case '\f': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'f'); break; case '\n': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'n'); break; case '\r': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'r'); break; case '\t': luaL_addwchar(b, '\\'); luaL_addwchar(b, 't'); break; case '\v': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'v'); break; default: if (*s < 256 && isprint((unsigned char)*s)) { luaL_addwchar(b, *s); } else { char str[10]; lua_WChar wstr[10]; sprintf(str, "\\x%04x", (unsigned long)*s); translate_single_to_wide(str, wstr); luaL_addwstring(b, wstr); } } s++; } luaL_addwchar(b, '"'); } else { const char *s = luaL_checklstring(L, arg, &l); luaL_addwchar(b, '"'); while (l--) { switch (*s) { case '"': case '\\': luaL_addwchar(b, '\\'); luaL_addwchar(b, *s); break; case '\a': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'a'); break; case '\b': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'b'); break; case '\f': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'f'); break; case '\n': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'n'); break; case '\r': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'r'); break; case '\t': luaL_addwchar(b, '\\'); luaL_addwchar(b, 't'); break; case '\v': luaL_addwchar(b, '\\'); luaL_addwchar(b, 'v'); break; default: if (isprint((unsigned char)*s)) { luaL_addwchar(b, *s); } else { char str[10]; lua_WChar wstr[10]; sprintf(str, "\\x%02x", (unsigned int)*s); translate_single_to_wide(str, wstr); luaL_addwstring(b, wstr); } } s++; } luaL_addwchar(b, '"'); } }