July 22, 2010 Parsing with ranges | ||||
---|---|---|---|---|
| ||||
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 |
July 23, 2010 Re: Parsing with ranges | ||||
---|---|---|---|---|
| ||||
Posted in reply to %u | Dnia 22-07-2010 o 15:22:14 %u <e@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
|
Copyright © 1999-2021 by the D Language Foundation