Uri class and parser
Mike van Dongen
dlang at mikevandongen.nl
Wed Oct 24 11:22:45 PDT 2012
On Wednesday, 24 October 2012 at 07:38:58 UTC, Jacob Carlborg
wrote:
> I would have expected a few additional components, like:
>
> * Domain
> * Password
> * Username
> * Host
> * Hash
>
> A way to build an URI base on the components.
> It would be nice if there were methods for getting/setting the
> path component as an array. Also methods for getting/setting
> the query component as an associative array.
Thanks for the suggestions!
I've added many, if not all, of them to the repo:
- Identifying/separating the username, password (together the
userinfo), the domain and the port number from the authority.
- The hash now also can be get/set and the same thing goes for
the data in the query
On Wednesday, 24 October 2012 at 12:47:15 UTC, Adam D. Ruppe
wrote:
> On Wednesday, 24 October 2012 at 07:38:58 UTC, Jacob Carlborg
> wrote:
>> It would be nice if there were methods for getting/setting the
>> path component as an array. Also methods for getting/setting
>> the query component as an associative array.
>
> BTW don't forget that this is legal:
>
> ?value&value=1&value=2
>
> The appropriate type for the AA is
>
> string[][string]
It does not yet take into account the fact that multiple query
elements can have the same name. I'll be working on that next.
On Wednesday, 24 October 2012 at 07:38:58 UTC, Jacob Carlborg
wrote:
> A few stylistic issues. There are a lot of places where you
> haven't indented the code, at least how it looks like on github.
>
> I wouldn't put the private methods at the top.
As for the indentations, I use tabs with the size of 4 spaces.
Viewing the code on Github (in Chromium) you'll see tabs of 8
spaces.
I'm not sure what the phobos standard is?
As all my code is part of a single class and the file std/uri.d
already existed, I decided to 'just' append my code to the file.
Should I perhaps put it in another file as the private methods
you mentioned are not relevant to my code?
You may be able to see the new getters by checking out this
unittest:
uri =
Uri.parse("foo://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal&novalue#nose");
assert(uri.scheme == "foo");
assert(uri.authority == "username:password at example.com:8042");
assert(uri.path == "over/there/index.dtb");
assert(uri.pathAsArray == ["over", "there", "index.dtb"]);
assert(uri.query == "type=animal&name=narwhal&novalue");
assert(uri.queryAsArray == ["type": "animal", "name": "narwhal",
"novalue": ""]);
assert(uri.fragment == "nose");
assert(uri.host == "example.com");
assert(uri.port == 8042);
assert(uri.username == "username");
assert(uri.password == "password");
assert(uri.userinfo == "username:password");
assert(uri.queryAsArray["type"] == "animal");
assert(uri.queryAsArray["novalue"] == "");
assert("novalue" in uri.queryAsArray);
assert(!("nothere" in uri.queryAsArray));
More information about the Digitalmars-d
mailing list