V2 string

Derek Parnell derek at psych.ward
Wed Jul 4 16:23:11 PDT 2007


On Wed, 04 Jul 2007 15:48:45 -0700, Walter Bright wrote:

> Derek Parnell wrote:
>> I'm converting Bud to compile using V2 and so far its been a very hard
>> thing to do. I'm finding that I'm now having to use '.dup' and '.idup' all
>> over the place, which is exactly what I thought would happen. Bud does a
>> lot of text manipulation so having 'string' as invariant means that calls
>> to functions that return string need to often be .dup'ed because I need to
>> assign the result to a malleable variable. 
>> 
>> I might have to rethink of the design of the application to avoid the
>> performance hit of all these dups.
>> 
> 
> First of all, if you were returning string literals as char[] and trying 
> to manipulate them, they'd fail on linux at run time (because string 
> literals are put into read only segments).

But I'm not, and never have been, returning string literals anywhere.
 
> Second, you can use char[] instead of string.

The idiom I'm using is that functions that receive text have those
parameters as 'string' to guard against the function inadvertantly
modifying that which is passed, and functions that return text return
'string' to guard against calling functions inadvertantly modifying data
that they did not create (own).

This leads to constructs like ...

   char[] result;

   result = SomeTextFunc(data).dup;

Another commonly used idiom that I had to stop using was ...

   char[] text;
   text = getvalue();
   if (wrongvalue(text))
       text = ""; // Reset to an empty string

I now code ...

       text.length = 0; // Reset to an empty string

which is slightly less readable.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell



More information about the Digitalmars-d mailing list