Implement printf, dprintf, string.format

This commit is contained in:
2026-02-17 19:57:58 -05:00
parent a987754b38
commit ff81d79b4a
7 changed files with 363 additions and 121 deletions

View File

@@ -27,6 +27,7 @@ struct PrettyPrintOptions {
int level;
bool expand;
PrettyPrintOptions() : indent(true), level(0), expand(true) {}
PrettyPrintOptions(bool indent, bool expand) : indent(indent), level(0), expand(expand) {}
void parse(LuaKeywordParser &kp);
};
@@ -42,4 +43,36 @@ void atomic_print(LuaCoreStack &LS, LuaSlot val, bool quote, std::ostream *os);
//
void pprint(LuaCoreStack &LS, LuaSlot val, const PrettyPrintOptions &opts, std::ostream *os);
// Format a string using printf-style format specifiers, consuming
// arguments from a LuaExtraArgs. Returns an empty string on success,
// or an error message on failure.
//
// Numeric types (argument must be a number). All of the following
// use the same formatting parameters as 'printf'.
//
// %d, %i — signed decimal integer
// %o — unsigned octal
// %u — unsigned decimal
// %x, %X — unsigned hexadecimal (lower/upper)
// %e, %E — scientific notation (lower/upper)
// %f — decimal floating point
// %g, %G — shortest of %e/%f (lower/upper)
// %c — character (integer converted to character)
//
// Non-numeric types:
// %s — prints any lua value using atomic_print (unquoted)
// %q — prints any lua value using atomic_print (quoted)
//
// Pretty-printing:
//
// %p - pretty print
// %lp - pretty print, but all on one line
// %P - pretty print, force table expansion
// %lP - pretty print, force table expansion, all on one line
//
// Special:
// %% — literal percent sign (consumes no argument)
//
eng::string format(LuaCoreStack &LS, std::string_view fmt, LuaExtraArgs args, std::ostream *os);
#endif // PPRINT_HPP