www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Type to represent URIs?

reply Victor Porton <porton narod.ru> writes:
Which type should I use to represent (absolute) URIs (or URLs)?

Is it OK to use `string` for this, or is there a better option?

I could also use URI class from my library:
https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d

but as you see in this library there are two choices for URI 
class and it's unclear which of the two I would use.
Jan 28
next sibling parent reply bauss <jj_1337 live.dk> writes:
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
 Which type should I use to represent (absolute) URIs (or URLs)?

 Is it OK to use `string` for this, or is there a better option?

 I could also use URI class from my library:
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d

 but as you see in this library there are two choices for URI 
 class and it's unclear which of the two I would use.
https://dlang.org/phobos/std_uri.html
Jan 28
parent Victor Porton <porton narod.ru> writes:
On Monday, 28 January 2019 at 18:36:11 UTC, bauss wrote:
 On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
 Which type should I use to represent (absolute) URIs (or URLs)?
...
 https://dlang.org/phobos/std_uri.html
This modules does not contain a type to represent URIs. OK, I will use alias URI = string;
Jan 28
prev sibling next sibling parent Erikvv <erik evanv.nl> writes:
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
 Which type should I use to represent (absolute) URIs (or URLs)?

 Is it OK to use `string` for this, or is there a better option?

 I could also use URI class from my library:
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d

 but as you see in this library there are two choices for URI 
 class and it's unclear which of the two I would use.
It depends. If you're just taking a string from config or user input and passing it to a library, I would just leave it a string. However, if you are going to manipulate the url, like adding query parameters, changing the path, or checking the protocol, I would use a URI class for that. http://code.dlang.org/packages/urld looks good to me.
Jan 28
prev sibling next sibling parent Atila Neves <atila.neves gmail.com> writes:
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
 Which type should I use to represent (absolute) URIs (or URLs)?

 Is it OK to use `string` for this, or is there a better option?

 I could also use URI class from my library:
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d

 but as you see in this library there are two choices for URI 
 class and it's unclear which of the two I would use.
It depends. If you have a function such as `void connect(string uri)` with only one parameter, I'd say a string is sufficient. If, however, it's `void fun(string uri, string config, string somethingElse)`, then use a URI type instead.
Jan 29
prev sibling parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
 Which type should I use to represent (absolute) URIs (or URLs)?

 Is it OK to use `string` for this, or is there a better option?

 I could also use URI class from my library:
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
 https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d

 but as you see in this library there are two choices for URI 
 class and it's unclear which of the two I would use.
I like std.path it does not use a path type and works directly off strings. There are benefits to a path type, but I don't know if that is enough. So I think I would prefer a uri library doing the same thing.
Jan 30
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Wed, 30 Jan 2019 15:17:28 +0000, Jesse Phillips wrote:
 I like std.path it does not use a path type and works directly off
 strings. There are benefits to a path type, but I don't know if that is
 enough.
 
 So I think I would prefer a uri library doing the same thing.
I admit I didn't even consider that when writing urld. Now that I'm considering it, it seems like a tiny benefit in memory usage at a cost of having to re-parse large portions of the URL each time you want to manipulate it. If you are only going to extract one part of a URL, it's better to use string everywhere. If you are going to manipulate a URL in several ways in a narrow section of code, you can use string at rest and in transit, then use the types provided by Vibe or urld only when you need to extract data or modify a URL. Or provide overloads for both, maybe.
Jan 30
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Jan 30, 2019 at 05:21:25PM +0000, Neia Neutuladh via Digitalmars-d
wrote:
 On Wed, 30 Jan 2019 15:17:28 +0000, Jesse Phillips wrote:
 I like std.path it does not use a path type and works directly off
 strings. There are benefits to a path type, but I don't know if that
 is enough.
 
 So I think I would prefer a uri library doing the same thing.
I admit I didn't even consider that when writing urld. Now that I'm considering it, it seems like a tiny benefit in memory usage at a cost of having to re-parse large portions of the URL each time you want to manipulate it. If you are only going to extract one part of a URL, it's better to use string everywhere. If you are going to manipulate a URL in several ways in a narrow section of code, you can use string at rest and in transit, then use the types provided by Vibe or urld only when you need to extract data or modify a URL. Or provide overloads for both, maybe.
This is the classic case of different representations for the same thing, like using cartesian coordinates vs. polar / spherical coordinates for vectors. Generally, it would make sense to use the most convenient representation for the particular piece of code you're working with, and interconvert where necessary. T -- If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell
Jan 30