Uri class and parser

Jacob Carlborg doob at me.com
Wed Oct 24 12:54:54 PDT 2012


On 2012-10-24 20:22, Mike van Dongen wrote:

> 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

> 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?

Ok, I'm using firefox and it doesn't look particular good on github. The 
Phobos standard is to use tabs as spaces with the size of 4.

> 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?

If the some methods aren't used by the URI parser you should remove the. 
If they're used I would suggested you move the further down in the code, 
possibly at the bottom.

> You may be able to see the new getters by checking out this unittest:

Cool. It would be nice to have a way to set the query and path as an 
(associative) array as well.

Just a suggestion, I don't really see a point in having getters and 
setters that just forwards to the instance variables. Just use public 
instance variables. The only reason to use getters and setters would be 
to be able to subclass and override them. But I think you could just 
make Uri a final class.

About path and query. I wonder that's best to be default return an 
(associative) array or a string. I would think it's more useful to 
return an (associative) array and then provide rawPath() and rawQuery() 
which would return strings.

A nitpick, I'm not really an expert on URI's but is "fragment" really 
the correct name for that I would call the "hash"? That would be "nose" 
in the example below.

> 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));


-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list