jsonfmt: Move arg parsing into XS
Going to need a way to pass arguments into the XS function anyway, so might as well do the entire arg parsing step in XS while we're at it. Provides a significant speedup for tiny inputs as well, but I don't find that too interesting.
This commit is contained in:
parent
e0161cd22c
commit
8ef2a724d1
6 changed files with 40 additions and 18 deletions
29
c/jsonfmt.c
29
c/jsonfmt.c
|
|
@ -172,6 +172,35 @@ static void fujson_fmt(pTHX_ fustr *out, SV *val) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
static SV *fujson_fmt_xs(pTHX_ I32 ax, I32 argc, SV *val) {
|
||||
I32 i = 1;
|
||||
int encutf8 = 0;
|
||||
char *arg;
|
||||
SV *r;
|
||||
|
||||
while (i < argc) {
|
||||
arg = SvPV_nolen(ST(i));
|
||||
i++;
|
||||
if (i == argc) croak("Odd name/value argument for json_format()");
|
||||
r = ST(i);
|
||||
i++;
|
||||
|
||||
if (strcmp(arg, "utf8") == 0) {
|
||||
encutf8 = SvPVXtrue(r);
|
||||
} else {
|
||||
croak("Unknown flag: '%s'", arg);
|
||||
}
|
||||
}
|
||||
|
||||
fustr buf;
|
||||
fustr_init(&buf, 128);
|
||||
fujson_fmt(aTHX_ &buf, val);
|
||||
r = fustr_done(&buf);
|
||||
if (!encutf8) SvUTF8_on(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* TODO: canonical */
|
||||
/* TODO: pretty */
|
||||
/* TODO: max depth? */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue