Improve Lua and JSON serialization

This commit is contained in:
Justine Tunney 2022-07-12 23:31:06 -07:00
parent 3027d67037
commit e3cd476a9b
20 changed files with 1041 additions and 476 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/testlib/ezbench.h"
@ -24,60 +25,69 @@
#include "libc/testlib/testlib.h"
#include "net/http/escape.h"
char *o;
size_t y;
void TearDownOnce(void) {
free(o);
o = 0;
y = 0;
}
char *escapejs(const char *s) {
char *p;
size_t n;
p = EscapeJsStringLiteral(s, strlen(s), &n);
p = EscapeJsStringLiteral(&o, &y, s, strlen(s), &n);
ASSERT_EQ(strlen(p), n);
return p;
}
TEST(EscapeJsStringLiteral, test) {
EXPECT_STREQ("", gc(escapejs("")));
EXPECT_STREQ("\\u00ff", gc(escapejs("\377")));
EXPECT_STREQ("", escapejs(""));
EXPECT_STREQ("\\u00ff", escapejs("\377"));
EXPECT_STREQ("\\u00ff\\u0080\\u0080\\u0080\\u0080",
gc(escapejs("\377\200\200\200\200")));
escapejs("\377\200\200\200\200"));
EXPECT_STREQ("\\u0001\\u0002\\u0003 \\u0026\\u003d\\u003c\\u003e\\/",
gc(escapejs("\1\2\3 &=<>/")));
escapejs("\1\2\3 &=<>/"));
}
TEST(EscapeJsStringLiteral, testUcs2) {
EXPECT_STREQ("\\u00d0\\u263b", gc(escapejs("Ð☻")));
EXPECT_STREQ("\\u00d0\\u263b", escapejs("Ð☻"));
}
TEST(EscapeJsStringLiteral, testAstralPlanes) {
EXPECT_STREQ("\\ud800\\udf30\\ud800\\udf30", gc(escapejs("𐌰𐌰")));
EXPECT_STREQ("\\ud800\\udf30\\ud800\\udf30", escapejs("𐌰𐌰"));
}
TEST(EscapeJsStringLiteral, testBrokenUnicode_sparesInnocentCharacters) {
EXPECT_STREQ("\\u00e1YO", gc(escapejs("\xE1YO")));
EXPECT_STREQ("\\u00e1YO", escapejs("\xE1YO"));
}
void makefile1(void) {
FILE *f;
char *p;
size_t n;
p = EscapeJsStringLiteral(kHyperion, kHyperionSize, &n);
p = EscapeJsStringLiteral(&o, &y, kHyperion, kHyperionSize, &n);
f = fopen("/tmp/a", "wb");
fwrite(p, n, 1, f);
fclose(f);
free(p);
}
void makefile2(void) {
int fd;
char *p;
size_t n;
p = EscapeJsStringLiteral(kHyperion, kHyperionSize, &n);
p = EscapeJsStringLiteral(&o, &y, kHyperion, kHyperionSize, &n);
fd = creat("/tmp/a", 0644);
write(fd, p, n);
close(fd);
free(p);
}
BENCH(EscapeJsStringLiteral, bench) {
EZBENCH2("escapejs", donothing,
free(EscapeJsStringLiteral(kHyperion, kHyperionSize, 0)));
EZBENCH2("escapejs tiny", donothing,
EscapeJsStringLiteral(&o, &y, "hello", 5, 0));
EZBENCH2("escapejs book", donothing,
EscapeJsStringLiteral(&o, &y, kHyperion, kHyperionSize, 0));
EZBENCH2("makefile1", donothing, makefile1());
EZBENCH2("makefile2", donothing, makefile2());
}