libcubescript/src/lib_list.cc

637 lines
18 KiB
C++
Raw Normal View History

2017-02-08 01:07:35 +01:00
#include <functional>
2017-06-20 21:21:39 +02:00
#include <cubescript/cubescript.hh>
#include "cs_util.hh"
namespace cscript {
2016-08-17 20:51:21 +02:00
template<typename T>
2018-04-27 23:53:55 +02:00
struct cs_arg_val;
2016-08-17 20:51:21 +02:00
template<>
2018-04-27 23:53:55 +02:00
struct cs_arg_val<cs_int> {
2017-02-13 18:10:40 +01:00
static cs_int get(cs_value &tv) {
2016-08-17 20:51:21 +02:00
return tv.get_int();
}
};
template<>
2018-04-27 23:53:55 +02:00
struct cs_arg_val<cs_float> {
2017-02-13 18:10:40 +01:00
static cs_float get(cs_value &tv) {
2016-08-17 20:51:21 +02:00
return tv.get_float();
}
};
template<>
2018-04-27 23:53:55 +02:00
struct cs_arg_val<ostd::string_range> {
2017-02-16 19:07:22 +01:00
static ostd::string_range get(cs_value &tv) {
2016-08-17 20:51:21 +02:00
return tv.get_strr();
}
};
template<typename T, typename F>
static inline void cs_list_find(
2017-02-13 18:10:40 +01:00
cs_state &cs, cs_value_r args, cs_value &res, F cmp
) {
2017-02-13 18:10:40 +01:00
cs_int n = 0, skip = args[2].get_int();
2018-04-27 23:53:55 +02:00
T val = cs_arg_val<T>::get(args[1]);
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[0].get_strr()); 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) {
if (!p.parse()) {
goto notfound;
}
++n;
}
}
notfound:
res.set_int(-1);
}
template<typename T, typename F>
static inline void cs_list_assoc(
2017-02-13 18:10:40 +01:00
cs_state &cs, cs_value_r args, cs_value &res, F cmp
) {
2018-04-27 23:53:55 +02:00
T val = cs_arg_val<T>::get(args[1]);
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[0].get_strr()); p.parse();) {
2016-08-17 20:51:21 +02:00
if (cmp(p, val)) {
if (p.parse()) {
2016-11-07 23:33:53 +01:00
res.set_str(p.get_item());
2016-08-17 20:51:21 +02:00
}
return;
}
if (!p.parse()) {
break;
}
}
}
static void cs_loop_list_conc(
2017-02-16 19:07:22 +01:00
cs_state &cs, cs_value &res, cs_ident *id, ostd::string_range list,
2017-02-13 18:10:40 +01:00
cs_bcode *body, bool space
) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv{id};
2016-08-31 19:49:24 +02:00
if (!idv.has_alias()) {
return;
2016-08-17 20:51:21 +02:00
}
2017-02-13 18:10:40 +01:00
cs_string r;
int n = 0;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, list); p.parse(); ++n) {
2016-11-07 23:33:53 +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) {
2016-09-15 22:34:52 +02:00
r += ' ';
2016-08-17 20:51:21 +02:00
}
2017-02-13 18:10:40 +01:00
cs_value v;
2016-09-14 23:24:13 +02:00
switch (cs.run_loop(body, v)) {
case CsLoopState::Break:
goto end;
case CsLoopState::Continue:
continue;
default:
break;
}
2016-09-15 22:34:52 +02:00
r += v.get_str();
}
2016-09-14 23:24:13 +02:00
end:
2017-01-25 01:57:33 +01:00
res.set_str(std::move(r));
}
int cs_list_includes(
2017-02-16 19:07:22 +01:00
cs_state &cs, ostd::string_range list, ostd::string_range needle
) {
int offset = 0;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, list); p.parse();) {
2016-11-07 23:33:53 +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>
static inline void cs_list_merge(
2017-02-13 18:10:40 +01:00
cs_state &cs, cs_value_r args, cs_value &res, F cmp
) {
2017-02-16 19:07:22 +01:00
ostd::string_range list = args[0].get_strr();
ostd::string_range elems = args[1].get_strr();
2017-02-13 18:10:40 +01:00
cs_string buf;
2016-08-17 20:51:21 +02:00
if (PushList) {
2016-09-15 22:34:52 +02:00
buf += 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
}
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, list); p.parse();) {
2016-11-07 23:33:53 +01:00
if (cmp(cs_list_includes(cs, elems, p.get_raw_item()), 0)) {
2016-08-17 20:52:18 +02:00
if (!buf.empty()) {
2016-09-15 22:34:52 +02:00
buf += ' ';
2016-08-17 20:52:18 +02:00
}
2016-11-07 23:33:53 +01:00
buf += p.get_raw_item(true);
2016-08-17 20:51:21 +02:00
}
}
2017-01-25 01:57:33 +01:00
res.set_str(std::move(buf));
2016-08-17 20:51:21 +02:00
}
2017-02-13 18:10:40 +01:00
static void cs_init_lib_list_sort(cs_state &cs);
2017-02-13 18:10:40 +01:00
void cs_init_lib_list(cs_state &gcs) {
gcs.new_command("listlen", "s", [](auto &cs, auto args, auto &res) {
2018-04-25 01:49:58 +02:00
res.set_int(cs_int(util::list_parser(cs, args[0].get_strr()).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
}
2017-02-13 18:10:40 +01:00
cs_string str = std::move(args[0].get_str());
2018-04-25 01:49:58 +02:00
util::list_parser p(cs, str);
2016-11-07 23:33:53 +01:00
p.get_raw_item() = str;
2017-01-25 02:09:50 +01:00
for (size_t i = 1; i < args.size(); ++i) {
2016-10-27 00:49:16 +02:00
p.get_input() = str;
2017-02-13 18:10:40 +01:00
cs_int pos = args[i].get_int();
2016-08-17 20:51:21 +02:00
for (; pos > 0; --pos) {
if (!p.parse()) {
break;
}
}
if (pos > 0 || !p.parse()) {
2017-02-16 19:07:22 +01:00
p.get_raw_item() = p.get_raw_item(true) = ostd::string_range();
2016-08-17 20:51:21 +02:00
}
}
2016-11-07 23:33:53 +01:00
res.set_str(p.get_item());
});
gcs.new_command("sublist", "siiN", [](auto &cs, auto args, auto &res) {
2017-02-13 18:10:40 +01:00
cs_int 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();
2017-02-18 17:49:01 +01:00
cs_int offset = std::max(skip, cs_int(0)),
len = (numargs >= 3) ? std::max(count, cs_int(0)) : -1;
2018-04-25 01:49:58 +02:00
util::list_parser p(cs, args[0].get_strr());
2017-02-13 18:10:40 +01:00
for (cs_int i = 0; i < offset; ++i) {
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) {
p.skip();
2016-08-17 20:51:21 +02:00
}
2017-02-13 18:10:40 +01:00
res.set_str(cs_string{p.get_input()});
return;
}
2016-10-27 00:49:16 +02:00
char const *list = p.get_input().data();
2017-02-16 19:07:22 +01:00
p.get_raw_item(true) = ostd::string_range();
2016-08-17 20:51:21 +02:00
if (len > 0 && p.parse()) {
while (--len > 0 && p.parse());
2016-08-17 20:51:21 +02:00
}
2017-02-16 19:07:22 +01:00
ostd::string_range quote = p.get_raw_item(true);
2016-11-07 23:33:53 +01:00
char const *qend = !quote.empty() ? &quote[quote.size()] : list;
2017-02-13 18:10:40 +01:00
res.set_str(cs_string{list, size_t(qend - list)});
});
gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv{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;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[1].get_strr()); p.parse();) {
++n;
2017-02-13 18:10:40 +01:00
idv.set_str(cs_string{p.get_raw_item()});
2016-08-31 19:49:24 +02:00
idv.push();
if (cs.run_bool(body)) {
2017-02-13 18:10:40 +01:00
res.set_int(cs_int(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) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv{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;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[1].get_strr()); p.parse();) {
++n;
2017-02-13 18:10:40 +01:00
idv.set_str(cs_string{p.get_raw_item()});
2016-08-31 19:49:24 +02:00
idv.push();
if (cs.run_bool(body)) {
if (p.parse()) {
2016-11-07 23:33:53 +01:00
res.set_str(p.get_item());
}
break;
}
2016-08-17 20:51:21 +02:00
if (!p.parse()) {
break;
2016-08-17 20:51:21 +02:00
}
}
});
gcs.new_command("listfind=", "i", [](auto &cs, auto args, auto &res) {
2017-02-13 18:10:40 +01:00
cs_list_find<cs_int>(
2018-04-25 01:49:58 +02:00
cs, args, res, [](const util::list_parser &p, cs_int val) {
2016-11-07 23:33:53 +01:00
return cs_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) {
2017-02-13 18:10:40 +01:00
cs_list_find<cs_float>(
2018-04-25 01:49:58 +02:00
cs, args, res, [](const util::list_parser &p, cs_float val) {
2016-11-07 23:33:53 +01:00
return cs_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) {
2017-02-16 19:07:22 +01:00
cs_list_find<ostd::string_range>(
2018-04-25 01:49:58 +02:00
cs, args, res, [](const util::list_parser &p, ostd::string_range val) {
2016-11-07 23:33:53 +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) {
2017-02-13 18:10:40 +01:00
cs_list_assoc<cs_int>(
2018-04-25 01:49:58 +02:00
cs, args, res, [](const util::list_parser &p, cs_int val) {
2016-11-07 23:33:53 +01:00
return cs_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) {
2017-02-13 18:10:40 +01:00
cs_list_assoc<cs_float>(
2018-04-25 01:49:58 +02:00
cs, args, res, [](const util::list_parser &p, cs_float val) {
2016-11-07 23:33:53 +01:00
return cs_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) {
2017-02-16 19:07:22 +01:00
cs_list_assoc<ostd::string_range>(
2018-04-25 01:49:58 +02:00
cs, args, res, [](const util::list_parser &p, ostd::string_range val) {
2016-11-07 23:33:53 +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 &) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv{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;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[1].get_strr()); p.parse(); ++n) {
2016-11-07 23:33:53 +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)) {
case CsLoopState::Break:
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 &) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv1{args[0].get_ident()}, idv2{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;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[2].get_strr()); p.parse(); n += 2) {
2016-11-07 23:33:53 +01:00
idv1.set_str(p.get_item());
2016-09-15 22:34:52 +02:00
if (p.parse()) {
2016-11-07 23:33:53 +01:00
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)) {
case CsLoopState::Break:
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 &) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv1{args[0].get_ident()};
cs_stacked_value idv2{args[1].get_ident()};
cs_stacked_value idv3{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;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[3].get_strr()); p.parse(); n += 3) {
2016-11-07 23:33:53 +01:00
idv1.set_str(p.get_item());
2016-09-15 22:34:52 +02:00
if (p.parse()) {
2016-11-07 23:33:53 +01:00
idv2.set_str(p.get_item());
2016-09-15 22:34:52 +02:00
} else {
idv2.set_str("");
}
if (p.parse()) {
2016-11-07 23:33:53 +01:00
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)) {
case CsLoopState::Break:
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) {
cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(),
args[2].get_code(), true
);
});
gcs.new_command("looplistconcatword", "rse", [](
auto &cs, auto args, auto &res
2016-08-17 20:51:21 +02:00
) {
cs_loop_list_conc(
cs, res, args[0].get_ident(), args[1].get_strr(),
args[2].get_code(), false
);
});
gcs.new_command("listfilter", "rse", [](auto &cs, auto args, auto &res) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv{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();
2017-02-13 18:10:40 +01:00
cs_string r;
int n = 0;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[1].get_strr()); p.parse(); ++n) {
2017-02-13 18:10:40 +01:00
idv.set_str(cs_string{p.get_raw_item()});
2016-08-31 19:49:24 +02:00
idv.push();
if (cs.run_bool(body)) {
2016-08-17 20:51:21 +02:00
if (r.size()) {
2016-09-15 22:34:52 +02:00
r += ' ';
2016-08-17 20:51:21 +02:00
}
2016-11-07 23:33:53 +01:00
r += p.get_raw_item(true);
}
}
2017-01-25 01:57:33 +01:00
res.set_str(std::move(r));
});
gcs.new_command("listcount", "rse", [](auto &cs, auto args, auto &res) {
2017-02-13 18:10:40 +01:00
cs_stacked_value idv{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;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, args[1].get_strr()); p.parse(); ++n) {
2017-02-13 18:10:40 +01:00
idv.set_str(cs_string{p.get_raw_item()});
2016-08-31 19:49:24 +02:00
idv.push();
2016-08-17 20:51:21 +02:00
if (cs.run_bool(body)) {
r++;
2016-08-17 20:51:21 +02:00
}
}
res.set_int(r);
});
gcs.new_command("prettylist", "ss", [](auto &cs, auto args, auto &res) {
2017-04-17 17:13:14 +02:00
auto buf = ostd::appender<cs_string>();
2017-02-16 19:07:22 +01:00
ostd::string_range s = args[0].get_strr();
ostd::string_range conj = args[1].get_strr();
2018-04-25 01:49:58 +02:00
size_t len = util::list_parser(cs, s).count();
2017-01-25 02:09:50 +01:00
size_t n = 0;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, s); p.parse(); ++n) {
2016-11-07 23:33:53 +01:00
if (!p.get_raw_item(true).empty() &&
(p.get_raw_item(true).front() == '"')) {
util::unescape_string(buf, p.get_raw_item());
} else {
2017-02-19 18:13:51 +01:00
ostd::range_put_all(buf, p.get_raw_item());
}
if ((n + 1) < len) {
2016-08-17 20:51:21 +02:00
if ((len > 2) || conj.empty()) {
2016-09-15 22:34:52 +02:00
buf.put(',');
2016-08-17 20:51:21 +02:00
}
if ((n + 2 == len) && !conj.empty()) {
2016-09-15 22:34:52 +02:00
buf.put(' ');
2017-02-19 18:13:51 +01:00
ostd::range_put_all(buf, conj);
}
2016-09-15 22:34:52 +02:00
buf.put(' ');
}
}
2017-01-25 01:57:33 +01:00
res.set_str(std::move(buf.get()));
});
gcs.new_command("indexof", "ss", [](auto &cs, auto args, auto &res) {
res.set_int(
cs_list_includes(cs, args[0].get_strr(), args[1].get_strr())
);
});
gcs.new_command("listdel", "ss", [](auto &cs, auto args, auto &res) {
2017-02-08 01:07:35 +01:00
cs_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) {
2017-02-08 01:07:35 +01:00
cs_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) {
2017-02-08 01:07:35 +01:00
cs_list_merge<true, true>(cs, args, res, std::less<int>());
});
gcs.new_command("listsplice", "ssii", [](auto &cs, auto args, auto &res) {
2017-02-18 17:49:01 +01:00
cs_int offset = std::max(args[2].get_int(), cs_int(0));
cs_int len = std::max(args[3].get_int(), cs_int(0));
2017-02-16 19:07:22 +01:00
ostd::string_range s = args[0].get_strr();
ostd::string_range vals = args[1].get_strr();
char const *list = s.data();
2018-04-25 01:49:58 +02:00
util::list_parser p(cs, s);
2017-02-13 18:10:40 +01:00
for (cs_int i = 0; i < offset; ++i) {
2016-08-17 20:51:21 +02:00
if (!p.parse()) {
break;
2016-08-17 20:51:21 +02:00
}
}
2017-02-16 19:07:22 +01:00
ostd::string_range quote = p.get_raw_item(true);
2016-11-07 23:33:53 +01:00
char const *qend = !quote.empty() ? &quote[quote.size()] : list;
2017-02-13 18:10:40 +01:00
cs_string buf;
2016-08-17 20:51:21 +02:00
if (qend > list) {
2017-02-16 19:07:22 +01:00
buf += ostd::string_range(list, qend);
2016-08-17 20:51:21 +02:00
}
if (!vals.empty()) {
2016-08-17 20:51:21 +02:00
if (!buf.empty()) {
2016-09-15 22:34:52 +02:00
buf += ' ';
2016-08-17 20:51:21 +02:00
}
2016-09-15 22:34:52 +02:00
buf += vals;
}
2017-02-13 18:10:40 +01:00
for (cs_int i = 0; i < len; ++i) {
2016-08-17 20:51:21 +02:00
if (!p.parse()) {
break;
2016-08-17 20:51:21 +02:00
}
}
p.skip();
2016-10-27 00:49:16 +02:00
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()) {
2016-09-15 22:34:52 +02:00
buf += ' ';
2016-08-17 20:51:21 +02:00
}
2016-10-27 00:49:16 +02:00
buf += p.get_input();
2016-08-17 20:51:21 +02:00
break;
}
}
2017-01-25 01:57:33 +01:00
res.set_str(std::move(buf));
});
cs_init_lib_list_sort(gcs);
}
struct ListSortItem {
2017-02-16 19:07:22 +01:00
ostd::string_range str;
ostd::string_range quote;
};
struct ListSortFun {
2017-02-13 18:10:40 +01:00
cs_state &cs;
cs_stacked_value &xv, &yv;
cs_bcode *body;
bool operator()(ListSortItem const &xval, ListSortItem const &yval) {
2016-08-31 19:49:24 +02:00
xv.set_cstr(xval.str);
yv.set_cstr(yval.str);
xv.push();
yv.push();
return cs.run_bool(body);
}
};
static void cs_list_sort(
2017-02-16 19:07:22 +01:00
cs_state &cs, cs_value &res, ostd::string_range list,
2017-02-13 18:10:40 +01:00
cs_ident *x, cs_ident *y, cs_bcode *body, cs_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
}
2017-02-13 18:10:40 +01:00
cs_alias *xa = static_cast<cs_alias *>(x), *ya = static_cast<cs_alias *>(y);
2016-08-18 00:06:39 +02:00
2018-04-27 23:53:55 +02:00
cs_vector<ListSortItem> items;
2017-01-25 02:09:50 +01:00
size_t total = 0;
2018-04-25 01:49:58 +02:00
for (util::list_parser p(cs, list); p.parse();) {
2016-11-07 23:33:53 +01:00
ListSortItem item = { p.get_raw_item(), p.get_raw_item(true) };
2017-01-25 01:18:29 +01:00
items.push_back(item);
total += item.quote.size();
}
if (items.empty()) {
2017-02-13 18:10:40 +01:00
res.set_str(cs_string{list});
return;
}
2017-02-13 18:10:40 +01:00
cs_stacked_value xval{xa}, yval{ya};
2016-08-31 19:49:24 +02:00
xval.set_null();
yval.set_null();
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 };
2017-01-25 01:18:29 +01:00
ostd::sort_cmp(ostd::iter(items), f);
2016-08-29 19:17:11 +02:00
if (!cs_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 = nullptr;
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 = nullptr;
break;
}
}
if (!item.quote.empty()) {
totaluniq += item.quote.size();
++nuniq;
}
}
}
2016-08-31 19:49:24 +02:00
xval.pop();
yval.pop();
2017-02-13 18:10:40 +01:00
cs_string sorted;
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) {
2016-09-15 22:45:28 +02:00
sorted += ' ';
2016-08-17 20:51:21 +02:00
}
2016-09-15 22:45:28 +02:00
sorted += item.quote;
}
2017-01-25 01:57:33 +01:00
res.set_str(std::move(sorted));
}
2017-02-13 18:10:40 +01:00
static void cs_init_lib_list_sort(cs_state &gcs) {
gcs.new_command("sortlist", "srree", [](auto &cs, auto args, auto &res) {
cs_list_sort(
cs, res, args[0].get_strr(), 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) {
cs_list_sort(
cs, res, args[0].get_strr(), args[1].get_ident(),
args[2].get_ident(), nullptr, args[3].get_code()
);
});
}
} /* namespace cscript */