Does anybody have an example of overloading a function to accept char[] or string parameter
Gary Miller
aiguy at roadrunner.com
Sun Mar 30 08:58:50 PDT 2014
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;
}
More information about the Digitalmars-d-learn
mailing list