V2 string
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Thu Jul 5 05:43:43 PDT 2007
Bruno Medeiros wrote:
> Regan Heath wrote:
>> tolower is an interesting case. As a caller I expect it to modify the
>> string, or perhaps give a modified copy back (both options are valid
>> and should perhaps be supported?).
>>
>> So, the 'string tolower(string)' version has 2 cases, the first case
>> where it doesn't need to modify the input and can simply return it, no
>> problem.
>> But case 2, where it does modify it should dup and return char[]. My
>> reasoning being that after it has completed and returned the copy, the
>> caller now 'owns' the string (as it's the only copy in existance and
>> no-one else has a reference to it).
>>
>
> Indeed, I think this illustrates that some standard library functions
> may not have the correct signature, and I tolower is likely one of them.
> The most general case for tolower is:
> char[] tolower(const(char)[] s);
> Since tolower creates a new array, but does not keep it, it can give
> away it's ownership of the the array (ie, return a mutable).
Sorry, but you seem to have missed a bit above: if the string doesn't
contain any uppercase characters tolower returns the input without
.dup-ing it (aka copy-on-write).
> The second case, more specific, is simply syntactic sugar for making
> that array invariant:
>
> invariant(char)[] tolowerinv(const(char)[] str) {
> return cast(invariant) tolower(str);
> }
Yes, but only if it actually needs to modify the string.
You seem to have missed that the two cases can't (in general) be
distinguished at compile time; it's only at run time when a choice is
made between a copy and no copy.
> 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.
More information about the Digitalmars-d
mailing list