list lib cleanup
parent
845232605d
commit
cf21ba0a86
125
src/lib_list.cc
125
src/lib_list.cc
|
@ -55,10 +55,7 @@ static inline void cs_list_assoc(CsValueRange args, CsValue &res, F cmp) {
|
||||||
for (util::ListParser p(args[0].get_strr()); p.parse();) {
|
for (util::ListParser p(args[0].get_strr()); p.parse();) {
|
||||||
if (cmp(p, val)) {
|
if (cmp(p, val)) {
|
||||||
if (p.parse()) {
|
if (p.parse()) {
|
||||||
auto elem = p.element();
|
res.set_str(p.element());
|
||||||
auto er = elem.iter();
|
|
||||||
elem.disown();
|
|
||||||
res.set_mstr(er);
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -76,14 +73,13 @@ static void cs_loop_list_conc(
|
||||||
if (!idv.has_alias()) {
|
if (!idv.has_alias()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
CsVector<char> r;
|
CsString r;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(list); p.parse(); ++n) {
|
for (util::ListParser p(list); p.parse(); ++n) {
|
||||||
char *val = p.element().disown();
|
idv.set_str(p.element());
|
||||||
idv.set_mstr(val);
|
|
||||||
idv.push();
|
idv.push();
|
||||||
if (n && space) {
|
if (n && space) {
|
||||||
r.push(' ');
|
r += ' ';
|
||||||
}
|
}
|
||||||
CsValue v;
|
CsValue v;
|
||||||
switch (cs.run_loop(body, v)) {
|
switch (cs.run_loop(body, v)) {
|
||||||
|
@ -94,13 +90,10 @@ static void cs_loop_list_conc(
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
CsString vstr = ostd::move(v.get_str());
|
r += v.get_str();
|
||||||
r.push_n(vstr.data(), vstr.size());
|
|
||||||
}
|
}
|
||||||
end:
|
end:
|
||||||
r.push('\0');
|
res.set_str(ostd::move(r));
|
||||||
ostd::Size len = r.size();
|
|
||||||
res.set_mstr(ostd::CharRange(r.disown(), len - 1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int cs_list_includes(ostd::ConstCharRange list, ostd::ConstCharRange needle) {
|
int cs_list_includes(ostd::ConstCharRange list, ostd::ConstCharRange needle) {
|
||||||
|
@ -118,9 +111,9 @@ template<bool PushList, bool Swap, typename F>
|
||||||
static inline void cs_list_merge(CsValueRange args, CsValue &res, F cmp) {
|
static inline void cs_list_merge(CsValueRange args, CsValue &res, F cmp) {
|
||||||
ostd::ConstCharRange list = args[0].get_strr();
|
ostd::ConstCharRange list = args[0].get_strr();
|
||||||
ostd::ConstCharRange elems = args[1].get_strr();
|
ostd::ConstCharRange elems = args[1].get_strr();
|
||||||
CsVector<char> buf;
|
CsString buf;
|
||||||
if (PushList) {
|
if (PushList) {
|
||||||
buf.push_n(list.data(), list.size());
|
buf += list;
|
||||||
}
|
}
|
||||||
if (Swap) {
|
if (Swap) {
|
||||||
ostd::swap(list, elems);
|
ostd::swap(list, elems);
|
||||||
|
@ -128,14 +121,12 @@ static inline void cs_list_merge(CsValueRange args, CsValue &res, F cmp) {
|
||||||
for (util::ListParser p(list); p.parse();) {
|
for (util::ListParser p(list); p.parse();) {
|
||||||
if (cmp(cs_list_includes(elems, p.item), 0)) {
|
if (cmp(cs_list_includes(elems, p.item), 0)) {
|
||||||
if (!buf.empty()) {
|
if (!buf.empty()) {
|
||||||
buf.push(' ');
|
buf += ' ';
|
||||||
}
|
}
|
||||||
buf.push_n(p.quote.data(), p.quote.size());
|
buf += p.quote;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf.push('\0');
|
res.set_str(ostd::move(buf));
|
||||||
ostd::Size len = buf.size() - 1;
|
|
||||||
res.set_mstr(ostd::CharRange(buf.disown(), len));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cs_init_lib_list_sort(CsState &cs);
|
static void cs_init_lib_list_sort(CsState &cs);
|
||||||
|
@ -164,10 +155,7 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
p.item = p.quote = ostd::ConstCharRange();
|
p.item = p.quote = ostd::ConstCharRange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto elem = p.element();
|
res.set_str(p.element());
|
||||||
auto er = p.element().iter();
|
|
||||||
elem.disown();
|
|
||||||
res.set_mstr(er);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
gcs.new_command("sublist", "siiN", [](auto &, auto args, auto &res) {
|
gcs.new_command("sublist", "siiN", [](auto &, auto args, auto &res) {
|
||||||
|
@ -209,7 +197,7 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
int n = -1;
|
int n = -1;
|
||||||
for (util::ListParser p(args[1].get_strr()); p.parse();) {
|
for (util::ListParser p(args[1].get_strr()); p.parse();) {
|
||||||
++n;
|
++n;
|
||||||
idv.set_mstr(cs_dup_ostr(p.item));
|
idv.set_str(p.item);
|
||||||
idv.push();
|
idv.push();
|
||||||
if (cs.run_bool(body)) {
|
if (cs.run_bool(body)) {
|
||||||
res.set_int(CsInt(n));
|
res.set_int(CsInt(n));
|
||||||
|
@ -228,14 +216,11 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
int n = -1;
|
int n = -1;
|
||||||
for (util::ListParser p(args[1].get_strr()); p.parse();) {
|
for (util::ListParser p(args[1].get_strr()); p.parse();) {
|
||||||
++n;
|
++n;
|
||||||
idv.set_mstr(cs_dup_ostr(p.item));
|
idv.set_str(p.item);
|
||||||
idv.push();
|
idv.push();
|
||||||
if (cs.run_bool(body)) {
|
if (cs.run_bool(body)) {
|
||||||
if (p.parse()) {
|
if (p.parse()) {
|
||||||
auto elem = p.element();
|
res.set_str(p.element());
|
||||||
auto er = elem.iter();
|
|
||||||
elem.disown();
|
|
||||||
res.set_mstr(er);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -297,7 +282,7 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
auto body = args[2].get_code();
|
auto body = args[2].get_code();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
||||||
idv.set_mstr(p.element().disown());
|
idv.set_str(p.element());
|
||||||
idv.push();
|
idv.push();
|
||||||
switch (cs.run_loop(body)) {
|
switch (cs.run_loop(body)) {
|
||||||
case CsLoopState::Break:
|
case CsLoopState::Break:
|
||||||
|
@ -318,8 +303,12 @@ end:
|
||||||
auto body = args[3].get_code();
|
auto body = args[3].get_code();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(args[2].get_strr()); p.parse(); n += 2) {
|
for (util::ListParser p(args[2].get_strr()); p.parse(); n += 2) {
|
||||||
idv1.set_mstr(p.element().disown());
|
idv1.set_str(p.element());
|
||||||
idv2.set_mstr(p.parse() ? p.element().disown() : cs_dup_ostr(""));
|
if (p.parse()) {
|
||||||
|
idv2.set_str(p.element());
|
||||||
|
} else {
|
||||||
|
idv2.set_str("");
|
||||||
|
}
|
||||||
idv1.push();
|
idv1.push();
|
||||||
idv2.push();
|
idv2.push();
|
||||||
switch (cs.run_loop(body)) {
|
switch (cs.run_loop(body)) {
|
||||||
|
@ -343,9 +332,17 @@ end:
|
||||||
auto body = args[4].get_code();
|
auto body = args[4].get_code();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(args[3].get_strr()); p.parse(); n += 3) {
|
for (util::ListParser p(args[3].get_strr()); p.parse(); n += 3) {
|
||||||
idv1.set_mstr(p.element().disown());
|
idv1.set_str(p.element());
|
||||||
idv2.set_mstr(p.parse() ? p.element().disown() : cs_dup_ostr(""));
|
if (p.parse()) {
|
||||||
idv3.set_mstr(p.parse() ? p.element().disown() : cs_dup_ostr(""));
|
idv2.set_str(p.element());
|
||||||
|
} else {
|
||||||
|
idv2.set_str("");
|
||||||
|
}
|
||||||
|
if (p.parse()) {
|
||||||
|
idv3.set_str(p.element());
|
||||||
|
} else {
|
||||||
|
idv3.set_str("");
|
||||||
|
}
|
||||||
idv1.push();
|
idv1.push();
|
||||||
idv2.push();
|
idv2.push();
|
||||||
idv3.push();
|
idv3.push();
|
||||||
|
@ -382,22 +379,19 @@ end:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto body = args[2].get_code();
|
auto body = args[2].get_code();
|
||||||
CsVector<char> r;
|
CsString r;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
||||||
char *val = cs_dup_ostr(p.item);
|
idv.set_str(p.item);
|
||||||
idv.set_mstr(val);
|
|
||||||
idv.push();
|
idv.push();
|
||||||
if (cs.run_bool(body)) {
|
if (cs.run_bool(body)) {
|
||||||
if (r.size()) {
|
if (r.size()) {
|
||||||
r.push(' ');
|
r += ' ';
|
||||||
}
|
}
|
||||||
r.push_n(p.quote.data(), p.quote.size());
|
r += p.quote;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.push('\0');
|
res.set_str(ostd::move(r));
|
||||||
ostd::Size len = r.size() - 1;
|
|
||||||
res.set_mstr(ostd::CharRange(r.disown(), len));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
gcs.new_command("listcount", "rse", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("listcount", "rse", [](auto &cs, auto args, auto &res) {
|
||||||
|
@ -408,8 +402,7 @@ end:
|
||||||
auto body = args[2].get_code();
|
auto body = args[2].get_code();
|
||||||
int n = 0, r = 0;
|
int n = 0, r = 0;
|
||||||
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
for (util::ListParser p(args[1].get_strr()); p.parse(); ++n) {
|
||||||
char *val = cs_dup_ostr(p.item);
|
idv.set_str(p.item);
|
||||||
idv.set_mstr(val);
|
|
||||||
idv.push();
|
idv.push();
|
||||||
if (cs.run_bool(body)) {
|
if (cs.run_bool(body)) {
|
||||||
r++;
|
r++;
|
||||||
|
@ -419,37 +412,29 @@ end:
|
||||||
});
|
});
|
||||||
|
|
||||||
gcs.new_command("prettylist", "ss", [](auto &, auto args, auto &res) {
|
gcs.new_command("prettylist", "ss", [](auto &, auto args, auto &res) {
|
||||||
CsVector<char> buf;
|
auto buf = ostd::appender<CsString>();
|
||||||
ostd::ConstCharRange s = args[0].get_strr();
|
ostd::ConstCharRange s = args[0].get_strr();
|
||||||
ostd::ConstCharRange conj = args[1].get_strr();
|
ostd::ConstCharRange conj = args[1].get_strr();
|
||||||
ostd::Size len = util::list_length(s);
|
ostd::Size len = util::list_length(s);
|
||||||
ostd::Size n = 0;
|
ostd::Size n = 0;
|
||||||
for (util::ListParser p(s); p.parse(); ++n) {
|
for (util::ListParser p(s); p.parse(); ++n) {
|
||||||
if (!p.quote.empty() && (p.quote.front() == '"')) {
|
if (!p.quote.empty() && (p.quote.front() == '"')) {
|
||||||
buf.reserve(buf.size() + p.item.size());
|
util::unescape_string(buf, p.item);
|
||||||
auto writer = ostd::CharRange(
|
|
||||||
&buf[buf.size()], buf.capacity() - buf.size()
|
|
||||||
);
|
|
||||||
ostd::Size adv = util::unescape_string(writer, p.item);
|
|
||||||
writer.put('\0');
|
|
||||||
buf.advance(adv);
|
|
||||||
} else {
|
} else {
|
||||||
buf.push_n(p.item.data(), p.item.size());
|
buf.put_n(p.item.data(), p.item.size());
|
||||||
}
|
}
|
||||||
if ((n + 1) < len) {
|
if ((n + 1) < len) {
|
||||||
if ((len > 2) || conj.empty()) {
|
if ((len > 2) || conj.empty()) {
|
||||||
buf.push(',');
|
buf.put(',');
|
||||||
}
|
}
|
||||||
if ((n + 2 == len) && !conj.empty()) {
|
if ((n + 2 == len) && !conj.empty()) {
|
||||||
buf.push(' ');
|
buf.put(' ');
|
||||||
buf.push_n(conj.data(), conj.size());
|
buf.put_n(conj.data(), conj.size());
|
||||||
}
|
}
|
||||||
buf.push(' ');
|
buf.put(' ');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf.push('\0');
|
res.set_str(ostd::move(buf.get()));
|
||||||
ostd::Size slen = buf.size() - 1;
|
|
||||||
res.set_mstr(ostd::CharRange(buf.disown(), slen));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
gcs.new_command("indexof", "ss", [](auto &, auto args, auto &res) {
|
gcs.new_command("indexof", "ss", [](auto &, auto args, auto &res) {
|
||||||
|
@ -481,15 +466,15 @@ end:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list;
|
char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list;
|
||||||
CsVector<char> buf;
|
CsString buf;
|
||||||
if (qend > list) {
|
if (qend > list) {
|
||||||
buf.push_n(list, qend - list);
|
buf += ostd::ConstCharRange(list, qend - list);
|
||||||
}
|
}
|
||||||
if (!vals.empty()) {
|
if (!vals.empty()) {
|
||||||
if (!buf.empty()) {
|
if (!buf.empty()) {
|
||||||
buf.push(' ');
|
buf += ' ';
|
||||||
}
|
}
|
||||||
buf.push_n(vals.data(), vals.size());
|
buf += vals;
|
||||||
}
|
}
|
||||||
for (CsInt i = 0; i < len; ++i) {
|
for (CsInt i = 0; i < len; ++i) {
|
||||||
if (!p.parse()) {
|
if (!p.parse()) {
|
||||||
|
@ -504,15 +489,13 @@ end:
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (!buf.empty()) {
|
if (!buf.empty()) {
|
||||||
buf.push(' ');
|
buf += ' ';
|
||||||
}
|
}
|
||||||
buf.push_n(p.input.data(), p.input.size());
|
buf += p.input;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf.push('\0');
|
res.set_str(ostd::move(buf));
|
||||||
ostd::Size slen = buf.size() - 1;
|
|
||||||
res.set_mstr(ostd::CharRange(buf.disown(), slen));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
cs_init_lib_list_sort(gcs);
|
cs_init_lib_list_sort(gcs);
|
||||||
|
|
Loading…
Reference in New Issue