digitalmars.D.learn - [Style] Converting from char[] to string, to!string vs idup
- Mark Isaacson (17/17) Mar 25 2014 I am presently working my way through TDPL for the second time,
- Justin Whear (7/27) Mar 25 2014 I believe char[] -> string conversion with to! will use this
- Mark Isaacson (3/3) Mar 25 2014 Much appreciated everyone! I had a vague intuition that these
- bearophile (5/6) Mar 25 2014 I sometimes prefer the text function:
- monarch_dodra (14/16) Mar 25 2014 Mostly because it's more generic. For example:
- H. S. Teoh (8/16) Mar 25 2014 I think it's a good thing. It avoids needless copying where it's not
- Steven Schveighoffer (5/18) Mar 25 2014 In the case of char[] -> char[], you explicitly want to dup. The origina...
I am presently working my way through TDPL for the second time, and there's an example in chapter 1 to the effect of: [code] string currentParagraph; foreach(line; stdin.byLine()) { if (line.length > 2) { currentParagraph = to!string(line[2 .. $]); } } [/code] The explicit conversion is necessary because `line` is a `char[]` but `currentParagraph` is a `string`. My question is why use `to!string` instead of just doing `line[2 .. $].idup`? Is there a performance benefit? Is it simply because it's more general? I tried taking a peek at the implementation of to!string, but it wasn't easy enough to follow and boil down into components.
Mar 25 2014
On Tue, 25 Mar 2014 21:35:46 +0000, Mark Isaacson wrote:I am presently working my way through TDPL for the second time, and there's an example in chapter 1 to the effect of: [code] string currentParagraph; foreach(line; stdin.byLine()) { if (line.length > 2) { currentParagraph = to!string(line[2 .. $]); } } [/code] The explicit conversion is necessary because `line` is a `char[]` but `currentParagraph` is a `string`. My question is why use `to!string` instead of just doing `line[2 .. $].idup`? Is there a performance benefit? Is it simply because it's more general? I tried taking a peek at the implementation of to!string, but it wasn't easy enough to follow and boil down into components.I believe char[] -> string conversion with to! will use this implementation: https://github.com/D-Programming-Language/phobos/blob/ master/std/conv.d#L823 So there should be no performance implications, but to!string is better self-documenting and flexible should you need to change the input type in future.
Mar 25 2014
Much appreciated everyone! I had a vague intuition that these were the reasons, but it was helpful to spell them out. I'm especially partial to the self-documentation reasoning.
Mar 25 2014
Mark Isaacson:why use `to!string` instead of just doing `line[2 .. $].idup`?I sometimes prefer the text function: = line[2 .. $].text; Bye, bearophile
Mar 25 2014
On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote:Is there a performance benefit? Is it simply because it's more general?Mostly because it's more generic. For example: If instead you want to do "string" => "char[]", then your code will have to be changed to use "dup". if instead you want to do char[] => dstring, then your code will simply not work at all. By contrast, "to!" will "just work". It's "A one-stop shop for converting values from one type to another." There is *1* thing you should take into account though: "to!" is a no-op for string=>string or char[]=>char[], or anything else that can be implicitly converted as such. In contrast, "dup"/"idup" will create an actual copy. Not that this is good or bad. Just something you should keep in mind.
Mar 25 2014
On Tue, Mar 25, 2014 at 10:02:33PM +0000, monarch_dodra wrote:On Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote:[...]Is there a performance benefit? Is it simply because it's more general?There is *1* thing you should take into account though: "to!" is a no-op for string=>string or char[]=>char[], or anything else that can be implicitly converted as such. In contrast, "dup"/"idup" will create an actual copy. Not that this is good or bad. Just something you should keep in mind.I think it's a good thing. It avoids needless copying where it's not necessary. (Of course, if you have a char[] and you actually want a new copy, then you have to use .dup explicitly.) T -- Маленькие детки - маленькие бедки.
Mar 25 2014
On Tue, 25 Mar 2014 19:13:07 -0400, H. S. Teoh <hsteoh quickfur.ath.cx> wrote:On Tue, Mar 25, 2014 at 10:02:33PM +0000, monarch_dodra wrote:In the case of char[] -> char[], you explicitly want to dup. The original text is part of a buffer that is reused. -SteveOn Tuesday, 25 March 2014 at 21:35:47 UTC, Mark Isaacson wrote:[...]Is there a performance benefit? Is it simply because it's more general?There is *1* thing you should take into account though: "to!" is a no-op for string=>string or char[]=>char[], or anything else that can be implicitly converted as such. In contrast, "dup"/"idup" will create an actual copy. Not that this is good or bad. Just something you should keep in mind.I think it's a good thing. It avoids needless copying where it's not necessary. (Of course, if you have a char[] and you actually want a new copy, then you have to use .dup explicitly.)
Mar 25 2014