V2 string

Bruno Medeiros brunodomedeiros+spam at com.gmail
Sat Jul 7 12:37:29 PDT 2007


Regan Heath wrote:
> 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 :(
> 

Algoritms should care about worst-case performance, or average-case 
performance. That most efficient "case", where a string is already 
tolower, is a minority case in most applications, and is never a 
worst-case scenario. So why bother?
Also, doing this tolower like that would give other performance problems 
like these:

> 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

Indeed, with such scenario, you would end up with worse performance overall.

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list