mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-15 23:29:56 +00:00
Add pretty printing to redbean serializers
This commit is contained in:
parent
6bb8b26d70
commit
516b68606f
11 changed files with 327 additions and 91 deletions
|
@ -787,6 +787,20 @@ FUNCTIONS
|
|||
don't care about ordering then setting `sorted=false`
|
||||
should yield a performance boost in serialization.
|
||||
|
||||
- pretty: (bool=false) Setting this option to true will
|
||||
cause tables with more than one entry to be formatted
|
||||
across multiple lines for readability.
|
||||
|
||||
- indent: (str=" ") This option controls the indentation of
|
||||
pretty formatting. This field is ignored if `pretty` isn't
|
||||
true.
|
||||
|
||||
- maxdepth: (int=64) This option controls the maximum amount
|
||||
of recursion the serializer is allowed to perform. The max
|
||||
is 32767. You might not be able to set it that high if
|
||||
there isn't enough C stack memory. Your serializer checks
|
||||
for this and will return an error rather than crashing.
|
||||
|
||||
This function will return an error if:
|
||||
|
||||
- `value` is cyclic
|
||||
|
@ -844,6 +858,20 @@ FUNCTIONS
|
|||
don't care about ordering then setting `sorted=false`
|
||||
should yield a performance boost in serialization.
|
||||
|
||||
- pretty: (bool=false) Setting this option to true will
|
||||
cause tables with more than one entry to be formatted
|
||||
across multiple lines for readability.
|
||||
|
||||
- indent: (str=" ") This option controls the indentation of
|
||||
pretty formatting. This field is ignored if `pretty` isn't
|
||||
true.
|
||||
|
||||
- maxdepth: (int=64) This option controls the maximum amount
|
||||
of recursion the serializer is allowed to perform. The max
|
||||
is 32767. You might not be able to set it that high if
|
||||
there isn't enough C stack memory. Your serializer checks
|
||||
for this and will return an error rather than crashing.
|
||||
|
||||
If a user data object has a `__repr` or `__tostring` meta
|
||||
method, then that'll be used to encode the Lua code.
|
||||
|
||||
|
|
|
@ -4239,12 +4239,16 @@ static int LuaLog(lua_State *L) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int LuaEncodeSmth(lua_State *L,
|
||||
int Encoder(lua_State *, char **, int, bool)) {
|
||||
static int LuaEncodeSmth(lua_State *L, int Encoder(lua_State *, char **, int,
|
||||
struct EncoderConfig)) {
|
||||
char *p = 0;
|
||||
int maxdepth = 64;
|
||||
int sorted = true;
|
||||
int useoutput = false;
|
||||
struct EncoderConfig conf = {
|
||||
.maxdepth = 64,
|
||||
.sorted = true,
|
||||
.pretty = false,
|
||||
.indent = " ",
|
||||
};
|
||||
if (lua_istable(L, 2)) {
|
||||
lua_settop(L, 2); // discard any extra arguments
|
||||
lua_getfield(L, 2, "useoutput");
|
||||
|
@ -4252,11 +4256,27 @@ static int LuaEncodeSmth(lua_State *L,
|
|||
if (ishandlingrequest && lua_isboolean(L, -1)) {
|
||||
useoutput = lua_toboolean(L, -1);
|
||||
}
|
||||
lua_getfield(L, 2, "maxdepth");
|
||||
if (!lua_isnoneornil(L, -1)) {
|
||||
lua_Integer n = lua_tointeger(L, -1);
|
||||
n = MAX(0, MIN(n, SHRT_MAX));
|
||||
conf.maxdepth = n;
|
||||
}
|
||||
lua_getfield(L, 2, "sorted");
|
||||
sorted = lua_toboolean(L, -1);
|
||||
if (!lua_isnoneornil(L, -1)) {
|
||||
conf.sorted = lua_toboolean(L, -1);
|
||||
}
|
||||
lua_getfield(L, 2, "pretty");
|
||||
if (!lua_isnoneornil(L, -1)) {
|
||||
conf.pretty = lua_toboolean(L, -1);
|
||||
lua_getfield(L, 2, "indent");
|
||||
if (!lua_isnoneornil(L, -1)) {
|
||||
conf.indent = luaL_checkstring(L, -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_settop(L, 1); // keep the passed argument on top
|
||||
if (Encoder(L, useoutput ? &outbuf : &p, -1, sorted) == -1) {
|
||||
if (Encoder(L, useoutput ? &outbuf : &p, -1, conf) == -1) {
|
||||
free(p);
|
||||
return 2;
|
||||
}
|
||||
|
@ -5373,7 +5393,13 @@ static void LuaPrint(lua_State *L) {
|
|||
if (n > 0) {
|
||||
for (i = 1; i <= n; i++) {
|
||||
if (i > 1) appendw(&b, '\t');
|
||||
LuaEncodeLuaData(L, &b, i, true);
|
||||
struct EncoderConfig conf = {
|
||||
.maxdepth = 64,
|
||||
.sorted = true,
|
||||
.pretty = true,
|
||||
.indent = " ",
|
||||
};
|
||||
LuaEncodeLuaData(L, &b, i, conf);
|
||||
}
|
||||
appendw(&b, '\n');
|
||||
WRITE(1, b, appendz(b).i);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue