www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Parsing with ranges

reply %u <e sp.am> writes:
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.

Thanks
Jul 22 2010
parent =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 22-07-2010 o 15:22:14 %u <e sp.am> napisa=B3(a):

 How to parse a simple format with ranges? For example a query string  =
 format:

 string[string] params =3D parse("foo=3D1&bar=3D1%202&baz=3D1+2+3");
 assert(params["foo"] =3D=3D "1");
 assert(params["bar"] =3D=3D "1 2");
 assert(params["baz"] =3D=3D "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 =3D splitter(assignment, '=3D'); string lvalue =3D a.front; a.popFront; string rvalue =3D replace(a.front, '+', ' '); params[lvalue] =3D rvalue; a.popFront; assert (a.empty); } return params; } Tomek
Jul 23 2010