Const, invariant, strings and "constant data need never be copied"

Stewart Gordon smjg_1998 at yahoo.com
Sat Nov 3 05:54:31 PDT 2007


"Janice Caron" <caron800 at googlemail.com> wrote in message 
news:mailman.522.1194074475.16939.digitalmars-d at puremagic.com...
> On 11/3/07, Stewart Gordon <smjg_1998 at yahoo.com> wrote:
<snip>
>> If /all/ strings are invariant, then you're very limited in what
>> manipulations you can perform.
>
> That's not true. You can do as much manipulation as you want at
> creation-time. Only once the manipulation is "finished" do you cast
> the result to string. (And that behavior is identical between const()
> and invariant(), by the way, so the change to invariant() makes zero
> difference to the source code at this point, except that you now have
> the option of using the assumeUnique() function).

So effectively, you're using the word "string" to refer specifically to the 
invariant kind, making "if all strings are invariant" a null condition.

>> Only in the cases where ensuring that the reference is unique is 
>> possible.
>
> It's always possible. Just write an opening brace, do all your string
> creation, assign the string to a string variable declared outside the
> scope, then write a closing brace. Viola - invariance guaranteed,
> because all the other references used in creation just went out of
> scope.

Maybe you're right ... but I'll have to see.

<snip>
>> You miss the point.  My example is of ad-hoc code to perform the 
>> conversion
>> in place, because it is the most efficient mechanism with the constraints
>> under which the application will ever perform it.  Data always loaded 
>> into
>> RAM immediately before the conversion, and no desire to keep the 'before'
>> data once the conversion has happened.
>
> Well then there's no problem anyway. For the whole time that your
> string is "under construction", then it's not a string, it's a
> (mutable) array of chars. Just keep it as such, until you've finished
> building the string. Then do can do everything in place.

So in other words, my code was more or less right in the first place.

> But note that if you want to do in-place manipulation of chars, then
> std.string.replace() is NOT the function to use, because that
> (possibly) makes a copy. Instead, you would have a replacing loop, or
> write your own in-place-replace function which operates on char arrays
> (or templatized for arrays in general)

Having the std.string functions is useful even if they create copies.  A 
little bit of copying where it makes coding easier is OK for apps that 
aren't performance-critical, but it's still nice not to be made to do even 
more copying or down-and-dirty casting away invariant.

>> Well, I wasn't writing a string function there.
>
> Then you shouldn't be calling std.string functions. What you need are
> array functions.

I'm not sure what you mean....

Stewart.

-- 
My e-mail address is valid but not my primary mailbox.  Please keep replies 
on the 'group where everybody may benefit. 




More information about the Digitalmars-d mailing list