libcubescript/src/lib_list.cc

646 lines
18 KiB
C++
Raw Normal View History

2017-02-08 01:07:35 +01:00
#include <functional>
#include <iterator>
2017-02-08 01:07:35 +01:00
2017-06-20 21:21:39 +02:00
#include <cubescript/cubescript.hh>
2021-03-23 01:11:21 +01:00
#include "cs_std.hh"
#include "cs_parser.hh"
2021-03-23 23:32:25 +01:00
namespace cubescript {
2016-08-17 20:51:21 +02:00
template<typename T>
2021-03-23 23:29:32 +01:00
struct arg_val;
2016-08-17 20:51:21 +02:00
template<>
2021-03-23 23:29:32 +01:00
struct arg_val<integer_type> {
static integer_type get(any_value &tv) {
2016-08-17 20:51:21 +02:00
return tv.get_int();
}
};
template<>
2021-03-23 23:29:32 +01:00
struct arg_val<float_type> {
static float_type get(any_value &tv) {
2016-08-17 20:51:21 +02:00
return tv.get_float();
}
};
template<>
2021-03-23 23:29:32 +01:00
struct arg_val<std::string_view> {
static std::string_view get(any_value &tv) {
2021-03-17 21:57:47 +01:00
return tv.get_str();
2016-08-17 20:51:21 +02:00
}
};
template<typename T, typename F>
2021-03-23 23:29:32 +01:00
static inline void list_find(
state &cs, std::span<any_value> args, any_value &res, F cmp
) {
2021-03-23 23:29:32 +01:00
integer_type n = 0, skip = args[2].get_int();
T val = arg_val<T>::get(args[1]);
for (list_parser p{cs, args[0].get_str()}; p.parse(); ++n) {
2016-08-17 20:51:21 +02:00
if (cmp(p, val)) {
res.set_int(n);
return;
}
for (int i = 0; i < skip; ++i) {
2021-03-20 19:34:26 +01:00
if (!p.parse()) {
2016-08-17 20:51:21 +02:00
goto notfound;
}
++n;
}
}
notfound:
res.set_int(-1);
}
template<typename T, typename F>
2021-03-23 23:29:32 +01:00
static inline void list_assoc(
state &cs, std::span<any_value> args, any_value &res, F cmp
) {
2021-03-23 23:29:32 +01:00
T val = arg_val<T>::get(args[1]);
for (list_parser p{cs, args[0].get_str()}; p.parse();) {
2016-08-17 20:51:21 +02:00
if (cmp(p, val)) {
2021-03-20 19:34:26 +01:00
if (p.parse()) {
res.set_str(p.get_item());
2016-08-17 20:51:21 +02:00
}
return;
}
2021-03-20 19:34:26 +01:00
if (!p.parse()) {
2016-08-17 20:51:21 +02:00
break;
}
}
}
2021-03-23 23:29:32 +01:00
static void loop_list_conc(
state &cs, any_value &res, ident *id, std::string_view list,
bcode *body, bool space
) {
2021-03-23 23:29:32 +01:00
stacked_value idv{cs, id};
2016-08-31 19:49:24 +02:00
if (!idv.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2021-03-23 23:29:32 +01:00
charbuf r{cs};
int n = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, list}; p.parse(); ++n) {
2021-03-20 19:34:26 +01:00
idv.set_str(p.get_item());
2016-08-31 19:49:24 +02:00
idv.push();
2016-08-17 20:51:21 +02:00
if (n && space) {
2021-03-19 02:55:59 +01:00
r.push_back(' ');
2016-08-17 20:51:21 +02:00
}
2021-03-23 23:29:32 +01:00
any_value v{cs};
2016-09-14 23:24:13 +02:00
switch (cs.run_loop(body, v)) {
2021-03-23 23:29:32 +01:00
case loop_state::BREAK:
2016-09-14 23:24:13 +02:00
goto end;
2021-03-23 23:29:32 +01:00
case loop_state::CONTINUE:
2016-09-14 23:24:13 +02:00
continue;
default:
break;
}
2021-03-19 02:55:59 +01:00
r.append(v.get_str());
}
2016-09-14 23:24:13 +02:00
end:
2021-03-19 02:55:59 +01:00
res.set_str(r.str());
}
2021-03-23 23:29:32 +01:00
int list_includes(
state &cs, std::string_view list, std::string_view needle
) {
int offset = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, list}; p.parse();) {
2021-03-20 19:34:26 +01:00
if (p.get_raw_item() == needle) {
return offset;
2016-08-17 20:51:21 +02:00
}
++offset;
}
return -1;
}
2016-08-17 20:51:21 +02:00
template<bool PushList, bool Swap, typename F>
2021-03-23 23:29:32 +01:00
static inline void list_merge(
state &cs, std::span<any_value> args, any_value &res, F cmp
) {
std::string_view list = args[0].get_str();
std::string_view elems = args[1].get_str();
2021-03-23 23:29:32 +01:00
charbuf buf{cs};
2016-08-17 20:51:21 +02:00
if (PushList) {
2021-03-19 02:55:59 +01:00
buf.append(list);
2016-08-17 20:51:21 +02:00
}
if (Swap) {
2017-01-29 15:56:38 +01:00
std::swap(list, elems);
2016-08-17 20:51:21 +02:00
}
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, list}; p.parse();) {
if (cmp(list_includes(cs, elems, p.get_raw_item()), 0)) {
2016-08-17 20:52:18 +02:00
if (!buf.empty()) {
2021-03-19 02:55:59 +01:00
buf.push_back(' ');
2016-08-17 20:52:18 +02:00
}
2021-03-20 19:34:26 +01:00
buf.append(p.get_quoted_item());
2016-08-17 20:51:21 +02:00
}
}
2021-03-19 02:55:59 +01:00
res.set_str(buf.str());
2016-08-17 20:51:21 +02:00
}
2021-03-23 23:29:32 +01:00
static void init_lib_list_sort(state &cs);
2021-03-23 23:29:32 +01:00
void init_lib_list(state &gcs) {
gcs.new_command("listlen", "s", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
res.set_int(integer_type(list_parser{cs, args[0].get_str()}.count()));
});
gcs.new_command("at", "si1V", [](auto &cs, auto args, auto &res) {
2016-08-17 20:51:21 +02:00
if (args.empty()) {
return;
2016-08-17 20:51:21 +02:00
}
2021-03-20 19:34:26 +01:00
if (args.size() <= 1) {
res = args[0];
return;
}
auto str = args[0].get_str();
2021-03-23 23:29:32 +01:00
list_parser p{cs, str};
2017-01-25 02:09:50 +01:00
for (size_t i = 1; i < args.size(); ++i) {
p.set_input(str);
2021-03-23 23:29:32 +01:00
integer_type pos = args[i].get_int();
2016-08-17 20:51:21 +02:00
for (; pos > 0; --pos) {
2021-03-20 19:34:26 +01:00
if (!p.parse()) {
2016-08-17 20:51:21 +02:00
break;
}
}
2021-03-20 19:34:26 +01:00
if (pos > 0 || !p.parse()) {
p.set_input("");
2016-08-17 20:51:21 +02:00
}
}
2021-03-20 19:34:26 +01:00
res.set_str(p.get_item());
});
gcs.new_command("sublist", "siiN", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
integer_type skip = args[1].get_int(),
2016-08-14 18:35:38 +02:00
count = args[2].get_int(),
2016-10-24 02:38:52 +02:00
numargs = args[3].get_int();
2021-03-23 23:29:32 +01:00
integer_type offset = std::max(skip, integer_type(0)),
len = (numargs >= 3) ? std::max(count, integer_type(0)) : -1;
2021-03-23 23:29:32 +01:00
list_parser p{cs, args[0].get_str()};
for (integer_type i = 0; i < offset; ++i) {
2021-03-20 19:34:26 +01:00
if (!p.parse()) break;
2016-08-17 20:51:21 +02:00
}
if (len < 0) {
2016-08-17 20:51:21 +02:00
if (offset > 0) {
2021-03-20 19:34:26 +01:00
p.skip_until_item();
2016-08-17 20:51:21 +02:00
}
res.set_str(p.get_input());
return;
}
char const *list = p.get_input().data();
2021-03-20 19:34:26 +01:00
if (len > 0 && p.parse()) {
while (--len > 0 && p.parse());
} else {
res.set_str("");
return;
2016-08-17 20:51:21 +02:00
}
2021-03-20 19:34:26 +01:00
auto quote = p.get_quoted_item();
auto *qend = &quote[quote.size()];
res.set_str(std::string_view{list, qend});
});
gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
stacked_value idv{cs, args[0].get_ident()};
2016-08-31 19:49:24 +02:00
if (!idv.has_alias()) {
res.set_int(-1);
return;
}
2016-08-31 19:49:24 +02:00
auto body = args[2].get_code();
int n = -1;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, args[1].get_str()}; p.parse();) {
++n;
2021-03-20 19:34:26 +01:00
idv.set_str(p.get_raw_item());
2016-08-31 19:49:24 +02:00
idv.push();
if (cs.run(body).get_bool()) {
2021-03-23 23:29:32 +01:00
res.set_int(integer_type(n));
2016-08-31 19:49:24 +02:00
return;
}
}
res.set_int(-1);
});
gcs.new_command("listassoc", "rse", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
stacked_value idv{cs, args[0].get_ident()};
2016-08-31 19:49:24 +02:00
if (!idv.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2016-08-31 19:49:24 +02:00
auto body = args[2].get_code();
int n = -1;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, args[1].get_str()}; p.parse();) {
++n;
2021-03-20 19:34:26 +01:00
idv.set_str(p.get_raw_item());
2016-08-31 19:49:24 +02:00
idv.push();
if (cs.run(body).get_bool()) {
2021-03-20 19:34:26 +01:00
if (p.parse()) {
res.set_str(p.get_item());
}
break;
}
2021-03-20 19:34:26 +01:00
if (!p.parse()) {
break;
2016-08-17 20:51:21 +02:00
}
}
});
gcs.new_command("listfind=", "i", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_find<integer_type>(
cs, args, res, [](list_parser const &p, integer_type val) {
2021-03-23 01:45:35 +01:00
return parse_int(p.get_raw_item()) == val;
2016-08-17 20:51:21 +02:00
}
);
});
gcs.new_command("listfind=f", "f", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_find<float_type>(
cs, args, res, [](list_parser const &p, float_type val) {
2021-03-23 01:45:35 +01:00
return parse_float(p.get_raw_item()) == val;
2016-08-17 20:51:21 +02:00
}
);
});
gcs.new_command("listfind=s", "s", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_find<std::string_view>(
cs, args, res, [](list_parser const &p, std::string_view val) {
2021-03-20 19:34:26 +01:00
return p.get_raw_item() == val;
2016-08-17 20:51:21 +02:00
}
);
});
gcs.new_command("listassoc=", "i", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_assoc<integer_type>(
cs, args, res, [](list_parser const &p, integer_type val) {
2021-03-23 01:45:35 +01:00
return parse_int(p.get_raw_item()) == val;
2016-08-17 20:51:21 +02:00
}
);
});
gcs.new_command("listassoc=f", "f", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_assoc<float_type>(
cs, args, res, [](list_parser const &p, float_type val) {
2021-03-23 01:45:35 +01:00
return parse_float(p.get_raw_item()) == val;
2016-08-17 20:51:21 +02:00
}
);
});
gcs.new_command("listassoc=s", "s", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_assoc<std::string_view>(
cs, args, res, [](list_parser const &p, std::string_view val) {
2021-03-20 19:34:26 +01:00
return p.get_raw_item() == val;
2016-08-17 20:51:21 +02:00
}
);
});
gcs.new_command("looplist", "rse", [](auto &cs, auto args, auto &) {
2021-03-23 23:29:32 +01:00
stacked_value idv{cs, args[0].get_ident()};
2016-08-31 19:49:24 +02:00
if (!idv.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2016-08-31 19:49:24 +02:00
auto body = args[2].get_code();
int n = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) {
2021-03-20 19:34:26 +01:00
idv.set_str(p.get_item());
2016-08-31 19:49:24 +02:00
idv.push();
2016-09-14 23:24:13 +02:00
switch (cs.run_loop(body)) {
2021-03-23 23:29:32 +01:00
case loop_state::BREAK:
2016-09-14 23:24:13 +02:00
goto end;
default: /* continue and normal */
break;
}
}
2016-09-14 23:24:13 +02:00
end:
return;
});
gcs.new_command("looplist2", "rrse", [](auto &cs, auto args, auto &) {
2021-03-23 23:29:32 +01:00
stacked_value idv1{cs, args[0].get_ident()};
stacked_value idv2{cs, args[1].get_ident()};
2016-08-31 19:49:24 +02:00
if (!idv1.has_alias() || !idv2.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2016-08-31 19:49:24 +02:00
auto body = args[3].get_code();
int n = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, args[2].get_str()}; p.parse(); n += 2) {
2021-03-20 19:34:26 +01:00
idv1.set_str(p.get_item());
if (p.parse()) {
idv2.set_str(p.get_item());
2016-09-15 22:34:52 +02:00
} else {
idv2.set_str("");
}
2016-08-31 19:49:24 +02:00
idv1.push();
idv2.push();
2016-09-14 23:24:13 +02:00
switch (cs.run_loop(body)) {
2021-03-23 23:29:32 +01:00
case loop_state::BREAK:
2016-09-14 23:24:13 +02:00
goto end;
default: /* continue and normal */
break;
}
}
2016-09-14 23:24:13 +02:00
end:
return;
});
gcs.new_command("looplist3", "rrrse", [](auto &cs, auto args, auto &) {
2021-03-23 23:29:32 +01:00
stacked_value idv1{cs, args[0].get_ident()};
stacked_value idv2{cs, args[1].get_ident()};
stacked_value idv3{cs, args[2].get_ident()};
2016-08-31 19:49:24 +02:00
if (!idv1.has_alias() || !idv2.has_alias() || !idv3.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2016-08-31 19:49:24 +02:00
auto body = args[4].get_code();
int n = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, args[3].get_str()}; p.parse(); n += 3) {
2021-03-20 19:34:26 +01:00
idv1.set_str(p.get_item());
if (p.parse()) {
idv2.set_str(p.get_item());
2016-09-15 22:34:52 +02:00
} else {
idv2.set_str("");
}
2021-03-20 19:34:26 +01:00
if (p.parse()) {
idv3.set_str(p.get_item());
2016-09-15 22:34:52 +02:00
} else {
idv3.set_str("");
}
2016-08-31 19:49:24 +02:00
idv1.push();
idv2.push();
idv3.push();
2016-09-14 23:24:13 +02:00
switch (cs.run_loop(body)) {
2021-03-23 23:29:32 +01:00
case loop_state::BREAK:
2016-09-14 23:24:13 +02:00
goto end;
default: /* continue and normal */
break;
}
}
2016-09-14 23:24:13 +02:00
end:
return;
});
gcs.new_command("looplistconcat", "rse", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
loop_list_conc(
2021-03-17 21:57:47 +01:00
cs, res, args[0].get_ident(), args[1].get_str(),
args[2].get_code(), true
);
});
gcs.new_command("looplistconcatword", "rse", [](
auto &cs, auto args, auto &res
2016-08-17 20:51:21 +02:00
) {
2021-03-23 23:29:32 +01:00
loop_list_conc(
2021-03-17 21:57:47 +01:00
cs, res, args[0].get_ident(), args[1].get_str(),
args[2].get_code(), false
);
});
gcs.new_command("listfilter", "rse", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
stacked_value idv{cs, args[0].get_ident()};
2016-08-31 19:49:24 +02:00
if (!idv.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2016-08-31 19:49:24 +02:00
auto body = args[2].get_code();
2021-03-23 23:29:32 +01:00
charbuf r{cs};
int n = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) {
2021-03-20 19:34:26 +01:00
idv.set_str(p.get_raw_item());
2016-08-31 19:49:24 +02:00
idv.push();
if (cs.run(body).get_bool()) {
2016-08-17 20:51:21 +02:00
if (r.size()) {
2021-03-19 02:55:59 +01:00
r.push_back(' ');
2016-08-17 20:51:21 +02:00
}
2021-03-20 19:34:26 +01:00
r.append(p.get_quoted_item());
}
}
2021-03-19 02:55:59 +01:00
res.set_str(r.str());
});
gcs.new_command("listcount", "rse", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
stacked_value idv{cs, args[0].get_ident()};
2016-08-31 19:49:24 +02:00
if (!idv.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2016-08-31 19:49:24 +02:00
auto body = args[2].get_code();
int n = 0, r = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, args[1].get_str()}; p.parse(); ++n) {
2021-03-20 19:34:26 +01:00
idv.set_str(p.get_raw_item());
2016-08-31 19:49:24 +02:00
idv.push();
if (cs.run(body).get_bool()) {
r++;
2016-08-17 20:51:21 +02:00
}
}
res.set_int(r);
});
gcs.new_command("prettylist", "ss", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
charbuf buf{cs};
std::string_view s = args[0].get_str();
std::string_view conj = args[1].get_str();
2021-03-23 23:29:32 +01:00
list_parser p{cs, s};
2021-03-20 19:34:26 +01:00
size_t len = p.count();
2017-01-25 02:09:50 +01:00
size_t n = 0;
2021-03-20 19:34:26 +01:00
for (p.set_input(s); p.parse(); ++n) {
auto qi = p.get_quoted_item();
if (!qi.empty() && (qi.front() == '"')) {
2021-03-23 23:29:32 +01:00
unescape_string(std::back_inserter(buf), p.get_raw_item());
} else {
2021-03-20 19:34:26 +01:00
buf.append(p.get_raw_item());
}
if ((n + 1) < len) {
2016-08-17 20:51:21 +02:00
if ((len > 2) || conj.empty()) {
buf.push_back(',');
2016-08-17 20:51:21 +02:00
}
if ((n + 2 == len) && !conj.empty()) {
buf.push_back(' ');
buf.append(conj);
}
buf.push_back(' ');
}
}
res.set_str(buf.str());
});
gcs.new_command("indexof", "ss", [](auto &cs, auto args, auto &res) {
res.set_int(
2021-03-23 23:29:32 +01:00
list_includes(cs, args[0].get_str(), args[1].get_str())
);
});
gcs.new_command("listdel", "ss", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_merge<false, false>(cs, args, res, std::less<int>());
2016-08-17 20:51:21 +02:00
});
gcs.new_command("listintersect", "ss", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_merge<false, false>(cs, args, res, std::greater_equal<int>());
2016-08-17 20:51:21 +02:00
});
gcs.new_command("listunion", "ss", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_merge<true, true>(cs, args, res, std::less<int>());
});
gcs.new_command("listsplice", "ssii", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
integer_type offset = std::max(args[2].get_int(), integer_type(0));
integer_type len = std::max(args[3].get_int(), integer_type(0));
std::string_view s = args[0].get_str();
std::string_view vals = args[1].get_str();
char const *list = s.data();
2021-03-23 23:29:32 +01:00
list_parser p{cs, s};
for (integer_type i = 0; i < offset; ++i) {
2021-03-20 19:34:26 +01:00
if (!p.parse()) {
break;
2016-08-17 20:51:21 +02:00
}
}
2021-03-20 19:34:26 +01:00
std::string_view quote = p.get_quoted_item();
2016-11-07 23:33:53 +01:00
char const *qend = !quote.empty() ? &quote[quote.size()] : list;
2021-03-23 23:29:32 +01:00
charbuf buf{cs};
2016-08-17 20:51:21 +02:00
if (qend > list) {
2021-03-19 02:55:59 +01:00
buf.append(list, qend);
2016-08-17 20:51:21 +02:00
}
if (!vals.empty()) {
2016-08-17 20:51:21 +02:00
if (!buf.empty()) {
2021-03-19 02:55:59 +01:00
buf.push_back(' ');
2016-08-17 20:51:21 +02:00
}
2021-03-19 02:55:59 +01:00
buf.append(vals);
}
2021-03-23 23:29:32 +01:00
for (integer_type i = 0; i < len; ++i) {
2021-03-20 19:34:26 +01:00
if (!p.parse()) {
break;
2016-08-17 20:51:21 +02:00
}
}
2021-03-20 19:34:26 +01:00
p.skip_until_item();
if (!p.get_input().empty()) {
switch (p.get_input().front()) {
2016-08-17 20:51:21 +02:00
case ')':
case ']':
break;
default:
if (!buf.empty()) {
2021-03-19 02:55:59 +01:00
buf.push_back(' ');
2016-08-17 20:51:21 +02:00
}
buf.append(p.get_input());
2016-08-17 20:51:21 +02:00
break;
}
}
2021-03-19 02:55:59 +01:00
res.set_str(buf.str());
});
2021-03-23 23:29:32 +01:00
init_lib_list_sort(gcs);
}
struct ListSortItem {
std::string_view str;
std::string_view quote;
};
struct ListSortFun {
2021-03-23 23:29:32 +01:00
state &cs;
stacked_value &xv, &yv;
bcode *body;
bool operator()(ListSortItem const &xval, ListSortItem const &yval) {
xv.set_str(xval.str);
yv.set_str(yval.str);
2016-08-31 19:49:24 +02:00
xv.push();
yv.push();
return cs.run(body).get_bool();
}
};
2021-03-23 23:29:32 +01:00
static void list_sort(
state &cs, any_value &res, std::string_view list,
ident *x, ident *y, bcode *body, bcode *unique
) {
2016-08-17 20:51:21 +02:00
if (x == y || !x->is_alias() || !y->is_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2021-03-23 23:29:32 +01:00
alias *xa = static_cast<alias *>(x), *ya = static_cast<alias *>(y);
2016-08-18 00:06:39 +02:00
2021-03-23 23:29:32 +01:00
valbuf<ListSortItem> items{cs};
2017-01-25 02:09:50 +01:00
size_t total = 0;
2021-03-23 23:29:32 +01:00
for (list_parser p{cs, list}; p.parse();) {
2021-03-20 19:34:26 +01:00
ListSortItem item = { p.get_raw_item(), p.get_quoted_item() };
2017-01-25 01:18:29 +01:00
items.push_back(item);
total += item.quote.size();
}
if (items.empty()) {
2021-03-18 23:53:16 +01:00
res.set_str(list);
return;
}
2021-03-23 23:29:32 +01:00
stacked_value xval{cs, xa}, yval{cs, ya};
2021-03-18 20:55:14 +01:00
xval.set_none();
yval.set_none();
2016-08-31 19:49:24 +02:00
xval.push();
yval.push();
2017-01-25 02:09:50 +01:00
size_t totaluniq = total;
size_t nuniq = items.size();
if (body) {
2016-08-31 19:49:24 +02:00
ListSortFun f = { cs, xval, yval, body };
std::sort(items.buf.begin(), items.buf.end(), f);
2021-03-23 23:29:32 +01:00
if (!code_is_empty(unique)) {
f.body = unique;
totaluniq = items[0].quote.size();
nuniq = 1;
2017-01-25 02:09:50 +01:00
for (size_t i = 1; i < items.size(); i++) {
ListSortItem &item = items[i];
2016-08-17 20:51:21 +02:00
if (f(items[i - 1], item)) {
item.quote = std::string_view{};
2016-08-17 20:51:21 +02:00
} else {
totaluniq += item.quote.size();
++nuniq;
}
}
}
} else {
2016-08-31 19:49:24 +02:00
ListSortFun f = { cs, xval, yval, unique };
totaluniq = items[0].quote.size();
nuniq = 1;
2017-01-25 02:09:50 +01:00
for (size_t i = 1; i < items.size(); i++) {
ListSortItem &item = items[i];
2017-01-25 02:09:50 +01:00
for (size_t j = 0; j < i; ++j) {
ListSortItem &prev = items[j];
if (!prev.quote.empty() && f(item, prev)) {
item.quote = std::string_view{};
break;
}
}
if (!item.quote.empty()) {
totaluniq += item.quote.size();
++nuniq;
}
}
}
2016-08-31 19:49:24 +02:00
xval.pop();
yval.pop();
2021-03-23 23:29:32 +01:00
charbuf sorted{cs};
2017-02-18 17:49:01 +01:00
sorted.reserve(totaluniq + std::max(nuniq - 1, size_t(0)));
2017-01-25 02:09:50 +01:00
for (size_t i = 0; i < items.size(); ++i) {
ListSortItem &item = items[i];
2016-08-17 20:51:21 +02:00
if (item.quote.empty()) {
continue;
2016-08-17 20:51:21 +02:00
}
if (i) {
2021-03-19 02:55:59 +01:00
sorted.push_back(' ');
2016-08-17 20:51:21 +02:00
}
2021-03-19 02:55:59 +01:00
sorted.append(item.quote);
}
2021-03-19 02:55:59 +01:00
res.set_str(sorted.str());
}
2021-03-23 23:29:32 +01:00
static void init_lib_list_sort(state &gcs) {
gcs.new_command("sortlist", "srree", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_sort(
2021-03-17 21:57:47 +01:00
cs, res, args[0].get_str(), args[1].get_ident(),
args[2].get_ident(), args[3].get_code(), args[4].get_code()
);
});
gcs.new_command("uniquelist", "srre", [](auto &cs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
list_sort(
2021-03-17 21:57:47 +01:00
cs, res, args[0].get_str(), args[1].get_ident(),
args[2].get_ident(), nullptr, args[3].get_code()
);
});
}
2021-03-23 23:32:25 +01:00
} /* namespace cubescript */