A debug class has started
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Dec 13 21:13:25 UTC 2021
On Mon, Dec 13, 2021 at 08:58:42PM +0000, forkit via Digitalmars-d-learn wrote:
[...]
> immutable(char)[] replaceChar(char* str, ulong len, char ch1, char ch2)
> {
> for (ulong i = 0; i < len; i++)
> {
> if (str[i] == ch1)
> {
> writefln("Found %c at str[%d]", ch1, i); // fine
> str[i] = ch2;
> }
> }
> return to!(immutable(char)[])(str);
This line is your problem: you have a raw pointer `str` and
force-casting it to an array without specifying its length. Do not
expect anything good to come of this. (In fact I'm surprised it .to even
accepts such a thing!)
What you should be doing is:
return to!string(str[0 .. len]);
Or just:
return str[0 .. len].idup;
T
--
Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. -- G. Steele
More information about the Digitalmars-d-learn
mailing list