Skip to content

Commit

Permalink
Improve cat.decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Mar 4, 2024
1 parent e886b9b commit 8889bde
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
42 changes: 19 additions & 23 deletions src/lcatlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static int cat_encode (lua_State *L) {
return 1;
}

static void cat_decode_aux (lua_State *L, const soup::catNode& node, bool withorder) {
static void cat_decode_aux_flat (lua_State *L, const soup::catNode& node, bool withorder) {
if (!node.value.empty()) {
lua_pushliteral(L, "__value");
pluto_pushstring(L, node.value);
Expand All @@ -54,7 +54,7 @@ static void cat_decode_aux (lua_State *L, const soup::catNode& node, bool withor
pluto_pushstring(L, child->name);
if (!child->children.empty()) {
lua_newtable(L);
cat_decode_aux(L, *child, withorder);
cat_decode_aux_flat(L, *child, withorder);
}
else {
pluto_pushstring(L, child->value);
Expand Down Expand Up @@ -102,19 +102,27 @@ static void cat_decode_aux_expanded (lua_State* L, const soup::catNode& node) {
}

static int cat_decode (lua_State *L) {
int flags = (int)luaL_optinteger(L, 2, 0);
const bool expanded = (flags & (1 << 0));
const bool withorder = (flags & (1 << 1));
if (expanded && withorder)
luaL_error(L, "cat.expanded and cat.withorder are mutually exclusive");
bool flat = false;
bool withorder = false;
if (lua_gettop(L) >= 2) {
const char *mode = luaL_checkstring(L, 2);
if (strcmp(mode, "flat") == 0)
flat = true;
else if (strcmp(mode, "flatwithorder") == 0) {
flat = true;
withorder = true;
}
else if (strcmp(mode, "expanded") != 0)
luaL_error(L, "unknown output format '%s'", mode);
}
std::string data = pluto_checkstring(L, 1);
soup::StringRefReader sr(data);
if (auto root = soup::catParse(sr)) {
lua_newtable(L);
if (expanded)
cat_decode_aux_expanded(L, *root);
if (flat)
cat_decode_aux_flat(L, *root, withorder);
else
cat_decode_aux(L, *root, withorder);
cat_decode_aux_expanded(L, *root);
return 1;
}
return 0;
Expand All @@ -126,16 +134,4 @@ static const luaL_Reg funcs[] = {
{nullptr, nullptr}
};

LUAMOD_API int luaopen_cat (lua_State *L) {
luaL_newlib(L, funcs);

// decode flags
lua_pushinteger(L, 1 << 0);
lua_setfield(L, -2, "expanded");
lua_pushinteger(L, 1 << 1);
lua_setfield(L, -2, "withorder");

return 1;
}

const Pluto::PreloadedLibrary Pluto::preloaded_cat{ "cat", funcs, &luaopen_cat };
PLUTO_NEWLIB(cat)
6 changes: 3 additions & 3 deletions testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -1616,18 +1616,18 @@ do
local cat = require"pluto:cat"

local data = "List: With Value\n\tChild: With Value\n"
local t = cat.decode(data)
local t = cat.decode(data, "flat")
assert(t.List.__value == "With Value")
assert(t.List.Child == "With Value")
assert(cat.encode(t) == data)

data = "Hello: World\n"
t = cat.decode(data)
t = cat.decode(data, "flat")
assert(t.Hello == "World")
assert(cat.encode(t) == data)

data = "First\nSecond"
t = cat.decode(data, cat.expanded)
t = cat.decode(data, "expanded")
assert(t[1].name == "First")
assert(t[2].name == "Second")
end
Expand Down

0 comments on commit 8889bde

Please sign in to comment.