V2 string

BCS ao at pathlink.com
Thu Jul 5 15:50:22 PDT 2007


Reply to Walter,

> Derek Parnell wrote:
> 
>> I'll make my code example more free from assumed functionality.
>> 
>> char[] qwerty;
>> 
>> qwerty = KJHGF(poiuy).dup;
>> version(xyzzy)
>> {
>> MNBVC(qwerty);
>> }
>> As you can see, my point is made without regard to converting stuff
>> to lower case.
>> 
> My point is that the way the snippet is written is inside out. Do not
> use .dup to preemptively make a copy in case it gets changed somewhere
> later one. The style is to make a .dup *only if* the contents will be
> changed and do the .dup *at the site* of the modification.
> 
> In other words, dups should be done from the bottom up, not from the
> top down.
> 
> I think such a style helps fit things together nicely and avoids
> strange .dups appearing in inexplicable places.
> 


The one issue I can see with this is where an input is const but may be changed 
(and .duped) at any of a number of points. The data though only needs to 
be .duped once.

|char[] Whatever(const char[] str)
|{
| if(c1) str = Mod1(str.dup);
| if(c2) str = Mod2(str.dup);
| if(c3) str = Mod3(str.dup);
| return str;
|}
// causes exces duping


I can't think of a better solution than this (and this is BAD):

|char[] Whatever(const char[] str)
|{
| sw: switch(-1)
| {
|  foreach(bool b; T!(true, false))
|  {
|   if(c1) {static if(b){str = str.dup; goto case 1;} else {case 1:  str 
= Mod1(str.dup);}}
|   if(c2) {static if(b){str = str.dup; goto case 2;} else {case 2:  str 
= Mod2(str.dup);}}
|   if(c3) {static if(b){str = str.dup; goto case 3;} else {case 3:  str 
= Mod3(str.dup);}}
|   return str;
|  }
| }
|}





More information about the Digitalmars-d mailing list