V2 string

Regan Heath regan at netmail.co.nz
Thu Jul 5 01:40:10 PDT 2007


Derek Parnell Wrote:
> 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

Yep, makes sense.

> , and functions that return text return
> 'string' to guard against calling functions inadvertantly modifying data
> that they did not create (own).

Question;  Do these functions keep a copy of the returned string?  Or, to re-phrase, after returning the string do they still 'own' it, or have they washed their hands of it?  Are they in a sense passing ownership to the calling function perhaps?

If they no longer 'own' the string then they can return it as a char[] instead of string and all your problems are solved, right?

I imagine that if they return a slice of the input string, and that string was 'string' not char[] then they would also return string (because doing otherwise would be claiming ownership of the input string and giving it away to the caller, which may not be valid)

Maybe you have a lot of functions returning slices to the input string?

Maybe you need to template them? i.e.

T function(T)(T param)
{
}

so if you pass string you get string, if you pass char[] you get char[].

Maybe all string routines which return slices of the input should be so templated?

Regan



More information about the Digitalmars-d mailing list