www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Style] Converting from char[] to string, to!string vs idup

reply "Mark Isaacson" <turck11 hotmail.com> writes:
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
next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
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
parent "Mark Isaacson" <turck11 hotmail.com> writes:
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
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
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
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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:
 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.)
In the case of char[] -> char[], you explicitly want to dup. The original text is part of a buffer that is reused. -Steve
Mar 25 2014