Parsing with ranges
Tomek Sowiński
just at ask.me
Fri Jul 23 14:32:28 PDT 2010
Dnia 22-07-2010 o 15:22:14 %u <e at sp.am> napisał(a):
> How to parse a simple format with ranges? For example a query string
> format:
>
> string[string] params = parse("foo=1&bar=1%202&baz=1+2+3");
> assert(params["foo"] == "1");
> assert(params["bar"] == "1 2");
> assert(params["baz"] == "1 2 3");
>
> parse() should accept a string, an stream or anything else that make
> sense.
Something like this (not tested):
import std.algorithm;
string[string] parse(R)(R input) {
string[string] params;
foreach (assignment; splitter(input, '&')) {
auto a = splitter(assignment, '=');
string lvalue = a.front;
a.popFront;
string rvalue = replace(a.front, '+', ' ');
params[lvalue] = rvalue;
a.popFront;
assert (a.empty);
}
return params;
}
Tomek
More information about the Digitalmars-d-learn
mailing list