Does anybody have an example of overloading a function to accept char[] or string parameter

bearophile bearophileHUGS at lycos.com
Sun Mar 30 09:51:29 PDT 2014


Gary Miller:

> 	char[] ReplaceAllSubstrings(inout char[] Original,
>                                     in char[] SearchString,
>                                     in char[] Substring)
> 	{
> 	    string SOriginal = Original.dup;
> 	    string SSearchString = SearchString.dup;
> 	    string SSubstring = Substring.dup;
> 	    SOriginal.replace(SSearchString, SSubstring);
>             return Original.dup;
>         }

Here if you care for some efficiency you need to dup only 
SOriginal. And at the end you can call assumeUnique if you want 
to return a string (or you can use a less efficient idup).

Note that in D string/function names start with a lowercase.

It's also better to use "auto" instead of "string" in that 
function, because the result of dup is not a string.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list