V2 string

Regan Heath regan at netmail.co.nz
Fri Jul 6 01:18:50 PDT 2007


Bruno Medeiros wrote:
>>> The current signature:
>>>   const(char)[] tolower(const(char)[] str)
>>> is kinda incorrect, because it returns a const reference for an array 
>>> that has no mutable references, and that is the same as an invariant 
>>> reference, so tolower might as well return invariant(char)[].
>>
>> Again, that only holds if a copy was actually made at run time. If no 
>> copy was made the original input is returned, to which there may be 
>> mutable references.
> 
> You're right, if a copy is not made *every* time (which is the case
> after all), then the above doesn't hold.
> But then, what I think is happening is that Phobo's current tolower is
> suboptimal in terms of usefulness, because the fact that we don't know
> if a new copy is made or not. I'm wondering now what would be the more
> useful form, or forms, of tolower (and similar functions) to have.
> Now that I think of it again (admittedly I haven't got much experience 
> with string manipulation in C++ or D, though), but perhaps the best form 
> is an in-place mutable version:
>   char[] tolower(char[] str);
> And it's this one after all that is the most general form. If you want 
> to call tolower on a const or invariant array you dup it yourself on the 
> call:
>   char[] str = tolower("FOO".dup);

True.. but it's unfortunate that the most efficient case, where no 
duplication is needed, is no longer possible :(

If we template the function, eg.

T tolower(T)(T input)
{
}

and we have some way to check whether the input is const or not (at 
runtime is(string) or something?) perhaps we can code the existing 
efficient solution (no dup of const data) as well as the general case 
where it mutates.  In the mutate case it can dup if the input is const 
and not dup if it isn't (adding an efficient solution which doesn't 
currently exist).

The only problem is that the case where you pass const data and it has 
to dup, you get back a const reference to a piece of data with no other 
owner (meaning it doesn't need to be const) which might cause another 
dup in your code at a later point.

Regan



More information about the Digitalmars-d mailing list