MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / luaO_pushvfstring

Function luaO_pushvfstring

3rd/lua-5.4.3/src/lobject.c:470–536  ·  view source on GitHub ↗

** this function handles only '%d', '%c', '%f', '%p', '%s', and '%%' conventional formats, plus Lua-specific '%I' and '%U' */

Source from the content-addressed store, hash-verified

468 conventional formats, plus Lua-specific '%I' and '%U'
469*/
470const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
471 BuffFS buff; /* holds last part of the result */
472 const char *e; /* points to next '%' */
473 buff.pushed = buff.blen = 0;
474 buff.L = L;
475 while ((e = strchr(fmt, '%')) != NULL) {
476 addstr2buff(&buff, fmt, e - fmt); /* add 'fmt' up to '%' */
477 switch (*(e + 1)) { /* conversion specifier */
478 case 's': { /* zero-terminated string */
479 const char *s = va_arg(argp, char *);
480 if (s == NULL) s = "(null)";
481 addstr2buff(&buff, s, strlen(s));
482 break;
483 }
484 case 'c': { /* an 'int' as a character */
485 char c = cast_uchar(va_arg(argp, int));
486 addstr2buff(&buff, &c, sizeof(char));
487 break;
488 }
489 case 'd': { /* an 'int' */
490 TValue num;
491 setivalue(&num, va_arg(argp, int));
492 addnum2buff(&buff, &num);
493 break;
494 }
495 case 'I': { /* a 'lua_Integer' */
496 TValue num;
497 setivalue(&num, cast(lua_Integer, va_arg(argp, l_uacInt)));
498 addnum2buff(&buff, &num);
499 break;
500 }
501 case 'f': { /* a 'lua_Number' */
502 TValue num;
503 setfltvalue(&num, cast_num(va_arg(argp, l_uacNumber)));
504 addnum2buff(&buff, &num);
505 break;
506 }
507 case 'p': { /* a pointer */
508 const int sz = 3 * sizeof(void*) + 8; /* enough space for '%p' */
509 char *bf = getbuff(&buff, sz);
510 void *p = va_arg(argp, void *);
511 int len = lua_pointer2str(bf, sz, p);
512 addsize(&buff, len);
513 break;
514 }
515 case 'U': { /* a 'long' as a UTF-8 sequence */
516 char bf[UTF8BUFFSZ];
517 int len = luaO_utf8esc(bf, va_arg(argp, long));
518 addstr2buff(&buff, bf + UTF8BUFFSZ - len, len);
519 break;
520 }
521 case '%': {
522 addstr2buff(&buff, "%", 1);
523 break;
524 }
525 default: {
526 luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'",
527 *(e + 1));

Callers 4

luaO_pushfstringFunction · 0.85
luaG_runerrorFunction · 0.85
lua_pushvfstringFunction · 0.85
lua_pushfstringFunction · 0.85

Calls 6

addstr2buffFunction · 0.85
addnum2buffFunction · 0.85
getbuffFunction · 0.85
luaO_utf8escFunction · 0.85
luaG_runerrorFunction · 0.85
clearbuffFunction · 0.85

Tested by

no test coverage detected