mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-04-15 12:18:52 +00:00
Simplify redbean serialization code
This commit is contained in:
parent
3fdb1c14f1
commit
8bd9ad8342
9 changed files with 261 additions and 86 deletions
40
libc/stdio/appendstrlist.c
Normal file
40
libc/stdio/appendstrlist.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/stdio/append.internal.h"
|
||||||
|
#include "libc/stdio/strlist.internal.h"
|
||||||
|
|
||||||
|
int AppendStrList(struct StrList *sl) {
|
||||||
|
int n2;
|
||||||
|
char **p2;
|
||||||
|
if (sl->i == sl->n) {
|
||||||
|
n2 = sl->n;
|
||||||
|
if (!n2) n2 = 2;
|
||||||
|
n2 += n2 >> 1;
|
||||||
|
if ((p2 = realloc(sl->p, n2 * sizeof(*p2)))) {
|
||||||
|
sl->p = p2;
|
||||||
|
sl->n = n2;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sl->p[sl->i] = 0;
|
||||||
|
appendr(&sl->p[sl->i], 0);
|
||||||
|
return sl->i++;
|
||||||
|
}
|
32
libc/stdio/freestrlist.c
Normal file
32
libc/stdio/freestrlist.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/stdio/append.internal.h"
|
||||||
|
#include "libc/stdio/strlist.internal.h"
|
||||||
|
|
||||||
|
void FreeStrList(struct StrList *sl) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < sl->i; ++i) {
|
||||||
|
free(sl->p[i]);
|
||||||
|
}
|
||||||
|
free(sl->p);
|
||||||
|
sl->p = 0;
|
||||||
|
sl->i = 0;
|
||||||
|
sl->n = 0;
|
||||||
|
}
|
38
libc/stdio/joinstrlist.c
Normal file
38
libc/stdio/joinstrlist.c
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/stdio/append.internal.h"
|
||||||
|
#include "libc/stdio/strlist.internal.h"
|
||||||
|
|
||||||
|
int JoinStrList(struct StrList *sl, char **buf, uint64_t sep) {
|
||||||
|
int i;
|
||||||
|
if (!*buf && !sl->i) {
|
||||||
|
return appendr(buf, 0);
|
||||||
|
}
|
||||||
|
for (i = 0; i < sl->i; ++i) {
|
||||||
|
if (i) {
|
||||||
|
if (appendw(buf, sep) == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (appends(buf, sl->p[i]) == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
35
libc/stdio/sortstrlist.c
Normal file
35
libc/stdio/sortstrlist.c
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/mem/alg.h"
|
||||||
|
#include "libc/stdio/strlist.internal.h"
|
||||||
|
|
||||||
|
static int CompareStrings(const void *p1, const void *p2) {
|
||||||
|
const char *a = *(const char **)p1;
|
||||||
|
const char *b = *(const char **)p2;
|
||||||
|
for (; *a == *b; a++, b++) {
|
||||||
|
if (!*a) break;
|
||||||
|
}
|
||||||
|
return (*a & 0xff) - (*b & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SortStrList(struct StrList *sl) {
|
||||||
|
if (sl->i) {
|
||||||
|
qsort(sl->p, sl->i, sizeof(*sl->p), CompareStrings);
|
||||||
|
}
|
||||||
|
}
|
18
libc/stdio/strlist.internal.h
Normal file
18
libc/stdio/strlist.internal.h
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef COSMOPOLITAN_LIBC_STDIO_STRLIST_H_
|
||||||
|
#define COSMOPOLITAN_LIBC_STDIO_STRLIST_H_
|
||||||
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||||
|
COSMOPOLITAN_C_START_
|
||||||
|
|
||||||
|
struct StrList {
|
||||||
|
int i, n;
|
||||||
|
char **p;
|
||||||
|
};
|
||||||
|
|
||||||
|
void FreeStrList(struct StrList *) hidden;
|
||||||
|
int AppendStrList(struct StrList *) hidden;
|
||||||
|
void SortStrList(struct StrList *) hidden;
|
||||||
|
int JoinStrList(struct StrList *, char **, uint64_t) hidden;
|
||||||
|
|
||||||
|
COSMOPOLITAN_C_END_
|
||||||
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||||
|
#endif /* COSMOPOLITAN_LIBC_STDIO_STRLIST_H_ */
|
55
test/libc/stdio/joinstrlist_test.c
Normal file
55
test/libc/stdio/joinstrlist_test.c
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||||
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||||
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||||
|
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||||
|
│ │
|
||||||
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||||
|
│ any purpose with or without fee is hereby granted, provided that the │
|
||||||
|
│ above copyright notice and this permission notice appear in all copies. │
|
||||||
|
│ │
|
||||||
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||||
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||||
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||||
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||||
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||||
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||||
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
|
#include "libc/intrin/bits.h"
|
||||||
|
#include "libc/mem/mem.h"
|
||||||
|
#include "libc/stdio/append.internal.h"
|
||||||
|
#include "libc/stdio/strlist.internal.h"
|
||||||
|
#include "libc/testlib/testlib.h"
|
||||||
|
|
||||||
|
struct StrList sl;
|
||||||
|
|
||||||
|
void TearDown(void) {
|
||||||
|
FreeStrList(&sl);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(strlist, test) {
|
||||||
|
int i;
|
||||||
|
char *b = 0;
|
||||||
|
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||||
|
ASSERT_NE(-1, appends(&sl.p[i], "world"));
|
||||||
|
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||||
|
ASSERT_NE(-1, appends(&sl.p[i], "hello"));
|
||||||
|
SortStrList(&sl);
|
||||||
|
ASSERT_NE(-1, JoinStrList(&sl, &b, READ16LE(", ")));
|
||||||
|
EXPECT_STREQ("hello, world", b);
|
||||||
|
free(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(strlist, testNumbers) {
|
||||||
|
int i;
|
||||||
|
char *b = 0;
|
||||||
|
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||||
|
ASSERT_NE(-1, appends(&sl.p[i], "2"));
|
||||||
|
ASSERT_NE(-1, (i = AppendStrList(&sl)));
|
||||||
|
ASSERT_NE(-1, appends(&sl.p[i], "1"));
|
||||||
|
SortStrList(&sl);
|
||||||
|
ASSERT_NE(-1, JoinStrList(&sl, &b, ':'));
|
||||||
|
EXPECT_STREQ("1:2", b);
|
||||||
|
free(b);
|
||||||
|
}
|
9
third_party/lua/cosmo.h
vendored
9
third_party/lua/cosmo.h
vendored
|
@ -22,15 +22,6 @@ struct Serializer {
|
||||||
size_t strbuflen;
|
size_t strbuflen;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SerializerJoin {
|
|
||||||
struct Serializer *z;
|
|
||||||
char **buf;
|
|
||||||
bool multi;
|
|
||||||
int depth;
|
|
||||||
int i;
|
|
||||||
const char *indent;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool LuaHasMultipleItems(lua_State *);
|
bool LuaHasMultipleItems(lua_State *);
|
||||||
char *LuaFormatStack(lua_State *) dontdiscard;
|
char *LuaFormatStack(lua_State *) dontdiscard;
|
||||||
int LuaCallWithTrace(lua_State *, int, int, lua_State *);
|
int LuaCallWithTrace(lua_State *, int, int, lua_State *);
|
||||||
|
|
55
third_party/lua/luaencodejsondata.c
vendored
55
third_party/lua/luaencodejsondata.c
vendored
|
@ -16,17 +16,17 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/mem/critbit0.h"
|
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
|
#include "libc/fmt/itoa.h"
|
||||||
#include "libc/intrin/bits.h"
|
#include "libc/intrin/bits.h"
|
||||||
#include "libc/intrin/likely.h"
|
#include "libc/intrin/likely.h"
|
||||||
#include "libc/fmt/itoa.h"
|
|
||||||
#include "libc/log/log.h"
|
#include "libc/log/log.h"
|
||||||
#include "libc/log/rop.h"
|
#include "libc/log/rop.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/gc.internal.h"
|
#include "libc/runtime/gc.internal.h"
|
||||||
#include "libc/runtime/stack.h"
|
#include "libc/runtime/stack.h"
|
||||||
#include "libc/stdio/append.internal.h"
|
#include "libc/stdio/append.internal.h"
|
||||||
|
#include "libc/stdio/strlist.internal.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
#include "net/http/escape.h"
|
#include "net/http/escape.h"
|
||||||
#include "third_party/double-conversion/wrapper.h"
|
#include "third_party/double-conversion/wrapper.h"
|
||||||
|
@ -127,56 +127,39 @@ OnError:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static intptr_t Join(const char *elem, void *arg) {
|
|
||||||
struct SerializerJoin *j = arg;
|
|
||||||
if (!j->i) {
|
|
||||||
++j->i;
|
|
||||||
} else {
|
|
||||||
RETURN_ON_ERROR(appendw(j->buf, ','));
|
|
||||||
if (j->multi) {
|
|
||||||
RETURN_ON_ERROR(SerializeObjectIndent(j->buf, j->z, j->depth + 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RETURN_ON_ERROR(appends(j->buf, elem));
|
|
||||||
return 0;
|
|
||||||
OnError:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int SerializeSorted(lua_State *L, char **buf, struct Serializer *z,
|
static int SerializeSorted(lua_State *L, char **buf, struct Serializer *z,
|
||||||
int depth, bool multi) {
|
int depth, bool multi) {
|
||||||
int i;
|
int i;
|
||||||
char *b = 0;
|
struct StrList sl = {0};
|
||||||
struct critbit0 t = {0};
|
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
while (lua_next(L, -2)) {
|
while (lua_next(L, -2)) {
|
||||||
if (lua_type(L, -2) == LUA_TSTRING) {
|
if (lua_type(L, -2) == LUA_TSTRING) {
|
||||||
RETURN_ON_ERROR(appendr(&b, 0));
|
RETURN_ON_ERROR(i = AppendStrList(&sl));
|
||||||
RETURN_ON_ERROR(SerializeString(L, &b, -2, z));
|
RETURN_ON_ERROR(SerializeString(L, sl.p + i, -2, z));
|
||||||
RETURN_ON_ERROR(appendw(&b, z->conf.pretty ? READ16LE(": ") : ':'));
|
RETURN_ON_ERROR(appendw(sl.p + i, z->conf.pretty ? READ16LE(": ") : ':'));
|
||||||
RETURN_ON_ERROR(Serialize(L, &b, -1, z, depth + 1));
|
RETURN_ON_ERROR(Serialize(L, sl.p + i, -1, z, depth + 1));
|
||||||
RETURN_ON_ERROR(critbit0_insert(&t, b));
|
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
} else {
|
} else {
|
||||||
z->reason = "json objects must only use string keys";
|
z->reason = "json objects must only use string keys";
|
||||||
goto OnError;
|
goto OnError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct SerializerJoin j = {
|
SortStrList(&sl);
|
||||||
.z = z,
|
|
||||||
.buf = buf,
|
|
||||||
.multi = multi,
|
|
||||||
.depth = depth,
|
|
||||||
};
|
|
||||||
RETURN_ON_ERROR(SerializeObjectStart(buf, z, depth, multi));
|
RETURN_ON_ERROR(SerializeObjectStart(buf, z, depth, multi));
|
||||||
RETURN_ON_ERROR(critbit0_allprefixed(&t, "", Join, &j));
|
for (i = 0; i < sl.i; ++i) {
|
||||||
|
if (i) {
|
||||||
|
RETURN_ON_ERROR(appendw(buf, ','));
|
||||||
|
if (multi) {
|
||||||
|
RETURN_ON_ERROR(SerializeObjectIndent(buf, z, depth + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RETURN_ON_ERROR(appends(buf, sl.p[i]));
|
||||||
|
}
|
||||||
RETURN_ON_ERROR(SerializeObjectEnd(buf, z, depth, multi));
|
RETURN_ON_ERROR(SerializeObjectEnd(buf, z, depth, multi));
|
||||||
critbit0_clear(&t);
|
FreeStrList(&sl);
|
||||||
free(b);
|
|
||||||
return 0;
|
return 0;
|
||||||
OnError:
|
OnError:
|
||||||
critbit0_clear(&t);
|
FreeStrList(&sl);
|
||||||
free(b);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
65
third_party/lua/luaencodeluadata.c
vendored
65
third_party/lua/luaencodeluadata.c
vendored
|
@ -16,15 +16,15 @@
|
||||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||||
#include "libc/mem/critbit0.h"
|
|
||||||
#include "libc/assert.h"
|
#include "libc/assert.h"
|
||||||
#include "libc/intrin/bits.h"
|
|
||||||
#include "libc/fmt/itoa.h"
|
#include "libc/fmt/itoa.h"
|
||||||
|
#include "libc/intrin/bits.h"
|
||||||
#include "libc/log/rop.h"
|
#include "libc/log/rop.h"
|
||||||
#include "libc/math.h"
|
#include "libc/math.h"
|
||||||
#include "libc/mem/mem.h"
|
#include "libc/mem/mem.h"
|
||||||
#include "libc/runtime/stack.h"
|
#include "libc/runtime/stack.h"
|
||||||
#include "libc/stdio/append.internal.h"
|
#include "libc/stdio/append.internal.h"
|
||||||
|
#include "libc/stdio/strlist.internal.h"
|
||||||
#include "libc/str/str.h"
|
#include "libc/str/str.h"
|
||||||
#include "libc/x/x.h"
|
#include "libc/x/x.h"
|
||||||
#include "third_party/double-conversion/wrapper.h"
|
#include "third_party/double-conversion/wrapper.h"
|
||||||
|
@ -304,64 +304,47 @@ OnError:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static intptr_t Join(const char *elem, void *arg) {
|
|
||||||
struct SerializerJoin *j = arg;
|
|
||||||
if (!j->i) {
|
|
||||||
++j->i;
|
|
||||||
} else {
|
|
||||||
if (j->multi) {
|
|
||||||
RETURN_ON_ERROR(appendw(j->buf, ','));
|
|
||||||
RETURN_ON_ERROR(SerializeObjectIndent(j->buf, j->z, j->depth + 1));
|
|
||||||
} else {
|
|
||||||
RETURN_ON_ERROR(appendw(j->buf, READ16LE(", ")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RETURN_ON_ERROR(appends(j->buf, elem));
|
|
||||||
return 0;
|
|
||||||
OnError:
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int SerializeSorted(lua_State *L, char **buf, struct Serializer *z,
|
static int SerializeSorted(lua_State *L, char **buf, struct Serializer *z,
|
||||||
int depth, bool multi) {
|
int depth, bool multi) {
|
||||||
size_t n;
|
size_t n;
|
||||||
int i, rc;
|
int i, rc;
|
||||||
char *b = 0;
|
|
||||||
const char *s;
|
const char *s;
|
||||||
struct critbit0 t = {0};
|
struct StrList sl = {0};
|
||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
while (lua_next(L, -2)) {
|
while (lua_next(L, -2)) {
|
||||||
RETURN_ON_ERROR(appendr(&b, 0));
|
RETURN_ON_ERROR(i = AppendStrList(&sl));
|
||||||
if (lua_type(L, -2) == LUA_TSTRING && IsLuaIdentifier(L, -2)) {
|
if (lua_type(L, -2) == LUA_TSTRING && IsLuaIdentifier(L, -2)) {
|
||||||
// use {𝑘=𝑣′} syntax when 𝑘 is a legal lua identifier
|
// use {𝑘=𝑣′} syntax when 𝑘 is a legal lua identifier
|
||||||
s = lua_tolstring(L, -2, &n);
|
s = lua_tolstring(L, -2, &n);
|
||||||
RETURN_ON_ERROR(appendd(&b, s, n));
|
RETURN_ON_ERROR(appendd(sl.p + i, s, n));
|
||||||
RETURN_ON_ERROR(appendw(&b, '='));
|
RETURN_ON_ERROR(appendw(sl.p + i, '='));
|
||||||
} else {
|
} else {
|
||||||
// use {[𝑘′]=𝑣′} otherwise
|
// use {[𝑘′]=𝑣′} otherwise
|
||||||
RETURN_ON_ERROR(appendw(&b, '['));
|
RETURN_ON_ERROR(appendw(sl.p + i, '['));
|
||||||
RETURN_ON_ERROR(Serialize(L, &b, -2, z, depth + 1));
|
RETURN_ON_ERROR(Serialize(L, sl.p + i, -2, z, depth + 1));
|
||||||
RETURN_ON_ERROR(appendw(&b, ']' | '=' << 010));
|
RETURN_ON_ERROR(appendw(sl.p + i, ']' | '=' << 010));
|
||||||
}
|
}
|
||||||
RETURN_ON_ERROR(Serialize(L, &b, -1, z, depth + 1));
|
RETURN_ON_ERROR(Serialize(L, sl.p + i, -1, z, depth + 1));
|
||||||
RETURN_ON_ERROR(critbit0_insert(&t, b));
|
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
struct SerializerJoin j = {
|
SortStrList(&sl);
|
||||||
.z = z,
|
|
||||||
.buf = buf,
|
|
||||||
.multi = multi,
|
|
||||||
.depth = depth,
|
|
||||||
};
|
|
||||||
RETURN_ON_ERROR(SerializeObjectStart(buf, z, depth, multi));
|
RETURN_ON_ERROR(SerializeObjectStart(buf, z, depth, multi));
|
||||||
RETURN_ON_ERROR(critbit0_allprefixed(&t, "", Join, &j));
|
for (i = 0; i < sl.i; ++i) {
|
||||||
|
if (i) {
|
||||||
|
if (multi) {
|
||||||
|
RETURN_ON_ERROR(appendw(buf, ','));
|
||||||
|
RETURN_ON_ERROR(SerializeObjectIndent(buf, z, depth + 1));
|
||||||
|
} else {
|
||||||
|
RETURN_ON_ERROR(appendw(buf, READ16LE(", ")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RETURN_ON_ERROR(appends(buf, sl.p[i]));
|
||||||
|
}
|
||||||
RETURN_ON_ERROR(SerializeObjectEnd(buf, z, depth, multi));
|
RETURN_ON_ERROR(SerializeObjectEnd(buf, z, depth, multi));
|
||||||
critbit0_clear(&t);
|
FreeStrList(&sl);
|
||||||
free(b);
|
|
||||||
return 0;
|
return 0;
|
||||||
OnError:
|
OnError:
|
||||||
critbit0_clear(&t);
|
FreeStrList(&sl);
|
||||||
free(b);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue