| Thread overview | |||||
|---|---|---|---|---|---|
|
March 30, 2014 Does anybody have an example of overloading a function to accept char[] or string parameter | ||||
|---|---|---|---|---|
| ||||
In a lot of my string manipulation functions I need the flexibility of passing a string but other times I need to pass a char[] so that I can change the contents of the string in the function. Because the string data type is immutable I get errors if I pass it as a parameter and try to change it in the function.
Can I overload a function to work for either a char[] or a string?
Many of the library functions that I need to call require string arguments such as the replace below.
I believe I can create a wrapper function for it like this to still get at the functionality. But this seems like a lot of work for every function.
I'm almost sorry that the the string datatype was created because then probably all the library string handling would have been written for char[].
Are there any alternate libraries for D that have a mutable string datatype or is there a way to override the immutable characteristic of the string datatype by reallocating it or something?
I realize that the reduced memory reallocation in string handling is probably a major reason that D is faster than other more dynamic languages like Python but
Maybe the functions in std.string could be overloaded into a std.mutablestring library at some point to eliminate emulate the functionality of more dynamic languages for those programs that need it.
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;
}
| ||||
March 30, 2014 Re: Does anybody have an example of overloading a function to accept char[] or string parameter | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Gary Miller | On Sunday, 30 March 2014 at 15:58:52 UTC, Gary Miller wrote:
> Are there any alternate libraries for D that have a mutable string datatype or is there a way to override the immutable characteristic of the string datatype by reallocating it or something?
string.dup property does a copy of original array(! note that this is array property and would work for any other arrays and slices).
you can have overloaded variant for strings but i think compiler would optimize to call everything as string variant, you can figure this out on your own.
| |||
March 30, 2014 Re: Does anybody have an example of overloading a function to accept char[] or string parameter | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Gary Miller | 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
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply