remove list_length and instead add a method

master
Daniel Kolesa 2016-10-24 02:33:04 +02:00
parent ed106d7e11
commit 61135fb79b
3 changed files with 13 additions and 14 deletions

View File

@ -768,14 +768,14 @@ namespace util {
void skip(); void skip();
bool parse(); bool parse();
ostd::Size count();
CsString element(); CsString element() const;
private: private:
CsState &p_state; CsState &p_state;
}; };
OSTD_EXPORT ostd::Size list_length(CsState &cs, ostd::ConstCharRange s);
OSTD_EXPORT ostd::Maybe<CsString> list_index( OSTD_EXPORT ostd::Maybe<CsString> list_index(
CsState &cs, ostd::ConstCharRange s, ostd::Size idx CsState &cs, ostd::ConstCharRange s, ostd::Size idx
); );

View File

@ -372,7 +372,15 @@ endblock:
return true; return true;
} }
CsString ListParser::element() { ostd::Size ListParser::count() {
ostd::Size ret = 0;
while (parse()) {
++ret;
}
return ret;
}
CsString ListParser::element() const {
CsString s; CsString s;
s.reserve(item.size()); s.reserve(item.size());
if (!quote.empty() && (*quote == '"')) { if (!quote.empty() && (*quote == '"')) {
@ -387,15 +395,6 @@ endblock:
return s; return s;
} }
OSTD_EXPORT ostd::Size list_length(CsState &cs, ostd::ConstCharRange s) {
ListParser p(cs, s);
ostd::Size ret = 0;
while (p.parse()) {
++ret;
}
return ret;
}
OSTD_EXPORT ostd::Maybe<CsString> list_index( OSTD_EXPORT ostd::Maybe<CsString> list_index(
CsState &cs, ostd::ConstCharRange s, ostd::Size idx CsState &cs, ostd::ConstCharRange s, ostd::Size idx
) { ) {

View File

@ -139,7 +139,7 @@ static void cs_init_lib_list_sort(CsState &cs);
void cs_init_lib_list(CsState &gcs) { void cs_init_lib_list(CsState &gcs) {
gcs.new_command("listlen", "s", [](auto &cs, auto args, auto &res) { gcs.new_command("listlen", "s", [](auto &cs, auto args, auto &res) {
res.set_int(CsInt(util::list_length(cs, args[0].get_strr()))); res.set_int(CsInt(util::ListParser(cs, args[0].get_strr()).count()));
}); });
gcs.new_command("at", "si1V", [](auto &cs, auto args, auto &res) { gcs.new_command("at", "si1V", [](auto &cs, auto args, auto &res) {
@ -421,7 +421,7 @@ end:
auto buf = ostd::appender<CsString>(); 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(cs, s); 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.quote.empty() && (p.quote.front() == '"')) {