clean up ListParser api
parent
da37ff51d1
commit
4f7c05a60d
|
@ -757,9 +757,6 @@ namespace util {
|
||||||
);
|
);
|
||||||
|
|
||||||
struct OSTD_EXPORT ListParser {
|
struct OSTD_EXPORT ListParser {
|
||||||
ostd::ConstCharRange quote = ostd::ConstCharRange();
|
|
||||||
ostd::ConstCharRange item = ostd::ConstCharRange();
|
|
||||||
|
|
||||||
ListParser() = delete;
|
ListParser() = delete;
|
||||||
ListParser(CsState &cs, ostd::ConstCharRange src):
|
ListParser(CsState &cs, ostd::ConstCharRange src):
|
||||||
p_state(cs), p_input(src)
|
p_state(cs), p_input(src)
|
||||||
|
@ -771,25 +768,35 @@ namespace util {
|
||||||
ostd::Size count();
|
ostd::Size count();
|
||||||
|
|
||||||
template<typename R>
|
template<typename R>
|
||||||
ostd::Size get_element(R &&writer) const {
|
ostd::Size get_item(R &&writer) const {
|
||||||
if (!quote.empty() && (*quote == '"')) {
|
if (!p_quote.empty() && (*p_quote == '"')) {
|
||||||
return unescape_string(ostd::forward<R>(writer), item);
|
return unescape_string(ostd::forward<R>(writer), p_item);
|
||||||
} else {
|
} else {
|
||||||
return writer.put_n(item.data(), item.size());
|
return writer.put_n(p_item.data(), p_item.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CsString get_element() const {
|
CsString get_item() const {
|
||||||
auto app = ostd::appender<CsString>();
|
auto app = ostd::appender<CsString>();
|
||||||
get_element(app);
|
get_item(app);
|
||||||
return ostd::move(app.get());
|
return ostd::move(app.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ostd::ConstCharRange &get_raw_item(bool quoted = false) {
|
||||||
|
return quoted ? p_quote : p_item;
|
||||||
|
}
|
||||||
|
|
||||||
|
ostd::ConstCharRange const &get_raw_item(bool quoted = false) const {
|
||||||
|
return quoted ? p_quote : p_item;
|
||||||
|
}
|
||||||
|
|
||||||
ostd::ConstCharRange &get_input() {
|
ostd::ConstCharRange &get_input() {
|
||||||
return p_input;
|
return p_input;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
ostd::ConstCharRange p_quote = ostd::ConstCharRange();
|
||||||
|
ostd::ConstCharRange p_item = ostd::ConstCharRange();
|
||||||
CsState &p_state;
|
CsState &p_state;
|
||||||
ostd::ConstCharRange p_input;
|
ostd::ConstCharRange p_input;
|
||||||
};
|
};
|
||||||
|
|
|
@ -303,17 +303,17 @@ end:
|
||||||
}
|
}
|
||||||
switch (*p_input) {
|
switch (*p_input) {
|
||||||
case '"':
|
case '"':
|
||||||
quote = p_input;
|
p_quote = p_input;
|
||||||
p_input = parse_string(p_state, p_input);
|
p_input = parse_string(p_state, p_input);
|
||||||
quote = ostd::slice_until(quote, p_input);
|
p_quote = ostd::slice_until(p_quote, p_input);
|
||||||
item = quote.slice(1, quote.size() - 1);
|
p_item = p_quote.slice(1, p_quote.size() - 1);
|
||||||
break;
|
break;
|
||||||
case '(':
|
case '(':
|
||||||
case '[': {
|
case '[': {
|
||||||
quote = p_input;
|
p_quote = p_input;
|
||||||
++p_input;
|
++p_input;
|
||||||
item = p_input;
|
p_item = p_input;
|
||||||
char btype = *quote;
|
char btype = *p_quote;
|
||||||
int brak = 1;
|
int brak = 1;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
p_input = ostd::find_one_of(
|
p_input = ostd::find_one_of(
|
||||||
|
@ -350,9 +350,9 @@ end:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
endblock:
|
endblock:
|
||||||
item = ostd::slice_until(item, p_input);
|
p_item = ostd::slice_until(p_item, p_input);
|
||||||
item.pop_back();
|
p_item.pop_back();
|
||||||
quote = ostd::slice_until(quote, p_input);
|
p_quote = ostd::slice_until(p_quote, p_input);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ')':
|
case ')':
|
||||||
|
@ -360,7 +360,7 @@ endblock:
|
||||||
return false;
|
return false;
|
||||||
default: {
|
default: {
|
||||||
ostd::ConstCharRange e = parse_word(p_state, p_input);
|
ostd::ConstCharRange e = parse_word(p_state, p_input);
|
||||||
quote = item = ostd::slice_until(p_input, e);
|
p_quote = p_item = ostd::slice_until(p_input, e);
|
||||||
p_input = e;
|
p_input = e;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ static inline void cs_list_assoc(
|
||||||
for (util::ListParser p(cs, args[0].get_strr()); p.parse();) {
|
for (util::ListParser p(cs, args[0].get_strr()); p.parse();) {
|
||||||
if (cmp(p, val)) {
|
if (cmp(p, val)) {
|
||||||
if (p.parse()) {
|
if (p.parse()) {
|
||||||
res.set_str(p.get_element());
|
res.set_str(p.get_item());
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -78,7 +78,7 @@ static void cs_loop_list_conc(
|
||||||
CsString r;
|
CsString r;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(cs, list); p.parse(); ++n) {
|
for (util::ListParser p(cs, list); p.parse(); ++n) {
|
||||||
idv.set_str(p.get_element());
|
idv.set_str(p.get_item());
|
||||||
idv.push();
|
idv.push();
|
||||||
if (n && space) {
|
if (n && space) {
|
||||||
r += ' ';
|
r += ' ';
|
||||||
|
@ -103,7 +103,7 @@ int cs_list_includes(
|
||||||
) {
|
) {
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
for (util::ListParser p(cs, list); p.parse();) {
|
for (util::ListParser p(cs, list); p.parse();) {
|
||||||
if (p.item == needle) {
|
if (p.get_raw_item() == needle) {
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
++offset;
|
++offset;
|
||||||
|
@ -125,11 +125,11 @@ static inline void cs_list_merge(
|
||||||
ostd::swap(list, elems);
|
ostd::swap(list, elems);
|
||||||
}
|
}
|
||||||
for (util::ListParser p(cs, list); p.parse();) {
|
for (util::ListParser p(cs, list); p.parse();) {
|
||||||
if (cmp(cs_list_includes(cs, elems, p.item), 0)) {
|
if (cmp(cs_list_includes(cs, elems, p.get_raw_item()), 0)) {
|
||||||
if (!buf.empty()) {
|
if (!buf.empty()) {
|
||||||
buf += ' ';
|
buf += ' ';
|
||||||
}
|
}
|
||||||
buf += p.quote;
|
buf += p.get_raw_item(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.set_str(ostd::move(buf));
|
res.set_str(ostd::move(buf));
|
||||||
|
@ -148,7 +148,7 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
}
|
}
|
||||||
CsString str = ostd::move(args[0].get_str());
|
CsString str = ostd::move(args[0].get_str());
|
||||||
util::ListParser p(cs, str);
|
util::ListParser p(cs, str);
|
||||||
p.item = str;
|
p.get_raw_item() = str;
|
||||||
for (ostd::Size i = 1; i < args.size(); ++i) {
|
for (ostd::Size i = 1; i < args.size(); ++i) {
|
||||||
p.get_input() = str;
|
p.get_input() = str;
|
||||||
CsInt pos = args[i].get_int();
|
CsInt pos = args[i].get_int();
|
||||||
|
@ -158,10 +158,10 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pos > 0 || !p.parse()) {
|
if (pos > 0 || !p.parse()) {
|
||||||
p.item = p.quote = ostd::ConstCharRange();
|
p.get_raw_item() = p.get_raw_item(true) = ostd::ConstCharRange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.set_str(p.get_element());
|
res.set_str(p.get_item());
|
||||||
});
|
});
|
||||||
|
|
||||||
gcs.new_command("sublist", "siiN", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("sublist", "siiN", [](auto &cs, auto args, auto &res) {
|
||||||
|
@ -185,11 +185,12 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char const *list = p.get_input().data();
|
char const *list = p.get_input().data();
|
||||||
p.quote = ostd::ConstCharRange();
|
p.get_raw_item(true) = ostd::ConstCharRange();
|
||||||
if (len > 0 && p.parse()) {
|
if (len > 0 && p.parse()) {
|
||||||
while (--len > 0 && p.parse());
|
while (--len > 0 && p.parse());
|
||||||
}
|
}
|
||||||
char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list;
|
ostd::ConstCharRange quote = p.get_raw_item(true);
|
||||||
|
char const *qend = !quote.empty() ? "e[quote.size()] : list;
|
||||||
res.set_str(ostd::ConstCharRange(list, qend - list));
|
res.set_str(ostd::ConstCharRange(list, qend - list));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -203,7 +204,7 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
int n = -1;
|
int n = -1;
|
||||||
for (util::ListParser p(cs, args[1].get_strr()); p.parse();) {
|
for (util::ListParser p(cs, args[1].get_strr()); p.parse();) {
|
||||||
++n;
|
++n;
|
||||||
idv.set_str(p.item);
|
idv.set_str(p.get_raw_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));
|
||||||
|
@ -222,11 +223,11 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
int n = -1;
|
int n = -1;
|
||||||
for (util::ListParser p(cs, args[1].get_strr()); p.parse();) {
|
for (util::ListParser p(cs, args[1].get_strr()); p.parse();) {
|
||||||
++n;
|
++n;
|
||||||
idv.set_str(p.item);
|
idv.set_str(p.get_raw_item());
|
||||||
idv.push();
|
idv.push();
|
||||||
if (cs.run_bool(body)) {
|
if (cs.run_bool(body)) {
|
||||||
if (p.parse()) {
|
if (p.parse()) {
|
||||||
res.set_str(p.get_element());
|
res.set_str(p.get_item());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -239,21 +240,21 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
gcs.new_command("listfind=", "i", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("listfind=", "i", [](auto &cs, auto args, auto &res) {
|
||||||
cs_list_find<CsInt>(
|
cs_list_find<CsInt>(
|
||||||
cs, args, res, [](const util::ListParser &p, CsInt val) {
|
cs, args, res, [](const util::ListParser &p, CsInt val) {
|
||||||
return cs_parse_int(p.item) == val;
|
return cs_parse_int(p.get_raw_item()) == val;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
gcs.new_command("listfind=f", "f", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("listfind=f", "f", [](auto &cs, auto args, auto &res) {
|
||||||
cs_list_find<CsFloat>(
|
cs_list_find<CsFloat>(
|
||||||
cs, args, res, [](const util::ListParser &p, CsFloat val) {
|
cs, args, res, [](const util::ListParser &p, CsFloat val) {
|
||||||
return cs_parse_float(p.item) == val;
|
return cs_parse_float(p.get_raw_item()) == val;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
gcs.new_command("listfind=s", "s", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("listfind=s", "s", [](auto &cs, auto args, auto &res) {
|
||||||
cs_list_find<ostd::ConstCharRange>(
|
cs_list_find<ostd::ConstCharRange>(
|
||||||
cs, args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
|
cs, args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
|
||||||
return p.item == val;
|
return p.get_raw_item() == val;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -261,21 +262,21 @@ void cs_init_lib_list(CsState &gcs) {
|
||||||
gcs.new_command("listassoc=", "i", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("listassoc=", "i", [](auto &cs, auto args, auto &res) {
|
||||||
cs_list_assoc<CsInt>(
|
cs_list_assoc<CsInt>(
|
||||||
cs, args, res, [](const util::ListParser &p, CsInt val) {
|
cs, args, res, [](const util::ListParser &p, CsInt val) {
|
||||||
return cs_parse_int(p.item) == val;
|
return cs_parse_int(p.get_raw_item()) == val;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
gcs.new_command("listassoc=f", "f", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("listassoc=f", "f", [](auto &cs, auto args, auto &res) {
|
||||||
cs_list_assoc<CsFloat>(
|
cs_list_assoc<CsFloat>(
|
||||||
cs, args, res, [](const util::ListParser &p, CsFloat val) {
|
cs, args, res, [](const util::ListParser &p, CsFloat val) {
|
||||||
return cs_parse_float(p.item) == val;
|
return cs_parse_float(p.get_raw_item()) == val;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
gcs.new_command("listassoc=s", "s", [](auto &cs, auto args, auto &res) {
|
gcs.new_command("listassoc=s", "s", [](auto &cs, auto args, auto &res) {
|
||||||
cs_list_assoc<ostd::ConstCharRange>(
|
cs_list_assoc<ostd::ConstCharRange>(
|
||||||
cs, args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
|
cs, args, res, [](const util::ListParser &p, ostd::ConstCharRange val) {
|
||||||
return p.item == val;
|
return p.get_raw_item() == val;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -288,7 +289,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(cs, args[1].get_strr()); p.parse(); ++n) {
|
for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) {
|
||||||
idv.set_str(p.get_element());
|
idv.set_str(p.get_item());
|
||||||
idv.push();
|
idv.push();
|
||||||
switch (cs.run_loop(body)) {
|
switch (cs.run_loop(body)) {
|
||||||
case CsLoopState::Break:
|
case CsLoopState::Break:
|
||||||
|
@ -309,9 +310,9 @@ end:
|
||||||
auto body = args[3].get_code();
|
auto body = args[3].get_code();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(cs, args[2].get_strr()); p.parse(); n += 2) {
|
for (util::ListParser p(cs, args[2].get_strr()); p.parse(); n += 2) {
|
||||||
idv1.set_str(p.get_element());
|
idv1.set_str(p.get_item());
|
||||||
if (p.parse()) {
|
if (p.parse()) {
|
||||||
idv2.set_str(p.get_element());
|
idv2.set_str(p.get_item());
|
||||||
} else {
|
} else {
|
||||||
idv2.set_str("");
|
idv2.set_str("");
|
||||||
}
|
}
|
||||||
|
@ -338,14 +339,14 @@ end:
|
||||||
auto body = args[4].get_code();
|
auto body = args[4].get_code();
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(cs, args[3].get_strr()); p.parse(); n += 3) {
|
for (util::ListParser p(cs, args[3].get_strr()); p.parse(); n += 3) {
|
||||||
idv1.set_str(p.get_element());
|
idv1.set_str(p.get_item());
|
||||||
if (p.parse()) {
|
if (p.parse()) {
|
||||||
idv2.set_str(p.get_element());
|
idv2.set_str(p.get_item());
|
||||||
} else {
|
} else {
|
||||||
idv2.set_str("");
|
idv2.set_str("");
|
||||||
}
|
}
|
||||||
if (p.parse()) {
|
if (p.parse()) {
|
||||||
idv3.set_str(p.get_element());
|
idv3.set_str(p.get_item());
|
||||||
} else {
|
} else {
|
||||||
idv3.set_str("");
|
idv3.set_str("");
|
||||||
}
|
}
|
||||||
|
@ -388,13 +389,13 @@ end:
|
||||||
CsString r;
|
CsString r;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) {
|
for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) {
|
||||||
idv.set_str(p.item);
|
idv.set_str(p.get_raw_item());
|
||||||
idv.push();
|
idv.push();
|
||||||
if (cs.run_bool(body)) {
|
if (cs.run_bool(body)) {
|
||||||
if (r.size()) {
|
if (r.size()) {
|
||||||
r += ' ';
|
r += ' ';
|
||||||
}
|
}
|
||||||
r += p.quote;
|
r += p.get_raw_item(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.set_str(ostd::move(r));
|
res.set_str(ostd::move(r));
|
||||||
|
@ -408,7 +409,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(cs, args[1].get_strr()); p.parse(); ++n) {
|
for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) {
|
||||||
idv.set_str(p.item);
|
idv.set_str(p.get_raw_item());
|
||||||
idv.push();
|
idv.push();
|
||||||
if (cs.run_bool(body)) {
|
if (cs.run_bool(body)) {
|
||||||
r++;
|
r++;
|
||||||
|
@ -424,10 +425,11 @@ end:
|
||||||
ostd::Size len = util::ListParser(cs, s).count();
|
ostd::Size len = util::ListParser(cs, s).count();
|
||||||
ostd::Size n = 0;
|
ostd::Size n = 0;
|
||||||
for (util::ListParser p(cs, s); p.parse(); ++n) {
|
for (util::ListParser p(cs, s); p.parse(); ++n) {
|
||||||
if (!p.quote.empty() && (p.quote.front() == '"')) {
|
if (!p.get_raw_item(true).empty() &&
|
||||||
util::unescape_string(buf, p.item);
|
(p.get_raw_item(true).front() == '"')) {
|
||||||
|
util::unescape_string(buf, p.get_raw_item());
|
||||||
} else {
|
} else {
|
||||||
buf.put_n(p.item.data(), p.item.size());
|
buf.put_n(p.get_raw_item().data(), p.get_raw_item().size());
|
||||||
}
|
}
|
||||||
if ((n + 1) < len) {
|
if ((n + 1) < len) {
|
||||||
if ((len > 2) || conj.empty()) {
|
if ((len > 2) || conj.empty()) {
|
||||||
|
@ -471,7 +473,8 @@ end:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char const *qend = !p.quote.empty() ? &p.quote[p.quote.size()] : list;
|
ostd::ConstCharRange quote = p.get_raw_item(true);
|
||||||
|
char const *qend = !quote.empty() ? "e[quote.size()] : list;
|
||||||
CsString buf;
|
CsString buf;
|
||||||
if (qend > list) {
|
if (qend > list) {
|
||||||
buf += ostd::ConstCharRange(list, qend - list);
|
buf += ostd::ConstCharRange(list, qend - list);
|
||||||
|
@ -540,7 +543,7 @@ static void cs_list_sort(
|
||||||
ostd::Size total = 0;
|
ostd::Size total = 0;
|
||||||
|
|
||||||
for (util::ListParser p(cs, list); p.parse();) {
|
for (util::ListParser p(cs, list); p.parse();) {
|
||||||
ListSortItem item = { p.item, p.quote };
|
ListSortItem item = { p.get_raw_item(), p.get_raw_item(true) };
|
||||||
items.push(item);
|
items.push(item);
|
||||||
total += item.quote.size();
|
total += item.quote.size();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue