use listparser more

master
Daniel Kolesa 2015-08-13 00:16:52 +01:00
parent 7ed92f19cc
commit 97b070b972
2 changed files with 13 additions and 11 deletions

View File

@ -4004,23 +4004,24 @@ endblock:
}; };
namespace util { namespace util {
ostd::Size list_length(const char *str) { ostd::Size list_length(ostd::ConstCharRange s) {
ListParser p(str); ListParser p(s);
ostd::Size ret = 0; ostd::Size ret = 0;
while (p.parse()) ++ret; while (p.parse()) ++ret;
return ret; return ret;
} }
ostd::Maybe<ostd::String> list_index(const char *s, ostd::Size idx) { ostd::Maybe<ostd::String> list_index(ostd::ConstCharRange s,
ostd::Size idx) {
ListParser p(s);
for (ostd::Size i = 0; i < idx; ++i) for (ostd::Size i = 0; i < idx; ++i)
if (!parselist(s)) return ostd::nothing; if (!p.parse()) return ostd::nothing;
const char *start, *end, *qstart; if (!p.parse())
if (!parselist(s, start, end, qstart))
return ostd::nothing; return ostd::nothing;
return ostd::move(listelem(start, end, qstart)); return ostd::move(p.element());
} }
ostd::Vector<ostd::String> list_explode(const char *s, ostd::Vector<ostd::String> list_explode(ostd::ConstCharRange s,
ostd::Size limit) { ostd::Size limit) {
ostd::Vector<ostd::String> ret; ostd::Vector<ostd::String> ret;
ListParser p(s); ListParser p(s);

View File

@ -470,9 +470,10 @@ namespace util {
return ret; return ret;
} }
ostd::Size list_length(const char *str); ostd::Size list_length(ostd::ConstCharRange s);
ostd::Maybe<ostd::String> list_index(const char *s, ostd::Size idx); ostd::Maybe<ostd::String> list_index(ostd::ConstCharRange s,
ostd::Vector<ostd::String> list_explode(const char *s, ostd::Size idx);
ostd::Vector<ostd::String> list_explode(ostd::ConstCharRange s,
ostd::Size limit = -1); ostd::Size limit = -1);
} }