V2 string
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Thu Jul 5 07:37:24 PDT 2007
Regan Heath wrote:
> Walter Bright Wrote:
>> Derek Parnell wrote:
>>> However, if I might need to update it ...
>>>
>>> char[] fullpath;
>>>
>>> fullpath = CanonicalPath(shortname).dup;
>>> version(Windows)
>>> {
>>> setLowerCase(fullpath);
>>> }
>>>
>>> The point is that the 'CanonicalPath' function hasn't got a clue what the
>>> calling function is intending to do with the result so it is trying to be
>>> responsible by guarding it against mistakes by the caller.
>> If you write it like this:
>>
>> string fullpath;
>>
>> fullpath = CanonicalPath(shortname);
>> version(Windows)
>> {
>> fullpath = std.string.tolower(fullpath);
>> }
>>
>> you won't need to do the .dup .
>
> Because tolower does it for you, but it still returns string and if for example you need to add something to the end of the path, like a filename you will end up doing yet another dup somewhere.
>
> I think the solution may be to template all functions which return the input string, or part of the input string, eg.
>
> T tolower(T)(T input)
> {
> }
>
> That way if you call it with char[] you get a char[] back, if you call it with string you get a string back.
>
It doesn't make sense to template it, because you'd still have two
different function versions, that would work differently. The one that
receives a string does a dup, the one that receives a char[] does not
dup. The return type of tolower(string str) might also be char[] and not
string, if tolower(string str) would allways does a dup, even if no
character modifications are necessary.
--
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list