V2 string (general issues)

Kristian Kilpi kjkilpi at gmail.com
Fri Jul 6 04:22:45 PDT 2007


On Thu, 05 Jul 2007 22:11:37 +0300, Walter Bright  
<newshound1 at digitalmars.com> wrote:
> Kristian Kilpi wrote:
>> First, I am wondering why some functions are formed as follows:
>> (but I'm sure someone will (hopefully) enlight me about that ;) )
>>    string foo(string bar);
>>  That is, if they return something else than 'bar' (they do some string  
>> manipulation).
>> Shouldn't they return char[] instead?
>
> No, because then they must always dup the string. If they don't need to  
> dup the string, they can return a reference to the parameter, and if so,  
> it must be const.
>
>> There should be two different functions, one for each group:
>>    char[] tolower(char[] str);  //modifies and returns 'str'
>>    char[] getlower(string str);  //returns a copy
>
> When one would use a mutating tolower, one is already manipulating the  
> contents of a string character by character. In such cases, one can  
> tolower the characters in that process, instead of doing it later (the  
> former will be more efficient anyway, and the only advantage to a  
> mutating tolower is an efficiency improvement).

That makes sense (especially with strings).

Of course, as said, it's not a perfect solution because
unnecessary .dupping can occur.

For example:

   s = "blah " ~ foo(tolower(str).dup);

'foo()' modifies its input string and returns it.

If 'foo' would be a copy-on-write function, you could just do:

   s = "blah " ~ foo(tolower(str));

That's much nicer, but 'str' could be copied twice in both the cases above.
If both 'foo()' and 'tolower()' would modify 'str', no copying
had been done (by these functions).

Well, it's just how you like to code and build things.
Both the ways have their own pros and cons.



More information about the Digitalmars-d mailing list